Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function addIOS(event: Event) {
let status = await CalendarEventsIOS.authorizationStatus();
if (status === 'undetermined') {
try {
// TODO(localization)
await OkCancelAlert(
'Add to Calendar',
'To add this event to your calendar, you need to allow access to your calendar.'
);
status = await CalendarEventsIOS.authorizeEventStore();
} catch (error) {
console.log('Canceled: Add to Calendar');
return false;
}
}
if (status !== 'authorized') {
if (status === 'restricted') {
OkAlert('Cannot Access Calendar', 'Could not access calendar.');
return false;
} else if (status === 'denied') {
try {
// TODO(localization)
await OkCancelAlert(
'Cannot Access Calendar',
'Please open Settings to allow Calendar permissions.'
async function addIOS(event: Event) {
let status = await CalendarEventsIOS.authorizationStatus();
if (status === 'undetermined') {
try {
// TODO(localization)
await OkCancelAlert(
'Add to Calendar',
'To add this event to your calendar, you need to allow access to your calendar.'
);
status = await CalendarEventsIOS.authorizeEventStore();
} catch (error) {
console.log('Canceled: Add to Calendar');
return false;
}
}
if (status !== 'authorized') {
{text: '继续', style: 'warning', onPress: async () => {
// 时间跨度超过 5 年,就不能获取到了...
const startDate = moment().subtract(4, 'years').toISOString();
const endDate = moment().add(1, 'years').toISOString();
const allEvents = await RNCalendarEvents.fetchAllEvents(startDate, endDate);
await allEvents.forEach(async item => {
console.log(item);
if (item.notes === '由应用UESTC创建') {
await RNCalendarEvents.removeEvent(item.id, {
futureEvents: true
});
}
});
await this.props.rootStore.UserStore.toast('success', '🎉 已从系统日历中删除所有课程');
await this.props.rootStore.UserStore.clearToast();
}
}
export async function addToCalendar(event: EventType): Promise {
try {
let authCode = await RNCalendarEvents.authorizationStatus()
let authStatus =
authCode === 'authorized' ? true : await requestCalendarAccess()
if (!authStatus) {
return false
}
return await saveEventToCalendar(event)
} catch (error) {
Sentry.captureException(error)
console.error(error)
return false
}
}
'Please open Settings to allow Calendar permissions.'
);
if (await Permissions.canOpenSettings()) {
Permissions.openSettings();
}
} catch (err) {
console.log('Canceled: Add to Calendar Permissions');
}
}
return false;
}
const { start, end } = getStartEndTime(event);
try {
CalendarEventsIOS.saveEvent(event.name, {
location: event.venue.fullAddress(),
notes: getDescription(event),
startDate: start.toISOString(),
endDate: end.toISOString(),
url: event.getUrl(),
});
} catch (e) {
console.warn(e);
}
return true;
}
async function saveEventToCalendar(event: EventType): Promise {
try {
await RNCalendarEvents.saveEvent(event.title, {
location: event.location,
startDate: event.startTime.toISOString(),
endDate: event.endTime.toISOString(),
description: event.description,
notes: event.description,
})
return true
} catch (err) {
Sentry.captureException(err)
console.error(err)
return false
}
}
}).then(id => {
RNCalendarEvents.saveEvent(title, {
id,
alarms: this.state.needNotice ? [{
date: before,
}] : []
}, {
futureEvents: true,
}).then((id) => {
idArray.push(id);
console.log(idArray.length);
console.log(courseData.filter(item => item.checked));
if (idArray.length === courseData.filter(item => item.checked).length) {
this.props.rootStore.LoadingStore.loading(false);
this.props.rootStore.UserStore.toast('success', `🎉 成功导入${idArray.length}节课程!可在系统日历中查看`);
this.props.rootStore.UserStore.clearToast();
}
});
async checkPermission() {
const authStatus = await RNCalendarEvents.authorizationStatus();
if (authStatus === 'undetermined') {
const authResult = await RNCalendarEvents.authorizeEventStore();
if (authResult !== 'authorized') await this.requestPermission();
} else if (authStatus !== 'authorized') {
await this.requestPermission();
}
}
.then(status => {
if (status === 'authorized') {
resolve(true);
} else if (promptForPermission) {
RNCalendarEvents.authorizeEventStore()
.then(result => {
dispatch(setCalendarAuthorization(result));
resolve(result === 'authorized');
})
.catch(reject);
} else {
resolve(false);
}
})
.catch(reject);
async function requestCalendarAccess(): Promise {
let status = null
try {
status = await RNCalendarEvents.authorizeEventStore()
} catch (err) {
return false
}
if (status !== 'authorized') {
return promptSettings()
}
return true
}