57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
'use client';
|
|
|
|
import { createService, getService, getServices, updateService } from '@/actions/api/services';
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
|
|
export const useServicesQuery = (variables: Parameters<typeof getServices>[0]) => {
|
|
return useQuery({
|
|
queryFn: () => getServices(variables),
|
|
queryKey: ['services', variables],
|
|
});
|
|
};
|
|
|
|
export const useServiceQuery = (variables: Parameters<typeof getService>[0]) => {
|
|
return useQuery({
|
|
queryFn: () => getService(variables),
|
|
queryKey: ['service', variables.documentId],
|
|
});
|
|
};
|
|
|
|
export const useServiceCreate = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: createService,
|
|
mutationKey: ['service', 'create'],
|
|
onSuccess: (data) => {
|
|
queryClient.invalidateQueries({ queryKey: ['services'] });
|
|
|
|
const documentId = data?.createService?.documentId;
|
|
|
|
if (documentId) {
|
|
queryClient.prefetchQuery({
|
|
queryFn: () => getService({ documentId }),
|
|
queryKey: ['service', documentId],
|
|
});
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useServiceMutation = ({
|
|
documentId,
|
|
}: Pick<Parameters<typeof updateService>[0], 'documentId'>) => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ data }: Pick<Parameters<typeof updateService>[0], 'data'>) =>
|
|
updateService({ data, documentId }),
|
|
mutationKey: ['service', 'update', documentId],
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['service', documentId] });
|
|
|
|
queryClient.invalidateQueries({ queryKey: ['services'] });
|
|
},
|
|
});
|
|
};
|