All files / src/features/Merchants/MerchantSidePanel/Modals DeleteMerchantModal.tsx

86.2% Statements 25/29
50% Branches 7/14
100% Functions 6/6
86.2% Lines 25/29

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                                    1x                 7x 7x 7x 7x 7x 1x           7x 1x         1x   1x 1x 1x 1x 1x 1x   1x 1x               7x                                                                                         1x   7x                       7x                   7x         1x 12x                
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import {
  QKEY_LIST_ACCOUNTS,
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_ENTERPRISE_STATS,
  QKEY_LIST_MERCHANT_STATS,
} from "@constants/queryKeys";
import NiceModal from "@ebay/nice-modal-react";
import { customInstance } from "@services/api";
import GivePopup from "@shared/Popup/GivePopup";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useMutation, useQueryClient } from "react-query";
import { showMessage } from "@common/Toast/ShowToast";
import GiveText from "@shared/Text/GiveText";
import React from "react";
import { useRefetchCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
 
type Props = {
  id: string;
  name: string;
  type: "Merchant" | "Provider";
  providerHasMerchants?: boolean;
  customCb?: () => void;
  closeSidepanel: () => void;
};
 
const DeleteMerchantModal = NiceModal.create(
  ({
    id,
    name,
    type,
    providerHasMerchants,
    customCb,
    closeSidepanel,
  }: Props) => {
    const { open, onClose } = useNiceModal();
    const { isMobileView } = useCustomThemeV2();
    const queryClient = useQueryClient();
    const { handleRefetchCounters } = useRefetchCounters();
    const { mutate, isLoading } = useMutation(() => {
      return customInstance({
        url: `/merchants/${id}`,
        method: "DELETE",
      });
    });
 
    const onDelete = () => {
      Iif (customCb) {
        customCb();
        onClose();
        return;
      }
      mutate(undefined, {
        onSuccess: () => {
          onClose();
          closeSidepanel();
          showMessage("Success", `${name} has been deleted`);
          queryClient.invalidateQueries(QKEY_LIST_MERCHANT_STATS);
          handleRefetchCounters();
          queryClient.invalidateQueries(QKEY_LIST_ACCOUNTS);
 
          if (type === "Merchant") {
            queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
          } else E{
            queryClient.invalidateQueries(QKEY_LIST_ENTERPRISE_STATS);
          }
        },
      });
    };
 
    return (
      <GivePopup
        title={`Delete ${type}`}
        description={getDescriptionText(name, !providerHasMerchants)}
        open={open}
        paperSx={{
          width: "560px",
          maxHeight: isMobileView
            ? "310px !important"
            : "calc(100% - 96px) !important",
        }}
        onClose={onClose}
        type="destructive"
        isMobile={isMobileView}
        buttons={[
          {
            label: "Cancel",
            onClick: onClose,
            variant: "ghost",
          },
          {
            label: providerHasMerchants ? "View Merchant List" : "Yes, Delete",
            onClick: onDelete,
            variant: "filled",
            submitType: "submit",
            color: providerHasMerchants ? "primary" : "destructive",
            disabled: isLoading,
          },
        ]}
        sx={{
          ...(isMobileView && {
            "& .MuiDialogActions-root": {
              display: "flex",
              flexDirection: "row",
              width: "280px",
              marginLeft: "auto",
              justifyContent: "flex-end",
            },
          }),
        }}
      />
    );
  },
);
 
const getDescriptionText = (name: string, deleteMerchantAllowed: boolean) => {
  const DELETE_MERCHANT_DESCRIPTION = (
    <GiveText
      variant="bodyS"
      color="secondary"
      data-testid="delete-merchant-modal-description"
    >
      Are you sure you want to delete{" "}
      <MerchantNameText>{name}</MerchantNameText>? This action will remove{" "}
      <MerchantNameText>{name}</MerchantNameText> from the platform, but it can
      be reversed by our backend team if necessary.
    </GiveText>
  );
  const DELETE_NOT_ALLOWED_DESCRIPTION = (
    <GiveText
      variant="bodyS"
      color="secondary"
      data-testid="delete-merchant-modal-delete-not-allowed"
    >
      Deletion not allowed: Please delete all associated merchants before
      removing <MerchantNameText>{name}</MerchantNameText>.
    </GiveText>
  );
 
  return deleteMerchantAllowed
    ? DELETE_MERCHANT_DESCRIPTION
    : DELETE_NOT_ALLOWED_DESCRIPTION;
};
 
const MerchantNameText = ({ children }: { children: React.ReactNode }) => {
  return (
    <GiveText component="span" variant="bodyS" color="primary">
      {children}
    </GiveText>
  );
};
 
export default DeleteMerchantModal;