39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
|
|
import { useDataSource } from "@erp/core/hooks";
|
||
|
|
import {
|
||
|
|
UpdateCustomerByIdRequestDTO,
|
||
|
|
UpdateCustomerByIdResponseDTO,
|
||
|
|
} from "@erp/customer-invoices/common";
|
||
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||
|
|
import { CUSTOMER_QUERY_KEY } from "./use-customer-query";
|
||
|
|
|
||
|
|
export const CUSTOMERS_LIST_KEY = ["customers"] as const;
|
||
|
|
|
||
|
|
type MutationDeps = {};
|
||
|
|
|
||
|
|
export function useUpdateCustomerMutation(customerId: string, deps?: MutationDeps) {
|
||
|
|
const queryClient = useQueryClient();
|
||
|
|
const dataSource = useDataSource();
|
||
|
|
|
||
|
|
return useMutation<UpdateCustomerByIdResponseDTO, Error, UpdateCustomerByIdRequestDTO>({
|
||
|
|
mutationKey: ["customer:update", customerId],
|
||
|
|
mutationFn: async (input) => {
|
||
|
|
if (!customerId) throw new Error("customerId is required");
|
||
|
|
const updated = await dataSource.updateOne("customers", customerId, input);
|
||
|
|
return updated as UpdateCustomerByIdResponseDTO;
|
||
|
|
},
|
||
|
|
onSuccess: (updated) => {
|
||
|
|
// Refresca inmediatamente el detalle
|
||
|
|
queryClient.setQueryData<UpdateCustomerByIdResponseDTO>(
|
||
|
|
CUSTOMER_QUERY_KEY(customerId),
|
||
|
|
updated
|
||
|
|
);
|
||
|
|
|
||
|
|
// Otra opción es invalidar el detalle para forzar refetch:
|
||
|
|
// queryClient.invalidateQueries({ queryKey: CUSTOMER_QUERY_KEY(customerId) });
|
||
|
|
|
||
|
|
// Invalida el listado para refrescar desde servidor
|
||
|
|
queryClient.invalidateQueries({ queryKey: CUSTOMERS_LIST_KEY });
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|