From 7eb040a8a93fbc766750c6894a09a475b2d247c1 Mon Sep 17 00:00:00 2001 From: Dejan Date: Sat, 5 Oct 2024 15:53:20 +0200 Subject: [PATCH] - Implemented google sign in and saved the id token in db for the current user - Used the id token for the current user for fetching the google calendar events --- .../pages/settings/CalendarSettingsPage.tsx | 114 +++++++++--------- 1 file changed, 60 insertions(+), 54 deletions(-) diff --git a/components/pages/settings/CalendarSettingsPage.tsx b/components/pages/settings/CalendarSettingsPage.tsx index dc2ec83..347f30f 100644 --- a/components/pages/settings/CalendarSettingsPage.tsx +++ b/components/pages/settings/CalendarSettingsPage.tsx @@ -7,23 +7,38 @@ import { TouchableOpacity } from "react-native-gesture-handler"; import { fetchGoogleCalendarEvents } from "@/calendar-integration/google-calendar-utils"; import { fetchMicrosoftCalendarEvents } from "@/calendar-integration/microsoft-calendar-utils"; import { useCreateEvent } from "@/hooks/firebase/useCreateEvent"; +import { useAuthContext } from "@/contexts/AuthContext"; +import { useUpdateUserData } from "@/hooks/firebase/useUpdateUserData"; import { GoogleSignin } from "@react-native-google-signin/google-signin"; +GoogleSignin.configure({ + webClientId: + "406146460310-hjadmfa1gg4ptaouira5rkhu0djlo5ut.apps.googleusercontent.com", + scopes: ["profile", "email"], // Note: add calendar scope +}); + +const GoogleLogin = async () => { + return await GoogleSignin.signIn(); +}; + const CalendarSettingsPage = (props: { setSelectedPage: (page: number) => void; }) => { const [selectedColor, setSelectedColor] = useState(colorMap.pink); const [startDate, setStartDate] = useState(true); + const { profileData } = useAuthContext(); const { mutateAsync: createEvent } = useCreateEvent(); + const { mutateAsync: updateUserData } = useUpdateUserData(); - // Note: user token for the correct provider is needed - const fetchAndSaveGoogleEvents = (token: string) => { + const fetchAndSaveGoogleEvents = () => { const timeMin = new Date(new Date().setHours(0, 0, 0, 0)).toISOString(); - fetchGoogleCalendarEvents(token, timeMin).then((response) => { - response?.forEach((item) => saveData(item)); - }); + fetchGoogleCalendarEvents(profileData?.googleToken, timeMin).then( + (response) => { + response?.forEach((item) => createEvent(item)); + }, + ); }; const fetchAndSaveMicrosoftEvents = (token: string) => { @@ -43,9 +58,21 @@ const CalendarSettingsPage = (props: { }); }; - async function saveData(item) { - await createEvent(item); - } + const handleGoogleLogin = async () => { + try { + const response = await GoogleLogin(); + if (response) { + const googleUserData = response.data; + let idToken = googleUserData?.idToken; + + if (idToken) { + await updateUserData({ newUserData: { googleToken: idToken } }); + } + } + } catch (apiError) { + console.log(apiError || "Something went wrong"); + } + }; return ( @@ -140,46 +167,44 @@ const CalendarSettingsPage = (props: { width={40} height={40} style={{ borderRadius: 50 }} - marginR-10 centerV centerH > - + )} - icon={} backgroundColor="white" color="#464039" borderRadius={15} - onPress={signInGoogle} + onPress={handleGoogleLogin} /> Calendars - {/** Note: Should check for the user if it has connected calendars **/} -