mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 08:24:55 +00:00
- Handled the response from the google calendar api to work with the eventData model for firebase
66 lines
1.9 KiB
JavaScript
66 lines
1.9 KiB
JavaScript
export async function fetchGoogleCalendarEvents(token) {
|
|
|
|
const response = await fetch(
|
|
'https://www.googleapis.com/calendar/v3/calendars/primary/events',
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
}
|
|
);
|
|
|
|
const data = await response.json();
|
|
const googleEvents = [];
|
|
data.items?.forEach((item) => {
|
|
|
|
let isAllDay = false;
|
|
const start = item.start;
|
|
let startDateTime;
|
|
if (start !== undefined) {
|
|
|
|
const timezone = start.timeZone;
|
|
if (start.dateTime) {
|
|
const stringDate = start.dateTime;
|
|
startDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone});
|
|
} else {
|
|
const stringDate = start.date;
|
|
startDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone});
|
|
isAllDay = true;
|
|
}
|
|
}
|
|
|
|
const end = item.end;
|
|
let endDateTime;
|
|
if (end !== undefined) {
|
|
const timezone = end.timeZone;
|
|
if (end.dateTime) {
|
|
const stringDate = end.dateTime;
|
|
endDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone});
|
|
} else {
|
|
const stringDate = end.date;
|
|
endDateTime = new Date(stringDate).toLocaleString("en-us", {...options, timeZone: timezone});
|
|
}
|
|
}
|
|
|
|
const googleEvent = {
|
|
id: item.id,
|
|
title: item.summary,
|
|
startDate: startDateTime,
|
|
endDate: endDateTime,
|
|
allDay: isAllDay
|
|
};
|
|
googleEvents.push(googleEvent);
|
|
});
|
|
|
|
return googleEvents;
|
|
}
|
|
|
|
const options = {
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
hour: "numeric",
|
|
minute: "numeric",
|
|
second: "numeric",
|
|
timeZoneName: "short"
|
|
}; |