* chore(docker): add healthcheck to service in docker-compose.yml and update deploy workflow to include docker compose down * refactor(orders): add useOrdersInfiniteQuery for improved pagination and add load more button in orders list components * refactor(graphql): remove NotifyService and related notification logic from orders and API, clean up unused dependencies * refactor(api): streamline customer, order, service, and slot actions by wrapping server functions with client action utility to rethrow error messages to client
90 lines
2.3 KiB
TypeScript
90 lines
2.3 KiB
TypeScript
/* eslint-disable unicorn/prevent-abbreviations */
|
|
'use client';
|
|
|
|
import { createOrder, getOrder, getOrders, updateOrder } from '@/actions/api/orders';
|
|
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
|
|
export const useOrderQuery = ({ documentId }: Parameters<typeof getOrder>[0]) =>
|
|
useQuery({
|
|
queryFn: () => getOrder({ documentId }),
|
|
queryKey: ['order', documentId],
|
|
});
|
|
|
|
export const useOrderCreate = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: createOrder,
|
|
mutationKey: ['order', 'create'],
|
|
onSuccess: (data) => {
|
|
queryClient.invalidateQueries({ queryKey: ['orders'] });
|
|
|
|
const documentId = data?.createOrder?.documentId;
|
|
|
|
if (documentId)
|
|
queryClient.prefetchQuery({
|
|
queryFn: () => getOrder({ documentId }),
|
|
queryKey: ['order', documentId],
|
|
});
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useOrdersQuery = (variables: Parameters<typeof getOrders>[0], enabled?: boolean) =>
|
|
useQuery({
|
|
enabled,
|
|
queryFn: () => getOrders(variables),
|
|
queryKey: ['orders', variables],
|
|
staleTime: 60 * 1_000,
|
|
});
|
|
|
|
export const useOrdersInfiniteQuery = (
|
|
variables: Omit<Parameters<typeof getOrders>[0], 'pagination'>,
|
|
{ enabled = true, pageSize = 5 },
|
|
) => {
|
|
const queryFn = ({ pageParam: page = 1 }) =>
|
|
getOrders({
|
|
...variables,
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
},
|
|
});
|
|
|
|
return useInfiniteQuery({
|
|
enabled,
|
|
getNextPageParam: (lastPage, _allPages, lastPageParameter) => {
|
|
if (!lastPage?.orders?.length) return undefined;
|
|
|
|
return lastPageParameter + 1;
|
|
},
|
|
initialPageParam: 1,
|
|
queryFn,
|
|
queryKey: ['orders', variables, 'infinite'],
|
|
staleTime: 60 * 1_000,
|
|
});
|
|
};
|
|
|
|
export const useOrderMutation = ({
|
|
documentId,
|
|
}: Pick<Parameters<typeof updateOrder>[0], 'documentId'>) => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ data }: Pick<Parameters<typeof updateOrder>[0], 'data'>) =>
|
|
updateOrder({ data, documentId }),
|
|
mutationKey: ['order', 'update', documentId],
|
|
onSuccess: () => {
|
|
if (documentId) {
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['order', documentId],
|
|
});
|
|
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['orders'],
|
|
});
|
|
}
|
|
},
|
|
});
|
|
};
|