mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 07:07:16 +00:00
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { useAuthContext } from "@/contexts/AuthContext";
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import firestore from "@react-native-firebase/firestore";
|
|
|
|
export const useDeleteNote = () => {
|
|
const { user: currentUser } = useAuthContext();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationKey: ["deleteNote"],
|
|
mutationFn: async (noteId: string) => {
|
|
try {
|
|
const snapshot = await firestore()
|
|
.collection("BrainDumps")
|
|
.where("id", "==", noteId)
|
|
.get();
|
|
|
|
if (snapshot.empty) {
|
|
throw new Error("Note not found");
|
|
}
|
|
|
|
const docId = snapshot.docs[0].id;
|
|
|
|
const noteData = snapshot.docs[0].data();
|
|
if (noteData.creatorId !== currentUser?.uid) {
|
|
throw new Error("Unauthorized: You can only delete your own Note");
|
|
}
|
|
|
|
await firestore().collection("BrainDumps").doc(docId).delete();
|
|
} catch (e) {
|
|
console.error("Error deleting note: ", e);
|
|
throw e;
|
|
}
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries("braindumps");
|
|
},
|
|
});
|
|
};
|