shoc-frontend-new/src/domain/locations/use-cases/use-update-location.ts
Alexandre Brandizzi a75b9c8be6 feat(sites): Add Site modal and site detail panel with in-place edit
- Sites page: Add Site modal with Site Code, Client, address, State/Area, optional Site Phone and Email, points of contact
- Duplicate Site Code shows inline under the field and keeps the modal open
- Row opens a non-modal detail panel; Street Address links to Google Maps; key-less map preview
- Edit switches the panel in place with Site Code read-only; Save needs a valid change, Cancel makes no call
- Site Phone is sent independently of the contacts; zip and title are carried through edits
- Retire the separate new/edit location pages
2026-09-25 11:43:30 -03:00

33 lines
1.2 KiB
TypeScript

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<void, Error, UpdateLocationInput> {
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");
},
});
}