mirror of
https://github.com/urosran/cally.git
synced 2025-07-15 17:47:08 +00:00
Deletion fix
This commit is contained in:
@ -2,88 +2,134 @@ import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {useMutation, useQueryClient} from "@tanstack/react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {EventData} from "@/hooks/firebase/types/eventData";
|
||||
import {useAtomValue} from "jotai";
|
||||
import {isFamilyViewAtom} from "@/components/pages/calendar/atoms";
|
||||
|
||||
export const useCreateEvent = () => {
|
||||
const {user: currentUser, profileData} = useAuthContext()
|
||||
const queryClients = useQueryClient()
|
||||
const {user: currentUser, profileData} = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createEvent"],
|
||||
mutationFn: async (eventData: Partial<EventData>) => {
|
||||
try {
|
||||
if (eventData.id) {
|
||||
const snapshot = await firestore()
|
||||
.collection("Events")
|
||||
.where("id", "==", eventData.id)
|
||||
.get();
|
||||
const newDoc = firestore().collection('Events').doc();
|
||||
await firestore()
|
||||
.collection("Events")
|
||||
.add({
|
||||
...eventData,
|
||||
id: newDoc.id,
|
||||
creatorId: currentUser?.uid,
|
||||
familyId: profileData?.familyId
|
||||
});
|
||||
},
|
||||
onMutate: async (newEvent) => {
|
||||
await queryClient.cancelQueries({
|
||||
queryKey: ["events", currentUser?.uid]
|
||||
});
|
||||
|
||||
if (!snapshot.empty) {
|
||||
const docId = snapshot.docs[0].id;
|
||||
await firestore()
|
||||
.collection("Events")
|
||||
.doc(docId)
|
||||
.set({
|
||||
...eventData,
|
||||
attendees: (eventData.attendees?.length ?? 0),
|
||||
creatorId: currentUser?.uid,
|
||||
familyId: profileData?.familyId
|
||||
}, {merge: true});
|
||||
return;
|
||||
}
|
||||
}
|
||||
const newDoc = firestore().collection('Events').doc();
|
||||
await firestore()
|
||||
.collection("Events")
|
||||
.add({...eventData, id: newDoc.id, creatorId: currentUser?.uid, familyId: profileData?.familyId});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
const formattedEvent = {
|
||||
...newEvent,
|
||||
start: newEvent.startDate,
|
||||
end: newEvent.endDate,
|
||||
id: Date.now().toString(),
|
||||
creatorId: currentUser?.uid,
|
||||
familyId: profileData?.familyId,
|
||||
eventColor: profileData?.eventColor,
|
||||
hideHours: newEvent.allDay,
|
||||
};
|
||||
|
||||
["false", "true"].forEach(viewState => {
|
||||
const queryKey = ["events", currentUser?.uid, viewState === "true"];
|
||||
const previousData = queryClient.getQueryData(queryKey) as any[] || [];
|
||||
queryClient.setQueryData(queryKey, [...previousData, formattedEvent]);
|
||||
});
|
||||
},
|
||||
onSettled: () => {
|
||||
if (profileData?.familyId) {
|
||||
firestore()
|
||||
.collection("Households")
|
||||
.where("familyId", "==", profileData.familyId)
|
||||
.get()
|
||||
.then(snapshot => {
|
||||
snapshot.docs.forEach(doc => {
|
||||
doc.ref.update({
|
||||
lastSyncTimestamp: firestore.FieldValue.serverTimestamp()
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const useCreateEventsFromProvider = () => {
|
||||
const {user: currentUser} = useAuthContext();
|
||||
const {user: currentUser, profileData} = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createEventsFromProvider"],
|
||||
mutationFn: async (eventDataArray: Partial<EventData>[]) => {
|
||||
try {
|
||||
// Create an array of promises for each event's Firestore read/write operation
|
||||
const promises = eventDataArray.map(async (eventData) => {
|
||||
console.log("Processing EventData: ", eventData);
|
||||
const promises = eventDataArray.map(async (eventData) => {
|
||||
const snapshot = await firestore()
|
||||
.collection("Events")
|
||||
.where("id", "==", eventData.id)
|
||||
.get();
|
||||
|
||||
// Check if the event already exists
|
||||
const snapshot = await firestore()
|
||||
if (snapshot.empty) {
|
||||
return firestore()
|
||||
.collection("Events")
|
||||
.where("id", "==", eventData.id)
|
||||
.get();
|
||||
.add({...eventData, creatorId: currentUser?.uid});
|
||||
}
|
||||
const docId = snapshot.docs[0].id;
|
||||
return firestore()
|
||||
.collection("Events")
|
||||
.doc(docId)
|
||||
.set({...eventData, creatorId: currentUser?.uid}, {merge: true});
|
||||
});
|
||||
|
||||
if (snapshot.empty) {
|
||||
// Event doesn't exist, so add it
|
||||
return firestore()
|
||||
.collection("Events")
|
||||
.add({...eventData, creatorId: currentUser?.uid});
|
||||
} else {
|
||||
// Event exists, update it
|
||||
const docId = snapshot.docs[0].id;
|
||||
return firestore()
|
||||
.collection("Events")
|
||||
.doc(docId)
|
||||
.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);
|
||||
}
|
||||
await Promise.all(promises);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({queryKey: ["events"]});
|
||||
onMutate: async (newEvents) => {
|
||||
await queryClient.cancelQueries({queryKey: ["events", currentUser?.uid]});
|
||||
|
||||
const previousPersonalEvents = queryClient.getQueryData(["events", currentUser?.uid, false]);
|
||||
const previousFamilyEvents = queryClient.getQueryData(["events", currentUser?.uid, true]);
|
||||
|
||||
const formattedEvents = newEvents.map(event => ({
|
||||
...event,
|
||||
start: new Date(event.startDate.seconds * 1000),
|
||||
end: new Date(event.endDate.seconds * 1000),
|
||||
hideHours: event.allDay,
|
||||
eventColor: profileData?.eventColor,
|
||||
creatorId: currentUser?.uid,
|
||||
familyId: profileData?.familyId
|
||||
}));
|
||||
|
||||
const updateQueryData = (old: any[] = []) => [...old, ...formattedEvents];
|
||||
|
||||
queryClient.setQueryData(["events", currentUser?.uid, false], updateQueryData);
|
||||
queryClient.setQueryData(["events", currentUser?.uid, true], updateQueryData);
|
||||
|
||||
return {previousPersonalEvents, previousFamilyEvents};
|
||||
},
|
||||
onError: (err, newEvents, context) => {
|
||||
queryClient.setQueryData(["events", currentUser?.uid, false], context?.previousPersonalEvents);
|
||||
queryClient.setQueryData(["events", currentUser?.uid, true], context?.previousFamilyEvents);
|
||||
},
|
||||
onSettled: () => {
|
||||
if (profileData?.familyId) {
|
||||
firestore()
|
||||
.collection("Households")
|
||||
.where("familyId", "==", profileData.familyId)
|
||||
.get()
|
||||
.then(snapshot => {
|
||||
snapshot.docs.forEach(doc => {
|
||||
doc.ref.update({
|
||||
lastSyncTimestamp: firestore.FieldValue.serverTimestamp()
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user