- Introduced a button for syncing Google calendar

- Implemented saving of the google event data in db
- Added start date to the google calendar fetch api endpoint
This commit is contained in:
Dejan
2024-10-04 01:04:11 +02:00
parent 68e1e840a0
commit ba74025589
2 changed files with 117 additions and 61 deletions

View File

@ -1,66 +1,75 @@
export async function fetchGoogleCalendarEvents(token) {
export async function fetchGoogleCalendarEvents(token, startDate) {
const response = await fetch(
`https://www.googleapis.com/calendar/v3/calendars/primary/events?single_events=true&time_min=${startDate}`,
{
headers: {
Authorization: `Bearer ${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 data = await response.json();
const googleEvents = [];
data.items?.forEach((item) => {
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,
});
}
}
let isAllDay = false;
const start = item.start;
let startDateTime;
if (start !== undefined) {
const googleEvent = {
id: item.id,
title: item.summary,
startDate: startDateTime,
endDate: endDateTime,
allDay: isAllDay,
};
googleEvents.push(googleEvent);
});
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;
return googleEvents;
}
const options = {
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
timeZoneName: "short"
};
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
timeZoneName: "short",
};