- Replaced use of useCustomersInfiniteQuery with a new useContactsInfiniteQuery hook for improved data fetching. - Simplified ContactsList and MastersGrid components by removing unnecessary customer documentId filters. - Deleted outdated contact-related hooks and queries to streamline the codebase. - Enhanced loading state management across components for better user experience.
119 lines
2.9 KiB
TypeScript
119 lines
2.9 KiB
TypeScript
'use client';
|
|
|
|
import { getCustomer, getCustomers, updateCustomer } from '@/actions/api/customers';
|
|
import { isCustomerBanned } from '@repo/utils/customer';
|
|
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { useSession } from 'next-auth/react';
|
|
|
|
export const useCustomerQuery = (variables?: Parameters<typeof getCustomer>[0]) => {
|
|
const { data: session } = useSession();
|
|
const telegramId =
|
|
variables?.telegramId === undefined ? session?.user?.telegramId : variables?.telegramId;
|
|
|
|
return useQuery({
|
|
enabled: Boolean(telegramId),
|
|
queryFn: () => getCustomer({ telegramId }),
|
|
queryKey: ['customer', telegramId],
|
|
});
|
|
};
|
|
|
|
export const useCustomersQuery = (
|
|
variables: Parameters<typeof getCustomers>[0],
|
|
enabled?: boolean,
|
|
) =>
|
|
useQuery({
|
|
enabled,
|
|
queryFn: () => getCustomers(variables),
|
|
queryKey: ['customers', variables],
|
|
staleTime: 60 * 1_000,
|
|
});
|
|
|
|
export const useCustomersInfiniteQuery = (
|
|
variables: Omit<Parameters<typeof getCustomers>[0], 'pagination'>,
|
|
{ enabled = true, pageSize = 10 } = {},
|
|
) => {
|
|
const queryFunction = ({ pageParam: page = 1 }) =>
|
|
getCustomers({
|
|
...variables,
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
},
|
|
});
|
|
|
|
return useInfiniteQuery({
|
|
enabled,
|
|
getNextPageParam: (lastPage, _allPages, lastPageParameter) => {
|
|
if (!lastPage?.customers?.length) return undefined;
|
|
|
|
return lastPageParameter + 1;
|
|
},
|
|
initialPageParam: 1,
|
|
queryFn: queryFunction,
|
|
queryKey: ['customers', variables, 'infinite'],
|
|
staleTime: 60 * 1_000,
|
|
});
|
|
};
|
|
|
|
export const useIsBanned = () => {
|
|
const { data: { customer } = {} } = useCustomerQuery();
|
|
|
|
if (!customer) return false;
|
|
|
|
return isCustomerBanned(customer);
|
|
};
|
|
|
|
export const useCustomerMutation = () => {
|
|
const { data: session } = useSession();
|
|
const telegramId = session?.user?.telegramId;
|
|
const queryClient = useQueryClient();
|
|
|
|
const handleOnSuccess = () => {
|
|
if (!telegramId) return;
|
|
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['customer', telegramId],
|
|
});
|
|
};
|
|
|
|
return useMutation({
|
|
mutationFn: updateCustomer,
|
|
onSuccess: handleOnSuccess,
|
|
});
|
|
};
|
|
|
|
export function useContactsInfiniteQuery() {
|
|
const { data: { customer } = {}, isLoading: isLoadingCustomer } = useCustomerQuery();
|
|
|
|
const { isLoading: isLoadingContacts, ...query } = useCustomersInfiniteQuery(
|
|
{
|
|
filters: {
|
|
or: [
|
|
{
|
|
invited: {
|
|
documentId: {
|
|
contains: customer?.documentId,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
invitedBy: {
|
|
documentId: {
|
|
eq: customer?.documentId,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{ enabled: Boolean(customer?.documentId) },
|
|
);
|
|
|
|
const isLoading = isLoadingContacts || isLoadingCustomer;
|
|
|
|
return {
|
|
isLoading,
|
|
...query,
|
|
};
|
|
}
|