mirror of
https://github.com/urosran/cally.git
synced 2025-07-15 01:35:22 +00:00
Fixes
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
import {useMutation, useQueryClient} from "@tanstack/react-query";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
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";
|
||||
import { Notification } from "@/hooks/firebase/useGetNotifications";
|
||||
|
||||
export const useDeleteNotification = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const {user} = useAuthContext();
|
||||
const { user } = useAuthContext();
|
||||
const notificationsKey = ["notifications", user?.uid];
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
@ -15,23 +16,24 @@ export const useDeleteNotification = () => {
|
||||
.delete();
|
||||
},
|
||||
onMutate: async (deletedId) => {
|
||||
await queryClient.cancelQueries(["notifications", user?.uid]);
|
||||
await queryClient.cancelQueries({ queryKey: notificationsKey });
|
||||
|
||||
const previousNotifications = queryClient.getQueryData<Notification[]>(["notifications", user?.uid]);
|
||||
const previousNotifications = queryClient.getQueryData<Notification[]>(notificationsKey);
|
||||
|
||||
queryClient.setQueryData<Notification[]>(["notifications", user?.uid], (old) =>
|
||||
old?.filter((notification) => notification?.id! !== deletedId) ?? []
|
||||
queryClient.setQueryData<Notification[]>(
|
||||
notificationsKey,
|
||||
old => old?.filter(notification => notification?.id !== deletedId) ?? []
|
||||
);
|
||||
|
||||
return {previousNotifications};
|
||||
return { previousNotifications };
|
||||
},
|
||||
onError: (_err, _deletedId, context) => {
|
||||
if (context?.previousNotifications) {
|
||||
queryClient.setQueryData(["notifications", user?.uid], context.previousNotifications);
|
||||
queryClient.setQueryData(notificationsKey, context.previousNotifications);
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries(["notifications", user?.uid]);
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: notificationsKey });
|
||||
}
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user