Syncing improvement

This commit is contained in:
Milan Paunovic
2024-10-19 19:55:46 +02:00
parent 043b80baa9
commit 00b6225a1c
5 changed files with 80 additions and 63 deletions

View File

@ -1,7 +1,7 @@
import { useAuthContext } from "@/contexts/AuthContext";
import { useMutation, useQueryClient } from "react-query";
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";
import {EventData} from "@/hooks/firebase/types/eventData";
export const useCreateEvent = () => {
const {user: currentUser, profileData} = useAuthContext()
@ -11,7 +11,6 @@ export const useCreateEvent = () => {
mutationKey: ["createEvent"],
mutationFn: async (eventData: Partial<EventData>) => {
try {
console.log("CALLLLL")
await firestore()
.collection("Events")
.add({...eventData, creatorId: currentUser?.uid, familyId: profileData?.familyId})
@ -25,37 +24,41 @@ export const useCreateEvent = () => {
})
}
export const useCreateEventFromProvider = () => {
const {user: currentUser} = useAuthContext()
const queryClients = useQueryClient()
export const useCreateEventsFromProvider = () => {
const {user: currentUser} = useAuthContext();
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["createEventFromProvider"],
mutationFn: async (eventData: Partial<EventData>) => {
mutationKey: ["createEventsFromProvider"],
mutationFn: async (eventDataArray: Partial<EventData>[]) => {
try {
const snapshot = await firestore()
.collection("Events")
.where("id", "==", eventData.id)
.get();
for (const eventData of eventDataArray) {
console.log("Processing EventData: ", eventData);
if (snapshot.empty) {
await firestore()
const snapshot = await firestore()
.collection("Events")
.add({...eventData, creatorId: currentUser?.uid})
} else {
console.log("ENTER HERE")
const docId = snapshot.docs[0].id;
await firestore()
.collection("Events")
.doc(docId)
.update({...eventData, creatorId: currentUser?.uid});
.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(e)
console.error("Error creating/updating events: ", e);
}
},
onSuccess: () => {
queryClients.invalidateQueries("events")
queryClient.invalidateQueries("events");
}
})
}
});
};