All files / src/components/common/Modal/DeleteModal DeleteConfirmationModal.tsx

96% Statements 24/25
78.57% Branches 11/14
100% Functions 5/5
100% Lines 21/21

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 155 156                                                                      9x                         70x   70x       70x       70x 70x   70x   70x         70x     2x 2x     1x                   70x 70x   70x 8x 5x   3x   8x   70x                                                                                               55x                      
import { Text } from "@common/Text";
import NiceModal from "@ebay/nice-modal-react";
import { styled } from "@mui/material";
import { palette } from "@palette";
import ModalFactory from "@common/Modal/ModalFactory/ModalFactory";
import { ModalActions, ModalTitle } from "@common/Modal/ModalFactory/atoms";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { BtnBGTypes } from "@common/Button/Button_V2";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import useDeleteConfirmationModal, {
  TModalVariant,
} from "./useDeleteConfirmationModal";
import { useDeleteMutation } from "./useDeleteMutation";
import { CampaignType } from "@common/Campaigns/hooks/useReportMenuActions";
import { FormProvider, useForm } from "react-hook-form";
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
 
export type TDeleteModal = {
  variant: TModalVariant;
  deleteHandler?: () => void;
  itemName?: string;
  url?: string;
  onSuccess?: (res: any) => void;
  onError?: (error: any) => void;
  onSettled?: () => void;
  refetchQuerieKeys: string[];
  invalidateQuerieKeys: string[];
  productVariant?: CampaignType;
};
 
type TForm = {
  enterpriseName: string;
};
 
const DeleteConfirmationModal = NiceModal.create(
  ({
    variant,
    deleteHandler,
    itemName,
    url,
    onSuccess,
    onError,
    onSettled,
    refetchQuerieKeys = [],
    invalidateQuerieKeys = [],
    productVariant,
  }: TDeleteModal) => {
    const { open, onClose, TransitionProps } = useNiceModal();
 
    const schema = Yup.object({
      enterpriseName: Yup.string(),
    });
 
    const methods = useForm<TForm>({
      resolver: yupResolver(schema),
      defaultValues: { enterpriseName: "" },
    });
    const { watch } = methods;
    const values = watch();
    const isEnterpriseMatching =
      variant === "provider" && values.enterpriseName !== itemName;
 
    const { modalValues } = useDeleteConfirmationModal({
      variant,
      itemName,
      productVariant,
    });
    const { handleDelete, mutation } = useDeleteMutation({
      url: url,
      onSuccess: (res: any) => {
        Iif (onSuccess) onSuccess(res);
        Eif (modalValues.onSuccess) modalValues.onSuccess();
      },
      onError: (res: any) => {
        Eif (onError) onError(res);
      },
      onSettled,
      refetchQuerieKeys: refetchQuerieKeys.concat(
        modalValues?.refetchQuerieKeys,
      ),
      invalidateQuerieKeys: invalidateQuerieKeys.concat(
        modalValues?.invalidateQuerieKeys,
      ),
    });
    const isLoading = mutation.isLoading;
    const isDisabeled = isLoading || isEnterpriseMatching;
 
    const onClick = () => {
      if (deleteHandler) {
        deleteHandler();
      } else {
        handleDelete();
      }
      onClose();
    };
    return (
      <ModalFactory
        variant="popup"
        modalProps={{
          open,
          onClose,
          PopupProps: {
            width: "420px",
            paperStyle: { padding: "24px 24px 16px " },
            TransitionProps,
          },
          sx: { zIndex: 999999 }, // confirmation popup is always on top
        }}
      >
        <FormProvider {...methods}>
          <ModalTitle
            title={modalValues?.title}
            closeButtonSize={22}
            onClose={onClose}
          />
          <FadeUpWrapper delay={100}>
            <StyledText>{modalValues?.description}</StyledText>
          </FadeUpWrapper>
          <ModalActions
            containerProps={{ sx: { mt: "20px", gap: "16px" } }}
            primaryAction={{
              label: modalValues?.button.text,
              background: "red" as BtnBGTypes,
              type: "button",
              onClick: onClick,
              disabled: isDisabeled,
              dataTestId: "delete-action",
            }}
            secondaryAction={{
              label: "Cancel",
              onClick: onClose,
              disabled: isLoading,
              dataTestId: "cancel-action",
            }}
          />
        </FormProvider>
      </ModalFactory>
    );
  },
);
 
export default DeleteConfirmationModal;
 
const StyledText = styled(Text)(() => ({
  color: palette.neutral[70],
  fontFamily: "Give Whyte",
  fontSize: "14px",
  fontWeight: 350,
  lineHeight: "16.8px",
  padding: 0,
  marginTop: "8px",
  marginBottom: "0px",
  "& span": { color: palette.neutral[80] },
}));