mirror of
https://github.com/urosran/cally.git
synced 2025-07-09 22:57:16 +00:00
46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
import * as Calendar from 'expo-calendar';
|
|
|
|
export async function fetchiPhoneCalendarEvents(familyId, email, startDate, endDate) {
|
|
try {
|
|
const {granted} = await Calendar.requestCalendarPermissionsAsync();
|
|
|
|
if (!granted) {
|
|
throw new Error("Calendar permission not granted");
|
|
}
|
|
|
|
const defaultCalendarSource = await Calendar.getDefaultCalendarAsync();
|
|
|
|
if (!defaultCalendarSource) {
|
|
throw new Error("No calendar found");
|
|
}
|
|
|
|
const events = await Calendar.getEventsAsync(
|
|
[defaultCalendarSource.id],
|
|
startDate,
|
|
endDate
|
|
);
|
|
|
|
return events.map((event) => {
|
|
let isAllDay = event.allDay || false;
|
|
const startDateTime = new Date(event.startDate);
|
|
let endDateTime = new Date(event.endDate);
|
|
|
|
if (isAllDay) {
|
|
endDateTime = startDateTime
|
|
}
|
|
|
|
return {
|
|
id: event.id,
|
|
title: event.title,
|
|
startDate: startDateTime,
|
|
endDate: endDateTime,
|
|
allDay: isAllDay,
|
|
familyId,
|
|
email
|
|
};
|
|
});
|
|
} catch (error) {
|
|
console.error("Error fetching iPhone Calendar events: ", error);
|
|
throw error;
|
|
}
|
|
} |