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

69.23% Statements 9/13
56.25% Branches 9/16
33.33% Functions 1/3
69.23% Lines 9/13

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                                                      1x                       3x       3x               3x   3x         3x         3x   3x   3x                                                                                                                                    
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import { Stack } from "@mui/material";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import GiveButton from "@shared/Button/GiveButton";
import GiveText from "@shared/Text/GiveText";
import { isEmpty } from "lodash";
import { formatCharCountWithLimit } from "@utils/index";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
 
export type FormValues = {
  comment: string;
};
 
interface Props {
  title: string;
  description: React.ReactNode | (() => React.ReactNode) | string;
  onConfirm: SubmitHandler<FormValues>;
  confirmButtonLabel?: string;
  confirmButtonColor?: "destructive" | "primary";
  triggerHistory?: () => React.ReactNode;
  commentInputLabel?: string;
  maxLength?: number;
}
 
const COMMENT_MAX_LENGTH = 500;
 
export default function CommentModalBase({
  title,
  description,
  onConfirm,
  confirmButtonLabel = "Confirm",
  confirmButtonColor = "primary",
  triggerHistory,
  commentInputLabel = "Comment",
  maxLength = COMMENT_MAX_LENGTH,
}: Props) {
  const schema = Yup.object().shape({
    comment: Yup.string().required(`${commentInputLabel} is required`),
  });
 
  const methods = useForm<FormValues>({
    mode: "onChange",
    reValidateMode: "onChange",
    defaultValues: {
      comment: "",
    },
    resolver: yupResolver(schema),
  });
  const { open, onClose } = useNiceModal();
 
  const handleClose = () => {
    methods.reset();
    onClose();
  };
 
  const handleSubmit = (data: FormValues) => {
    onConfirm(data);
    handleClose();
  };
 
  const comment = methods.watch("comment");
 
  const isValid = isEmpty(methods.formState.errors);
 
  return (
    <GiveBaseModal
      open={open}
      title={title}
      width="560px"
      height="fit-content"
      PaperProps={{
        sx: { overflowX: "hidden" },
      }}
      onClose={handleClose}
      buttons={
        <Stack gap="12px" flexDirection="row">
          <GiveButton
            onClick={handleClose}
            label="Cancel"
            variant="ghost"
            size="large"
          />
          <GiveButton
            label={confirmButtonLabel}
            color={confirmButtonColor}
            variant="filled"
            size="large"
            sx={{
              border: "none",
              whiteSpace: "nowrap",
            }}
            data-testid={"comment-modal-confirm-button"}
            onClick={methods.handleSubmit(handleSubmit)}
            disabled={!isValid || !comment}
          />
        </Stack>
      }
    >
      <FormProvider {...methods}>
        <Stack gap="24px">
          {description && (
            <GiveText variant="bodyS" color="secondary">
              {typeof description === "function" ? description() : description}
            </GiveText>
          )}
          {triggerHistory && triggerHistory()}
          <HFGiveInput
            name="comment"
            label={commentInputLabel}
            placeholder=" " //if we use `hidePlaceholder` prop, the border color won't be correct
            multiline
            rows={6}
            fullWidth
            data-testid={`comment-modal-input`}
            inputProps={{
              ...(!!maxLength && {
                maxLength: `${maxLength}`,
              }),
            }}
            helperText={
              !!maxLength
                ? formatCharCountWithLimit(comment, maxLength)
                : undefined
            }
          />
        </Stack>
      </FormProvider>
    </GiveBaseModal>
  );
}