mirror of
https://github.com/urosran/cally.git
synced 2026-10-23 07:27:55 +00:00
sync update
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user