36 lines
991 B
TypeScript
36 lines
991 B
TypeScript
'use client';
|
|
|
|
import { getCustomer, updateCustomer } from '@/actions/api/customers';
|
|
import { 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 || session?.user?.telegramId;
|
|
|
|
return useQuery({
|
|
enabled: Boolean(telegramId),
|
|
queryFn: () => getCustomer({ telegramId }),
|
|
queryKey: ['customer', telegramId],
|
|
});
|
|
};
|
|
|
|
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,
|
|
});
|
|
};
|