All files / src/hooks/queries useWhatsAppRecipientQueries.ts

100% Statements 23/23
100% Branches 2/2
100% Functions 18/18
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                  79x   42x             12x           36x 36x   5x 3x         97x 97x             6x 4x         77x 77x 2x 2x         21x 21x   4x 2x         21x 21x 1x 1x      
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
	type CreateWhatsAppRecipientInput,
	type UpdateWhatsAppRecipientInput,
	whatsappRecipientsApi,
} from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
 
export function useWhatsAppRecipients(proId: string | null) {
	return useQuery({
		queryKey: queryKeys.pro.whatsappRecipients(proId ?? ""),
		queryFn: () => whatsappRecipientsApi.list(proId as string),
		enabled: !!proId,
		staleTime: 1000 * 30,
	});
}
 
function invalidate(queryClient: ReturnType<typeof useQueryClient>, proId: string) {
	queryClient.invalidateQueries({
		queryKey: queryKeys.pro.whatsappRecipients(proId),
	});
}
 
export function useCreateWhatsAppRecipient(proId: string) {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: (input: CreateWhatsAppRecipientInput) =>
			whatsappRecipientsApi.create(proId, input),
		onSuccess: () => invalidate(qc, proId),
	});
}
 
export function useUpdateWhatsAppRecipient(proId: string) {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: ({
			id,
			patch,
		}: {
			id: number;
			patch: UpdateWhatsAppRecipientInput;
		}) => whatsappRecipientsApi.update(proId, id, patch),
		onSuccess: () => invalidate(qc, proId),
	});
}
 
export function useDeleteWhatsAppRecipient(proId: string) {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: (id: number) => whatsappRecipientsApi.remove(proId, id),
		onSuccess: () => invalidate(qc, proId),
	});
}
 
export function useVerifyWhatsAppRecipient(proId: string) {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: ({ id, code }: { id: number; code: string }) =>
			whatsappRecipientsApi.verify(proId, id, code),
		onSuccess: () => invalidate(qc, proId),
	});
}
 
export function useResendWhatsAppRecipientOtp(proId: string) {
	const qc = useQueryClient();
	return useMutation({
		mutationFn: (id: number) => whatsappRecipientsApi.resendOtp(proId, id),
		onSuccess: () => invalidate(qc, proId),
	});
}