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

0% Statements 0/17
0% Branches 0/10
0% Functions 0/4
0% Lines 0/17

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                                                                                                                                                                                                                                                                                         
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import NiceModal from "@ebay/nice-modal-react";
import { yupResolver } from "@hookform/resolvers/yup";
import { Box, Stack } from "@mui/material";
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 { capitalizeFirstLetter, formatCharCountWithLimit } from "@utils/index";
import { Controller, useForm } from "react-hook-form";
import * as Yup from "yup";
import { useUpdateMerchantStatus } from "../ApprovalFlowPanel/hooks/useUpdateMerchantStatus";
 
type Props = {
  merchantId: number;
  merchantName: string;
  status: "KYB" | "Underwriting" | "pending";
  handleSuccess?: () => void;
};
 
type ReasonType = {
  reason: string;
};
 
const schema = Yup.object().shape({
  reason: Yup.string()
    .transform((value) => (typeof value === "string" ? value.trim() : value))
    .required("Reason is required")
    .min(1, "Reason cannot be empty"),
});
 
const MAX_TEXT_LENGTH = 500;
 
const UpdateMerchantStatusModal = NiceModal.create(
  ({ merchantName, status, merchantId, handleSuccess }: Props) => {
    const { open, onClose } = useNiceModal();
    const {
      handleBackToKYB,
      handleBackToUnderwriting,
      handleBackToPending,
      isLoading,
    } = useUpdateMerchantStatus({ merchantId });
 
    const methods = useForm<ReasonType>({
      mode: "onBlur",
      reValidateMode: "onBlur",
      defaultValues: {
        reason: "",
      },
      resolver: yupResolver(schema),
    });
 
    const handleClose = () => {
      handleSuccess?.();
      methods.reset();
      onClose();
    };
 
    let onMoveToSection =
      status == "KYB"
        ? handleBackToKYB(handleClose)
        : handleBackToUnderwriting(handleClose);
 
    if (status === "pending") {
      onMoveToSection = handleBackToPending(handleClose);
    }
 
    const isValid = methods.formState.isValid;
 
    return (
      <GiveBaseModal
        open={open}
        title={`Move to ${
          status == "KYB" ? "KYB" : capitalizeFirstLetter(status)
        }`}
        width="560px"
        height="fit-content"
        onClose={handleClose}
        buttons={
          <Stack gap="12px" flexDirection="row">
            <GiveButton
              onClick={handleClose}
              label="Cancel"
              variant="ghost"
              size="large"
              disabled={isLoading}
            />
            <GiveButton
              label="Confirm"
              color="primary"
              variant="filled"
              size="large"
              sx={{
                border: "none",
              }}
              onClick={methods.handleSubmit(onMoveToSection)}
              disabled={!isValid || isLoading}
            />
          </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 move <strong>{merchantName}</strong>{" "}
                  back to {status}?
                </GiveText>
 
                <GiveInput
                  label="Reason"
                  name="reason"
                  multiline
                  rows={8}
                  maxLength={MAX_TEXT_LENGTH}
                  value={value}
                  onChange={onChange}
                  error={!!error}
                />
                <GiveText pt="8px" variant="bodyXS" color="secondary">
                  {formatCharCountWithLimit(value, MAX_TEXT_LENGTH)}
                </GiveText>
              </>
            )}
          />
        </Box>
      </GiveBaseModal>
    );
  },
);
 
export default UpdateMerchantStatusModal;