All files / src/hooks/merchant-api/customers useDeleteCustomer.tsx

5.88% Statements 1/17
0% Branches 0/6
0% Functions 0/5
6.25% Lines 1/16

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                        1x                                                                                    
import { useQueryClient, useMutation } from "react-query";
import { AxiosError } from "axios";
import { customInstance } from "@services/api";
import { showMessage } from "@common/Toast";
import { useGetCurrentMerchantId } from "@hooks/common";
 
type useDeleteCustomerProps = {
  id?: number;
  name: string;
  deleteAction: () => void;
};
 
export const useDeleteCustomer = ({
  id,
  name,
  deleteAction,
}: useDeleteCustomerProps) => {
  const { merchantId } = useGetCurrentMerchantId();
  const queryclient = useQueryClient();
 
  const deleteCustomerMutation = useMutation(() => {
    return customInstance({
      url: `/merchants/${merchantId}/customers/${id}`,
      method: "DELETE",
    });
  });
 
  const { isLoading, isSuccess } = deleteCustomerMutation;
 
  const deleteHandler = async () => {
    if (!id || !merchantId) return;
 
    deleteCustomerMutation.mutate(undefined, {
      onError: (error: unknown) => {
        const axiosError = error as AxiosError;
        const errorMessage = axiosError.response;
        console.log("errorMessage = ", errorMessage);
      },
      onSuccess: async (res: any) => {
        showMessage(
          "Success",
          <>
            Customer <span style={{ fontWeight: 600 }}>“{name}”</span> has been
            deleted successfully
          </>,
        );
        deleteAction();
        queryclient.invalidateQueries("list-all-customers");
      },
    });
  };
 
  return { deleteHandler, isDisabled: isLoading || isSuccess };
};