mirror of
https://github.com/urosran/cally.git
synced 2025-07-16 10:06:15 +00:00
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
import {useAuthContext} from "@/contexts/AuthContext";
|
|
import {useMutation, useQueryClient} from "react-query";
|
|
import firestore from "@react-native-firebase/firestore";
|
|
import {EventData} from "@/hooks/firebase/types/eventData";
|
|
|
|
export const useCreateEvent = () => {
|
|
const {user: currentUser, profileData} = useAuthContext()
|
|
const queryClients = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationKey: ["createEvent"],
|
|
mutationFn: async (eventData: Partial<EventData>) => {
|
|
try {
|
|
await firestore()
|
|
.collection("Events")
|
|
.add({...eventData, creatorId: currentUser?.uid, familyId: profileData?.familyId})
|
|
} catch (e) {
|
|
console.error(e)
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClients.invalidateQueries("events")
|
|
}
|
|
})
|
|
}
|
|
|
|
export const useCreateEventsFromProvider = () => {
|
|
const {user: currentUser} = useAuthContext();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationKey: ["createEventsFromProvider"],
|
|
mutationFn: async (eventDataArray: Partial<EventData>[]) => {
|
|
try {
|
|
for (const eventData of eventDataArray) {
|
|
console.log("Processing EventData: ", eventData);
|
|
|
|
const snapshot = await firestore()
|
|
.collection("Events")
|
|
.where("id", "==", eventData.id)
|
|
.get();
|
|
|
|
if (snapshot.empty) {
|
|
await firestore()
|
|
.collection("Events")
|
|
.add({...eventData, creatorId: currentUser?.uid});
|
|
} else {
|
|
console.log("Event already exists, updating...");
|
|
const docId = snapshot.docs[0].id;
|
|
await firestore()
|
|
.collection("Events")
|
|
.doc(docId)
|
|
.set({...eventData, creatorId: currentUser?.uid}, {merge: true});
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error("Error creating/updating events: ", e);
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries("events");
|
|
}
|
|
});
|
|
}; |