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

0% Statements 0/19
0% Branches 0/14
0% Functions 0/4
0% Lines 0/19

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                                                                                                                                                                                                                                                                                               
import { Box, Stack } from "@mui/material";
import { Controller, SubmitHandler, useForm } from "react-hook-form";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import NiceModal from "@ebay/nice-modal-react";
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import GiveButton from "@shared/Button/GiveButton";
import { GiveInput } from "@shared/GiveInputs/GiveInput";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveText from "@shared/Text/GiveText";
import { formatCharCountWithLimit } from "@utils/index";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import ButtonWithSpinner from "@shared/Button/ButtonWithSpinner";
 
type Props = {
  merchantName: string;
  isLoading: boolean;
  onHandleDecline: (handleClose: () => void) => SubmitHandler<ReasonType>;
  isProvider?: boolean;
};
 
export type ReasonType = {
  reason: string;
};
 
const MAX_TEXT_LENGTH = 500;
 
const schema = Yup.object().shape({
  reason: Yup.string()
    .required("Reason is required")
    .min(1, "Reason cannot be empty"),
});
 
const DeclineMerchantModal = NiceModal.create(
  ({ merchantName, onHandleDecline, isLoading, isProvider }: Props) => {
    const { isNewApprovalFlowEnabled, isNewConversationsEnabled } =
      useGetFeatureFlagValues();
    const { open, onClose } = useNiceModal();
 
    const methods = useForm<ReasonType>({
      mode: "onChange",
      reValidateMode: "onChange",
      defaultValues: {
        reason: "",
      },
      resolver: yupResolver(schema),
    });
 
    const handleClose = () => {
      methods.reset();
      onClose();
    };
 
    const onDecline = onHandleDecline(handleClose);
 
    const isValid = methods.formState.isValid;
 
    const getInputLabel = () => {
      if (isNewConversationsEnabled) {
        return "Comment";
      }
      if (isNewApprovalFlowEnabled) {
        return "Reason";
      }
      return "Reason (required)";
    };
 
    return (
      <GiveBaseModal
        open={open}
        title={`Decline ${isProvider ? "Provider" : "Merchant"}`}
        width="560px"
        height="57%"
        onClose={handleClose}
        buttons={
          <Stack gap="12px" flexDirection="row">
            <GiveButton
              onClick={handleClose}
              label="Cancel"
              variant="ghost"
              size="large"
            />
            <ButtonWithSpinner isLoading={isLoading}>
              <GiveButton
                label="Decline"
                color="destructive"
                variant="filled"
                size="large"
                sx={{
                  border: "none",
                }}
                onClick={methods.handleSubmit(onDecline)}
                disabled={!isValid || isLoading}
              />
            </ButtonWithSpinner>
          </Stack>
        }
      >
        <Box>
          <Controller
            control={methods.control}
            name="reason"
            render={({ field: { onChange, value }, fieldState: { error } }) => (
              <>
                <GiveText
                  mb="24px"
                  color="secondary"
                  fontSize="14px"
                  lineHeight="20px"
                >
                  Are you sure you want to decline{" "}
                  <strong>{merchantName}</strong>? This action will result in
                  the rejection of the{" "}
                  {isProvider ? "provider's" : "merchant's"} application.
                </GiveText>
 
                <GiveInput
                  label={getInputLabel()}
                  name="reason"
                  multiline
                  rows={8}
                  maxLength={
                    isNewApprovalFlowEnabled ? MAX_TEXT_LENGTH : undefined
                  }
                  value={value}
                  onChange={onChange}
                  error={!!error}
                />
                {isNewApprovalFlowEnabled && (
                  <GiveText pt="8px" variant="bodyXS" color="secondary">
                    {formatCharCountWithLimit(value, MAX_TEXT_LENGTH)}
                  </GiveText>
                )}
              </>
            )}
          />
        </Box>
      </GiveBaseModal>
    );
  },
);
 
export default DeclineMerchantModal;