sync update

This commit is contained in:
Milan Paunovic
2024-11-02 12:00:49 +01:00
parent a6b2c4b97f
commit 8a0370933d
4 changed files with 209 additions and 7 deletions
+47
View File
@@ -8,6 +8,8 @@ import * as WebBrowser from "expo-web-browser";
import * as Google from "expo-auth-session/providers/google";
import * as AuthSession from "expo-auth-session";
import * as AppleAuthentication from "expo-apple-authentication";
import * as Notifications from 'expo-notifications';
import {useQueryClient} from "react-query";
const googleConfig = {
androidClientId:
@@ -44,6 +46,7 @@ const microsoftConfig = {
export const useCalSync = () => {
const {profileData} = useAuthContext();
const queryClient = useQueryClient();
const {mutateAsync: updateUserData} = useUpdateUserData();
const {mutateAsync: fetchAndSaveGoogleEvents, isLoading: isSyncingGoogle} = useFetchAndSaveGoogleEvents();
@@ -235,6 +238,35 @@ export const useCalSync = () => {
};
const resyncAllCalendars = async (): Promise<void> => {
try {
const syncPromises: Promise<void>[] = [];
if (profileData?.googleAccounts) {
for (const [email, { accessToken, refreshToken }] of Object.entries(profileData.googleAccounts)) {
syncPromises.push(fetchAndSaveGoogleEvents({ token: accessToken, refreshToken, email }));
}
}
if (profileData?.microsoftAccounts) {
for (const [email, accessToken] of Object.entries(profileData.microsoftAccounts)) {
syncPromises.push(fetchAndSaveOutlookEvents(accessToken, email));
}
}
if (profileData?.appleAccounts) {
for (const [email, token] of Object.entries(profileData.appleAccounts)) {
syncPromises.push(fetchAndSaveAppleEvents({ token, email }));
}
}
await Promise.all(syncPromises);
console.log("All calendars have been resynced.");
} catch (error) {
console.error("Error resyncing calendars:", error);
}
};
let isConnectedToGoogle = false;
if (profileData?.googleAccounts) {
Object.values(profileData?.googleAccounts).forEach((item) => {
@@ -267,6 +299,20 @@ export const useCalSync = () => {
}
useEffect(() => {
const handleNotification = async (notification: Notifications.Notification) => {
const eventId = notification?.request?.content?.data?.eventId;
await resyncAllCalendars();
queryClient.invalidateQueries(["events"]);
};
const sub = Notifications.addNotificationReceivedListener(handleNotification);
return () => sub.remove();
}, []);
return {
handleAppleSignIn,
handleMicrosoftSignIn,
@@ -281,6 +327,7 @@ export const useCalSync = () => {
isSyncingOutlook,
isSyncingGoogle,
isSyncingApple,
resyncAllCalendars,
isSyncing: isSyncingApple || isSyncingOutlook || isSyncingGoogle
}
}