diff --git a/apps/web/hooks/api/contacts/query.ts b/apps/web/hooks/api/contacts/query.ts index f28aa61..935500d 100644 --- a/apps/web/hooks/api/contacts/query.ts +++ b/apps/web/hooks/api/contacts/query.ts @@ -9,7 +9,7 @@ export const useClientsQuery = (props?: Parameters[0] return useQuery({ enabled: false, queryFn: () => getCustomerClients({ telegramId }), - queryKey: ['customer', 'telegramId', telegramId, 'clients', 'get'], + queryKey: ['customer', 'telegramId', telegramId, 'clients'], }); }; @@ -20,6 +20,6 @@ export const useMastersQuery = (props?: Parameters[0] return useQuery({ enabled: false, queryFn: () => getCustomerMasters({ telegramId }), - queryKey: ['customer', 'telegramId', telegramId, 'masters', 'get'], + queryKey: ['customer', 'telegramId', telegramId, 'masters'], }); }; diff --git a/apps/web/hooks/api/customers.ts b/apps/web/hooks/api/customers.ts index 527d598..5c69a04 100644 --- a/apps/web/hooks/api/customers.ts +++ b/apps/web/hooks/api/customers.ts @@ -8,26 +8,27 @@ export const useCustomerQuery = (props?: Parameters[0]) => { const telegramId = props?.telegramId || session?.user?.telegramId; return useQuery({ + enabled: Boolean(telegramId), queryFn: () => getCustomer({ telegramId }), - queryKey: ['customer', 'telegramId', telegramId, 'get'], + queryKey: ['customer', telegramId], }); }; export const useCustomerMutation = () => { const { data: session } = useSession(); const telegramId = session?.user?.telegramId; - const queryClient = useQueryClient(); - function handleOnSuccess() { + const handleOnSuccess = () => { + if (!telegramId) return; + queryClient.invalidateQueries({ - queryKey: ['customer', 'telegramId', telegramId, 'get'], + queryKey: ['customer', telegramId], }); - } + }; return useMutation({ mutationFn: updateCustomer, - mutationKey: ['customer', 'update'], onSuccess: handleOnSuccess, }); }; diff --git a/apps/web/hooks/api/services.ts b/apps/web/hooks/api/services.ts index 8af6c6d..062ee76 100644 --- a/apps/web/hooks/api/services.ts +++ b/apps/web/hooks/api/services.ts @@ -3,16 +3,15 @@ import { getService, getServices } from '@/actions/api/services'; import { useQuery } from '@tanstack/react-query'; export const useServicesQuery = (input: Parameters[0]) => { - const masterId = input.filters?.master?.documentId?.eq; - return useQuery({ queryFn: () => getServices(input), - queryKey: ['services', 'master', masterId, 'list'], + queryKey: ['services', input], }); }; -export const useServiceQuery = (input: Parameters[0]) => - useQuery({ +export const useServiceQuery = (input: Parameters[0]) => { + return useQuery({ queryFn: () => getService(input), - queryKey: ['services', 'documentId', input.documentId], + queryKey: ['service', input.documentId], }); +}; diff --git a/apps/web/hooks/api/slots.ts b/apps/web/hooks/api/slots.ts index 67066a3..664b120 100644 --- a/apps/web/hooks/api/slots.ts +++ b/apps/web/hooks/api/slots.ts @@ -11,7 +11,7 @@ export const useSlots = (variables: Parameters[0]) => { return useQuery({ queryFn: () => getSlots(variables), - queryKey: ['slots', 'master', masterId, 'list', date.toISOString()], + queryKey: ['slots', { date: date?.toISOString(), masterId }], }); }; @@ -20,7 +20,7 @@ export const useSlotQuery = (variables: Parameters[0]) => { return useQuery({ queryFn: () => getSlot(variables), - queryKey: ['slots', 'get', documentId], + queryKey: ['slot', documentId], }); }; @@ -29,17 +29,15 @@ export const useSlotMutation = ({ }: Pick[0], 'documentId'>) => { const queryClient = useQueryClient(); - function handleOnSuccess() { - queryClient.invalidateQueries({ - queryKey: ['slots', 'get', documentId], - }); - } - return useMutation({ mutationFn: ({ data }: Pick[0], 'data'>) => updateSlot({ data, documentId }), - mutationKey: ['slots', 'update', documentId], - onSuccess: handleOnSuccess, + mutationKey: ['slot', 'update', documentId], + onSuccess: () => { + if (documentId) { + queryClient.invalidateQueries({ queryKey: ['slot', documentId] }); + } + }, }); }; @@ -49,17 +47,17 @@ export const useSlotCreate = ({ date }: { date: Date }) => { const queryClient = useQueryClient(); - function handleOnSuccess() { - queryClient.invalidateQueries({ - queryKey: ['slots', 'master', masterId, 'list', date.toISOString()], - }); - } - return useMutation({ mutationFn: ({ input }: { input: Omit[0]['input'], 'date'> }) => createSlot({ input: { ...input, date } }), - mutationKey: ['slots', 'create', 'date', date.toISOString(), 'master', masterId], - onSuccess: handleOnSuccess, + mutationKey: ['slot', 'create'], + onSuccess: () => { + if (masterId && date) { + queryClient.invalidateQueries({ + queryKey: ['slots', { date: date?.toISOString(), masterId }], + }); + } + }, }); }; @@ -68,18 +66,18 @@ export const useSlotDelete = ({ documentId }: Parameters[0]) const queryClient = useQueryClient(); - function handleOnSuccess() { - const date = slot?.date; - const masterId = slot?.master; - - queryClient.invalidateQueries({ - queryKey: ['slots', 'master', masterId, 'list', date.toISOString()], - }); - } - return useMutation({ mutationFn: () => deleteSlot({ documentId }), - mutationKey: ['slots', 'delete', documentId], - onSuccess: handleOnSuccess, + mutationKey: ['slot', 'delete', documentId], + onSuccess: () => { + const date = slot?.date; + const masterId = slot?.master; + + if (date && masterId) { + queryClient.invalidateQueries({ + queryKey: ['slots', { date: date?.toISOString(), masterId }], + }); + } + }, }); };