mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 15:17:17 +00:00
83 lines
3.1 KiB
TypeScript
83 lines
3.1 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 {
|
|
if (eventData.id) {
|
|
const snapshot = await firestore()
|
|
.collection("Events")
|
|
.where("id", "==", eventData.id)
|
|
.get();
|
|
|
|
if (!snapshot.empty) {
|
|
const docId = snapshot.docs[0].id;
|
|
await firestore()
|
|
.collection("Events")
|
|
.doc(docId)
|
|
.set({
|
|
...eventData,
|
|
creatorId: currentUser?.uid,
|
|
familyId: profileData?.familyId
|
|
}, {merge: true});
|
|
return;
|
|
}
|
|
}
|
|
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");
|
|
}
|
|
});
|
|
}; |