mirror of
https://github.com/urosran/cally.git
synced 2025-07-10 15:17:17 +00:00
added Feedback page, added braindump backend
This commit is contained in:
65
hooks/firebase/useUpdateNote.ts
Normal file
65
hooks/firebase/useUpdateNote.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import { useMutation, useQueryClient } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import { IBrainDump } from "@/contexts/DumpContext";
|
||||
|
||||
interface UpdateNoteParams {
|
||||
id: number;
|
||||
changes: Partial<IBrainDump>;
|
||||
}
|
||||
|
||||
export const useUpdateNote = () => {
|
||||
const { user: currentUser } = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["updateNote"],
|
||||
mutationFn: async ({ id, changes }: UpdateNoteParams) => {
|
||||
try {
|
||||
const snapshot = await firestore()
|
||||
.collection("BrainDumps")
|
||||
.where("id", "==", id)
|
||||
.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 update your own note"
|
||||
);
|
||||
}
|
||||
|
||||
await firestore()
|
||||
.collection("BrainDumps")
|
||||
.doc(docId)
|
||||
.update({
|
||||
...changes,
|
||||
updatedAt: firestore.FieldValue.serverTimestamp(),
|
||||
lastModifiedBy: currentUser?.uid,
|
||||
});
|
||||
|
||||
return {
|
||||
id,
|
||||
...noteData,
|
||||
...changes,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error updating note: ", e);
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
onSuccess: (updatedNote) => {
|
||||
queryClient.invalidateQueries("braindumps");
|
||||
|
||||
queryClient.setQueryData(
|
||||
["feedback", updatedNote.id],
|
||||
updatedNote
|
||||
);
|
||||
},
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user