/** * Company Mutation Hooks * * Provides mutation hooks for company CRUD operations using TanStack Query. * Replaces CompanyRepository + UseCases pattern with automatic cache invalidation. * * @layer Presentation - Hooks */ import { useMutation, useQueryClient } from '@tanstack/react-query'; import { companiesApi } from '@/infrastructure/http/api/company'; import { queryKeys } from '@/lib/query-keys'; import { Company } from '@/domain/entities/Company'; /** * useCreateCompany * * Mutation hook for creating a new company. * Automatically invalidates companies list cache on success. * * @returns Mutation result with mutate function and loading/error states * * @example * ```typescript * function CreateCompanyForm() { * const { mutate: createCompany, isPending, error } = useCreateCompany(); * * const handleSubmit = (data: Partial) => { * const company = Company.create({ ...data, id: crypto.randomUUID() }); * createCompany(company, { * onSuccess: (created) => { * console.log('Company created:', created.id); * navigate(`/companies/${created.id}`); * } * }); * }; * * return
...
; * } * ``` */ export function useCreateCompany() { const queryClient = useQueryClient(); return useMutation({ mutationFn: async (company: Company) => { const created = await companiesApi.createCompany(company); return created; }, onSuccess: () => { // Invalidate all company lists to refetch with new company queryClient.invalidateQueries({ queryKey: queryKeys.companies.lists() }); }, }); } /** * useUpdateCompany * * Mutation hook for updating an existing company. * Automatically invalidates both detail and list caches on success. * * @returns Mutation result with mutate function and loading/error states * * @example * ```typescript * function EditCompanyForm({ companyId }: { companyId: string }) { * const { data: company } = useCompany(companyId); * const { mutate: updateCompany, isPending } = useUpdateCompany(); * * const handleSubmit = (updates: Partial>) => { * updateCompany({ id: companyId, updates }, { * onSuccess: (updated) => { * console.log('Company updated:', updated.name); * } * }); * }; * * return
...
; * } * ``` */ export function useUpdateCompany() { const queryClient = useQueryClient(); return useMutation({ mutationFn: async (payload: { id: string; updates: Partial>; }) => { const { id, updates } = payload; const updated = await companiesApi.updateCompany(id, updates); return updated; }, onSuccess: (updatedCompany) => { // Invalidate specific company detail cache queryClient.invalidateQueries({ queryKey: queryKeys.companies.detail(updatedCompany.id), }); // Invalidate all company lists to show updated data queryClient.invalidateQueries({ queryKey: queryKeys.companies.lists() }); }, }); } /** * useDeleteCompany * * Mutation hook for deleting a company. * Automatically invalidates both detail and list caches on success. * * @returns Mutation result with mutate function and loading/error states * * @example * ```typescript * function DeleteCompanyButton({ companyId }: { companyId: string }) { * const { mutate: deleteCompany, isPending } = useDeleteCompany(); * * const handleDelete = () => { * if (confirm('Delete this company? This action is irreversible.')) { * deleteCompany(companyId, { * onSuccess: () => { * console.log('Company deleted'); * navigate('/companies'); * } * }); * } * }; * * return ; * } * ``` * * @warning * This operation is irreversible. All company data will be permanently deleted. */ export function useDeleteCompany() { const queryClient = useQueryClient(); return useMutation({ mutationFn: async (companyId: string) => { await companiesApi.deleteCompany(companyId); return companyId; }, onSuccess: (deletedId) => { // Invalidate specific company detail cache queryClient.invalidateQueries({ queryKey: queryKeys.companies.detail(deletedId), }); // Invalidate all company lists to remove deleted company queryClient.invalidateQueries({ queryKey: queryKeys.companies.lists() }); }, }); }