mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +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()
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
@ -6,10 +6,11 @@ import { IGrocery } from "@/hooks/firebase/types/groceryData";
|
||||
export const useCreateGrocery = () => {
|
||||
const { user: currentUser, profileData } = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
const groceriesKey = ["groceries"];
|
||||
const groceriesKey = ["groceries", currentUser?.uid];
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (groceryData: Partial<IGrocery>) => {
|
||||
console.log("Call")
|
||||
const newDoc = firestore().collection('Groceries').doc();
|
||||
return firestore()
|
||||
.collection("Groceries")
|
||||
@ -20,10 +21,9 @@ export const useCreateGrocery = () => {
|
||||
creatorId: currentUser?.uid
|
||||
});
|
||||
},
|
||||
onSuccess: () => {
|
||||
onSettled: () => {
|
||||
return queryClient.invalidateQueries({
|
||||
queryKey: groceriesKey,
|
||||
exact: true
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
|
||||
export const useSignUp = () => {
|
||||
return useMutation({
|
||||
mutationKey: ["getCaregivers"],
|
||||
mutationFn: async () => {
|
||||
const snapshot = await firestore()
|
||||
.collection("Profiles")
|
||||
.where("userType", "==", "caregiver")
|
||||
.get();
|
||||
},
|
||||
});
|
||||
};
|
||||
@ -1,34 +0,0 @@
|
||||
import {useQuery} from "@tanstack/react-query";
|
||||
import {ChildProfile} from "@/hooks/firebase/types/profileTypes";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
|
||||
export const useGetChildrenByParentId = () => {
|
||||
const {user} = useAuthContext()
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["getChildrenByParentId", user?.uid],
|
||||
queryFn: async (): Promise<ChildProfile[]> => {
|
||||
try {
|
||||
const snapshot = await firestore()
|
||||
.collection("Profiles")
|
||||
.where("userType", "==", "child")
|
||||
.where("parentId", "==", user?.uid!)
|
||||
.get();
|
||||
|
||||
return snapshot.docs.map((doc) => {
|
||||
const data = doc.data();
|
||||
return {
|
||||
...data,
|
||||
birthday: data.birthday.toDate(),
|
||||
} as ChildProfile;
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error retrieving child users:", error);
|
||||
return [];
|
||||
}
|
||||
},
|
||||
enabled: !!user?.uid
|
||||
}
|
||||
)
|
||||
}
|
||||
@ -127,6 +127,9 @@ export const useGetEvents = () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["events", user.uid]
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["notifications"]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,6 +27,9 @@ export const useGetGroceries = () => {
|
||||
creatorId: data.creatorId
|
||||
};
|
||||
});
|
||||
}
|
||||
},
|
||||
staleTime: Infinity,
|
||||
gcTime: Infinity,
|
||||
placeholderData: (previousData) => previousData,
|
||||
})
|
||||
};
|
||||
@ -28,5 +28,8 @@ export const useGetNotes = () => {
|
||||
}
|
||||
},
|
||||
enabled: !!currentUser?.uid,
|
||||
staleTime: Infinity,
|
||||
gcTime: Infinity,
|
||||
placeholderData: (previousData) => previousData,
|
||||
});
|
||||
};
|
||||
|
||||
@ -29,6 +29,8 @@ export interface Notification {
|
||||
export const useGetNotifications = () => {
|
||||
const { user, profileData } = useAuthContext();
|
||||
|
||||
console.log(profileData?.familyId)
|
||||
|
||||
return useQuery<Notification[], Error>({
|
||||
queryKey: ["notifications", user?.uid],
|
||||
queryFn: async () => {
|
||||
@ -44,11 +46,13 @@ export const useGetNotifications = () => {
|
||||
id: doc.id,
|
||||
...data,
|
||||
timestamp: new Date(data.timestamp.seconds * 1000 + data.timestamp.nanoseconds / 1e6),
|
||||
date: data.date ? new Date(data.date.seconds * 1000 + data.date.nanoseconds / 1e6) : undefined
|
||||
date: data.date ? new Date(data.date.seconds * 1000 + data.date.nanoseconds / 1e6) : undefined,
|
||||
};
|
||||
});
|
||||
},
|
||||
refetchOnWindowFocus: true,
|
||||
staleTime: 60000,
|
||||
staleTime: Infinity,
|
||||
gcTime: Infinity,
|
||||
placeholderData: (previousData) => previousData,
|
||||
});
|
||||
};
|
||||
@ -31,6 +31,9 @@ export const useGetTodos = () => {
|
||||
repeatDays: data.repeatDays ?? []
|
||||
};
|
||||
}) as IToDo[];
|
||||
}
|
||||
},
|
||||
staleTime: Infinity,
|
||||
gcTime: Infinity,
|
||||
placeholderData: (previousData) => previousData,
|
||||
})
|
||||
};
|
||||
@ -1,24 +0,0 @@
|
||||
import {useMutation, useQueryClient} from "@tanstack/react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {EventData} from "@/hooks/firebase/types/eventData";
|
||||
|
||||
export const useUpdateEvent = () => {
|
||||
const queryClients = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["updateEvent"],
|
||||
mutationFn: async (eventData: Partial<EventData>) => {
|
||||
try {
|
||||
await firestore()
|
||||
.collection("Events")
|
||||
.doc(`${eventData.id}`)
|
||||
.update(eventData);
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClients.invalidateQueries({queryKey: ["events"]})
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -47,7 +47,7 @@ export const useUpdateTodo = () => {
|
||||
date: data.date ? new Date(data.date.seconds * 1000) : null,
|
||||
ref: doc.ref
|
||||
};
|
||||
}) as IToDo[];
|
||||
}) as unknown as IToDo[];
|
||||
|
||||
let filteredTodos = connectedTodos?.filter((item) => compareAsc(format(item.date, 'yyyy-MM-dd'), format(todoData.date, 'yyyy-MM-dd')) === 1 ||
|
||||
compareAsc(format(item.date, 'yyyy-MM-dd'), format(todoData.date, 'yyyy-MM-dd')) === 0).sort((a,b) =>{
|
||||
|
||||
Reference in New Issue
Block a user