FE-003: Extended partners feature with: - addresses API (CRUD endpoints) - contacts API (CRUD endpoints) - bankAccounts API (CRUD endpoints) - AddressForm, AddressList, ContactForm, ContactList components - usePartnerAddresses, usePartnerContacts, usePartnerBankAccounts hooks FE-004: Extended companies feature with: - settings endpoints (GET/PUT company settings) - branches endpoints (CRUD branches) - CompanySettingsForm, BranchesList components - useCompanySettings, useCompanyBranches hooks Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
99 lines
3.5 KiB
TypeScript
99 lines
3.5 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { bankAccountsApi } from '../api/bankAccounts.api';
|
|
import type {
|
|
CreatePartnerBankAccountDto,
|
|
UpdatePartnerBankAccountDto,
|
|
} from '../types';
|
|
|
|
const QUERY_KEY = 'partner-bank-accounts';
|
|
|
|
// ==================== Bank Accounts List Hook ====================
|
|
|
|
export function usePartnerBankAccounts(partnerId: string | null | undefined) {
|
|
return useQuery({
|
|
queryKey: [QUERY_KEY, partnerId],
|
|
queryFn: () => bankAccountsApi.getByPartnerId(partnerId as string),
|
|
enabled: !!partnerId,
|
|
staleTime: 1000 * 60 * 5, // 5 minutes
|
|
});
|
|
}
|
|
|
|
// ==================== Bank Account Mutations Hook ====================
|
|
|
|
export function usePartnerBankAccountMutations(partnerId: string) {
|
|
const queryClient = useQueryClient();
|
|
|
|
const createMutation = useMutation({
|
|
mutationFn: (data: CreatePartnerBankAccountDto) => bankAccountsApi.create(partnerId, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, partnerId] });
|
|
},
|
|
});
|
|
|
|
const updateMutation = useMutation({
|
|
mutationFn: ({ accountId, data }: { accountId: string; data: UpdatePartnerBankAccountDto }) =>
|
|
bankAccountsApi.update(partnerId, accountId, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, partnerId] });
|
|
},
|
|
});
|
|
|
|
const deleteMutation = useMutation({
|
|
mutationFn: (accountId: string) => bankAccountsApi.delete(partnerId, accountId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, partnerId] });
|
|
},
|
|
});
|
|
|
|
const verifyMutation = useMutation({
|
|
mutationFn: (accountId: string) => bankAccountsApi.verify(partnerId, accountId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, partnerId] });
|
|
},
|
|
});
|
|
|
|
const setDefaultMutation = useMutation({
|
|
mutationFn: (accountId: string) => bankAccountsApi.setDefault(partnerId, accountId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: [QUERY_KEY, partnerId] });
|
|
},
|
|
});
|
|
|
|
return {
|
|
create: createMutation.mutateAsync,
|
|
update: updateMutation.mutateAsync,
|
|
delete: deleteMutation.mutateAsync,
|
|
verify: verifyMutation.mutateAsync,
|
|
setDefault: setDefaultMutation.mutateAsync,
|
|
isCreating: createMutation.isPending,
|
|
isUpdating: updateMutation.isPending,
|
|
isDeleting: deleteMutation.isPending,
|
|
isVerifying: verifyMutation.isPending,
|
|
isSettingDefault: setDefaultMutation.isPending,
|
|
createError: createMutation.error,
|
|
updateError: updateMutation.error,
|
|
deleteError: deleteMutation.error,
|
|
};
|
|
}
|
|
|
|
// ==================== Combined Hook ====================
|
|
|
|
export function usePartnerBankAccountsWithMutations(partnerId: string | null | undefined) {
|
|
const { data, isLoading, error, refetch } = usePartnerBankAccounts(partnerId);
|
|
const mutations = usePartnerBankAccountMutations(partnerId || '');
|
|
|
|
return {
|
|
bankAccounts: data || [],
|
|
isLoading,
|
|
error,
|
|
refetch,
|
|
...mutations,
|
|
// Disable mutations if no partnerId
|
|
create: partnerId ? mutations.create : async () => { throw new Error('Partner ID required'); },
|
|
update: partnerId ? mutations.update : async () => { throw new Error('Partner ID required'); },
|
|
delete: partnerId ? mutations.delete : async () => { throw new Error('Partner ID required'); },
|
|
verify: partnerId ? mutations.verify : async () => { throw new Error('Partner ID required'); },
|
|
setDefault: partnerId ? mutations.setDefault : async () => { throw new Error('Partner ID required'); },
|
|
};
|
|
}
|