diff --git a/calendar-integration/google-calendar-utils.js b/calendar-integration/google-calendar-utils.js index c7d332c..c91dcf8 100644 --- a/calendar-integration/google-calendar-utils.js +++ b/calendar-integration/google-calendar-utils.js @@ -1,4 +1,4 @@ -export async function fetchGoogleCalendarEvents(token, startDate, endDate) { +export async function fetchGoogleCalendarEvents(token, email, startDate, endDate) { console.log(token); const response = await fetch( `https://www.googleapis.com/calendar/v3/calendars/primary/events?single_events=true&time_min=${startDate}&time_max=${endDate}`, @@ -45,6 +45,7 @@ export async function fetchGoogleCalendarEvents(token, startDate, endDate) { startDate: startDateTime, endDate: endDateTime, allDay: isAllDay, + email }; googleEvents.push(googleEvent); }); diff --git a/calendar-integration/microsoft-calendar-utils.js b/calendar-integration/microsoft-calendar-utils.js index b459a29..cbf5d66 100644 --- a/calendar-integration/microsoft-calendar-utils.js +++ b/calendar-integration/microsoft-calendar-utils.js @@ -1,4 +1,4 @@ -export async function fetchMicrosoftCalendarEvents(token, startDate, endDate) { +export async function fetchMicrosoftCalendarEvents(token, email, startDate, endDate) { const response = await fetch( `https://graph.microsoft.com/v1.0/me/calendar/calendarView?startDateTime=${startDate}&endDateTime=${endDate}`, { @@ -34,6 +34,7 @@ export async function fetchMicrosoftCalendarEvents(token, startDate, endDate) { startDate: startDateTime, endDate: endDateTime, allDay: item.isAllDay, + email }; diff --git a/components/pages/calendar/EventCalendar.tsx b/components/pages/calendar/EventCalendar.tsx index 98cc566..9f34d54 100644 --- a/components/pages/calendar/EventCalendar.tsx +++ b/components/pages/calendar/EventCalendar.tsx @@ -23,6 +23,8 @@ export const EventCalendar: React.FC = memo(({calendarHeight const setEventForEdit = useSetAtom(eventForEditAtom) const setSelectedNewEndDate = useSetAtom(selectedNewEventDateAtom) + console.log("Events: ", events) + return ( { - console.log("fetch"); - const timeMin = new Date(new Date().setHours(0, 0, 0, 0)); - const timeMax = new Date( - new Date(new Date().setHours(0, 0, 0, 0)).setDate(timeMin.getDate() + 30) - ); + const fetchAndSaveGoogleEvents = async (token?: string, email?: string) => { + console.log("Fetching Google Calendar events..."); + const timeMin = new Date(new Date().setFullYear(new Date().getFullYear() - 1)); + const timeMax = new Date(new Date().setFullYear(new Date().getFullYear() + 5)); + console.log("Token: ", token ?? profileData?.googleToken) fetchGoogleCalendarEvents( - profileData?.googleToken, + token ?? profileData?.googleToken, + email ?? profileData?.googleMail, timeMin.toISOString().slice(0, -5) + "Z", timeMax.toISOString().slice(0, -5) + "Z" - ).then((response) => { - response?.forEach((item) => saveData(item)); + ).then(async (response) => { + console.log("Google Calendar events fetched:", response); + const items = response?.map((item) => { + if (item.allDay) { + item.startDate = new Date(new Date(item.startDate).setHours(0, 0, 0, 0)) + item.endDate = item.startDate + } + return item; + }) || []; + await createEventsFromProvider(items); + }).catch((error) => { + console.error("Error fetching Google Calendar events:", error); }); }; - async function saveData(item: any) { - await createEventFromProvider(item); - } + const fetchAndSaveMicrosoftEvents = async (token?: string, email?: string) => { + const timeMin = new Date(new Date().setFullYear(new Date().getFullYear() - 1)); + const timeMax = new Date(new Date().setFullYear(new Date().getFullYear() + 3)); - const fetchAndSaveMicrosoftEvents = () => { - const startDateTime = new Date(new Date().setHours(0, 0, 0, 0)); - const endDateTime = new Date( - new Date(new Date().setHours(0, 0, 0, 0)).setDate( - startDateTime.getDate() + 30 - ) - ); - - fetchMicrosoftCalendarEvents( - profileData?.microsoftToken, - startDateTime.toISOString().slice(0, -5) + "Z", - endDateTime.toISOString().slice(0, -5) + "Z" - ).then((response) => { + console.log("Token: ", token ?? profileData?.microsoftToken) + fetchMicrosoftCalendarEvents( + token ?? profileData?.microsoftToken, + email ?? profileData?.outlookMail, + timeMin.toISOString().slice(0, -5) + "Z", + timeMax.toISOString().slice(0, -5) + "Z" + ).then(async (response) => { console.log(response); - response?.forEach((item) => saveData(item)); + const items = response ?? []; + await createEventsFromProvider(items); }); }; @@ -126,6 +131,8 @@ const CalendarSettingsPage = (props: { await updateUserData({ newUserData: {googleToken: accessToken, googleMail: googleMail}, }); + + await fetchAndSaveGoogleEvents(accessToken, googleMail) } } catch (error) { console.error("Error during Google sign-in:", error); @@ -207,6 +214,7 @@ const CalendarSettingsPage = (props: { newUserData: {microsoftToken: tokenData.access_token, outlookMail: outlookMail}, }); + await fetchAndSaveMicrosoftEvents(tokenData.access_token, outlookMail) console.log("User data updated successfully."); } } @@ -257,7 +265,7 @@ const CalendarSettingsPage = (props: { return ( - + props.setSelectedPage(0)}> ( @@ -374,7 +382,7 @@ const CalendarSettingsPage = (props: { label="Connect Apple" labelStyle={styles.addCalLbl} labelProps={{ - numberOfLines:2 + numberOfLines: 2 }} iconSource={() => ( @@ -390,7 +398,7 @@ const CalendarSettingsPage = (props: { label={profileData?.microsoftToken ? `Disconnect ${profileData.outlookMail}` : "Connect Outlook"} labelStyle={styles.addCalLbl} labelProps={{ - numberOfLines:2 + numberOfLines: 2 }} iconSource={() => ( @@ -412,9 +420,10 @@ const CalendarSettingsPage = (props: { {!!profileData?.googleMail && (