import { useMutation, useQueryClient, type UseMutationResult } from "@tanstack/react-query"; import { toast } from "react-toastify"; import { locationsApi } from "@/domain/locations/api/locations-api"; import type { LocationFormValues } from "@/domain/locations/schemas/location-schema"; import { queryKeys } from "@/infra/query-key/query-key"; interface UpdateLocationInput { id: string | number; values: LocationFormValues; } /** Saves a site. The response carries no site, so the list and detail are refetched. */ export function useUpdateLocation(): UseMutationResult { const queryClient = useQueryClient(); return useMutation({ mutationFn: async ({ id, values }: UpdateLocationInput) => { await locationsApi.update(id, values); }, onSuccess: async (_data, variables) => { await Promise.all([ queryClient.invalidateQueries({ queryKey: queryKeys.locations.all }), queryClient.invalidateQueries({ queryKey: queryKeys.locations.detail(String(variables.id)), }), ]); toast.success("Site updated"); }, onError: (error: Error) => { toast.error(error.message || "Failed to update site"); }, }); }