Files
cally/calendar-integration/google-calendar-utils.js
Dejan 68e1e840a0 - Introduced a google-calendar-utils.js and function to fetch Google calendar events for the provided token
- Handled the response from the google calendar api to work with the eventData model for firebase
2024-10-03 23:37:25 +02:00

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"
};