mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +00:00
- 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
This commit is contained in:
66
calendar-integration/google-calendar-utils.js
Normal file
66
calendar-integration/google-calendar-utils.js
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
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"
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user