mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 16:34:54 +00:00
Small fixes, calendar token refresh for google
This commit is contained in:
39
hooks/firebase/useClearTokens.ts
Normal file
39
hooks/firebase/useClearTokens.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import {useMutation} from "react-query";
|
||||
import {UserProfile} from "@firebase/auth";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {useUpdateUserData} from "@/hooks/firebase/useUpdateUserData";
|
||||
|
||||
export const useClearTokens = () => {
|
||||
const {profileData} = useAuthContext();
|
||||
const {mutateAsync: updateUserData} = useUpdateUserData();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["clearTokens"],
|
||||
mutationFn: async ({provider, email}: {
|
||||
provider: "google" | "outlook" | "apple",
|
||||
email: string
|
||||
}) => {
|
||||
const newUserData: Partial<UserProfile> = {};
|
||||
if (provider === "google") {
|
||||
let googleAccounts = profileData?.googleAccounts;
|
||||
if (googleAccounts) {
|
||||
googleAccounts[email] = null;
|
||||
newUserData.googleAccounts = googleAccounts;
|
||||
}
|
||||
} else if (provider === "outlook") {
|
||||
let microsoftAccounts = profileData?.microsoftAccounts;
|
||||
if (microsoftAccounts) {
|
||||
microsoftAccounts[email] = null;
|
||||
newUserData.microsoftAccounts = microsoftAccounts;
|
||||
}
|
||||
} else if (provider === "apple") {
|
||||
let appleAccounts = profileData?.appleAccounts;
|
||||
if (appleAccounts) {
|
||||
appleAccounts[email] = null;
|
||||
newUserData.appleAccounts = appleAccounts;
|
||||
}
|
||||
}
|
||||
await updateUserData({newUserData});
|
||||
},
|
||||
})
|
||||
}
|
||||
@ -45,34 +45,41 @@ export const useCreateEvent = () => {
|
||||
}
|
||||
|
||||
export const useCreateEventsFromProvider = () => {
|
||||
const {user: currentUser} = useAuthContext();
|
||||
const { user: currentUser } = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createEventsFromProvider"],
|
||||
mutationFn: async (eventDataArray: Partial<EventData>[]) => {
|
||||
try {
|
||||
for (const eventData of eventDataArray) {
|
||||
// Create an array of promises for each event's Firestore read/write operation
|
||||
const promises = eventDataArray.map(async (eventData) => {
|
||||
console.log("Processing EventData: ", eventData);
|
||||
|
||||
// Check if the event already exists
|
||||
const snapshot = await firestore()
|
||||
.collection("Events")
|
||||
.where("id", "==", eventData.id)
|
||||
.get();
|
||||
|
||||
if (snapshot.empty) {
|
||||
await firestore()
|
||||
// Event doesn't exist, so add it
|
||||
return firestore()
|
||||
.collection("Events")
|
||||
.add({...eventData, creatorId: currentUser?.uid});
|
||||
.add({ ...eventData, creatorId: currentUser?.uid });
|
||||
} else {
|
||||
console.log("Event already exists, updating...");
|
||||
// Event exists, update it
|
||||
const docId = snapshot.docs[0].id;
|
||||
await firestore()
|
||||
return firestore()
|
||||
.collection("Events")
|
||||
.doc(docId)
|
||||
.set({...eventData, creatorId: currentUser?.uid}, {merge: true});
|
||||
.set({ ...eventData, creatorId: currentUser?.uid }, { merge: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Execute all promises in parallel
|
||||
await Promise.all(promises);
|
||||
|
||||
} catch (e) {
|
||||
console.error("Error creating/updating events: ", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user