optimize hooks queryKey

This commit is contained in:
vchikalkin 2025-05-16 17:25:51 +03:00
parent 39498dbcff
commit f77e21b815
4 changed files with 41 additions and 43 deletions

View File

@ -9,7 +9,7 @@ export const useClientsQuery = (props?: Parameters<typeof getCustomerClients>[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<typeof getCustomerMasters>[0]
return useQuery({
enabled: false,
queryFn: () => getCustomerMasters({ telegramId }),
queryKey: ['customer', 'telegramId', telegramId, 'masters', 'get'],
queryKey: ['customer', 'telegramId', telegramId, 'masters'],
});
};

View File

@ -8,26 +8,27 @@ export const useCustomerQuery = (props?: Parameters<typeof getCustomer>[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,
});
};

View File

@ -3,16 +3,15 @@ import { getService, getServices } from '@/actions/api/services';
import { useQuery } from '@tanstack/react-query';
export const useServicesQuery = (input: Parameters<typeof getServices>[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<typeof getService>[0]) =>
useQuery({
export const useServiceQuery = (input: Parameters<typeof getService>[0]) => {
return useQuery({
queryFn: () => getService(input),
queryKey: ['services', 'documentId', input.documentId],
queryKey: ['service', input.documentId],
});
};

View File

@ -11,7 +11,7 @@ export const useSlots = (variables: Parameters<typeof getSlots>[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<typeof getSlot>[0]) => {
return useQuery({
queryFn: () => getSlot(variables),
queryKey: ['slots', 'get', documentId],
queryKey: ['slot', documentId],
});
};
@ -29,17 +29,15 @@ export const useSlotMutation = ({
}: Pick<Parameters<typeof updateSlot>[0], 'documentId'>) => {
const queryClient = useQueryClient();
function handleOnSuccess() {
queryClient.invalidateQueries({
queryKey: ['slots', 'get', documentId],
});
}
return useMutation({
mutationFn: ({ data }: Pick<Parameters<typeof updateSlot>[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<Parameters<typeof createSlot>[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<typeof deleteSlot>[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 }],
});
}
},
});
};