import {useMutation, useQueryClient} from "react-query";
import firestore from "@react-native-firebase/firestore";
import {IGrocery} from "@/hooks/firebase/types/groceryData";

export const useUpdateGrocery = () => {
    const queryClients = useQueryClient()

    return useMutation({
        mutationKey: ["updateGrocery"],
        mutationFn: async (groceryData: Partial<IGrocery>) => {
            try {
                await firestore()
                    .collection("Groceries")
                    .doc(groceryData.id)
                    .update(groceryData);
            } catch (e) {
                console.error(e)
            }
        },
        onSuccess: () => {
            queryClients.invalidateQueries("groceries")
        }
    })
}