mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 15:17:17 +00:00
39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { useAuthContext } from "@/contexts/AuthContext";
|
|
import firestore from "@react-native-firebase/firestore";
|
|
import { Notification } from "@/hooks/firebase/useGetNotifications";
|
|
|
|
export const useDeleteNotification = () => {
|
|
const queryClient = useQueryClient();
|
|
const { user } = useAuthContext();
|
|
const notificationsKey = ["notifications", user?.uid];
|
|
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
await firestore()
|
|
.collection("Notifications")
|
|
.doc(id)
|
|
.delete();
|
|
},
|
|
onMutate: async (deletedId) => {
|
|
await queryClient.cancelQueries({ queryKey: notificationsKey });
|
|
|
|
const previousNotifications = queryClient.getQueryData<Notification[]>(notificationsKey);
|
|
|
|
queryClient.setQueryData<Notification[]>(
|
|
notificationsKey,
|
|
old => old?.filter(notification => notification?.id !== deletedId) ?? []
|
|
);
|
|
|
|
return { previousNotifications };
|
|
},
|
|
onError: (_err, _deletedId, context) => {
|
|
if (context?.previousNotifications) {
|
|
queryClient.setQueryData(notificationsKey, context.previousNotifications);
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: notificationsKey });
|
|
}
|
|
});
|
|
}; |