All files / src/components/Merchants/MerchantPreview/modals/GiveRiskTriggerModal ModalContent.tsx

100% Statements 16/16
80% Branches 8/10
100% Functions 9/9
100% Lines 16/16

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                                                                              14x 14x   14x                                                 1x       14x     42x                 10x 10x 10x   10x           11x     1x       22x                                                                               2x 4x     20x                      
import {
  MerchantInfoContent,
  NotesContent,
} from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/Sections";
import TaskCard from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/TaskCard";
import { Box, FormControlLabel, RadioGroup, Stack } from "@mui/material";
import { useRef, useState } from "react";
import { Controller, useFormContext } from "react-hook-form";
import GiveRadio from "@shared/Radio/GiveRadio";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import GiveText from "@shared/Text/GiveText";
import { IReason } from "../../hooks/useGetManageRiskReasons";
import { isOtherReason } from "../../RiskProfile/utils";
import Skeleton from "@components/animation/Skeleton";
 
interface Props {
  merchantInfo: {
    name: string;
    legalName: string;
    taxID: string;
    imageURL?: string;
    hasUnreadMessages?: boolean;
    didMerchantReply?: boolean;
    merchantID: number;
    images?: string[];
    threadID?: number;
    threadName: string;
    isMentioned?: boolean;
    unreadMessagesCount?: number;
  };
  reasons?: IReason[];
  isLoading?: boolean;
}
 
export default function ModalContent({
  merchantInfo,
  reasons,
  isLoading,
}: Props) {
  const [isNotesFocused, setIsNotesFocused] = useState(false);
  const notesRef = useRef<HTMLTextAreaElement | null>(null);
 
  const items = [
    {
      title: "Merchant",
      content: <MerchantInfoContent {...merchantInfo} actions={[]} />,
    },
    {
      title: "Trigger Cause",
      content: isLoading ? (
        <TriggerCauseSkeleton />
      ) : (
        <TriggerCauseContent reasons={reasons} />
      ),
    },
    {
      title: "Comment",
      content: (
        <NotesContent
          name="notes"
          placeholder="Write a comment"
          inputRef={notesRef}
          setIsNotesFocused={setIsNotesFocused}
          minHeight={60}
          maxHeight={248}
        />
      ),
      onEdit: isNotesFocused ? undefined : () => notesRef?.current?.focus(),
    },
  ];
 
  return (
    <Stack gap="20px">
      {items.map((item, index) => (
        <TaskCard key={index} title={item.title} onEdit={item.onEdit}>
          {item.content}
        </TaskCard>
      ))}
    </Stack>
  );
}
 
function TriggerCauseContent({ reasons }: { reasons?: IReason[] }) {
  const { control, watch } = useFormContext();
  const triggerCause = watch("reasonID");
  const isOtherSelected = isOtherReason(triggerCause, reasons);
 
  return (
    <Stack gap="16px">
      <Controller
        name="reasonID"
        control={control}
        render={({ field: { onChange, value }, fieldState: { error } }) => (
          <Box>
            <RadioGroup
              value={value || ""}
              onChange={(e) => onChange(e.target.value)}
              sx={{ gap: "12px" }}
            >
              {reasons?.map((option: IReason) => (
                <FormControlLabel
                  key={option.id}
                  control={<GiveRadio />}
                  label={
                    <GiveText variant="bodyS" color="primary">
                      {option.name}
                    </GiveText>
                  }
                  value={option.id}
                  checked={`${value}` === `${option.id}`}
                  sx={{
                    "&.MuiFormControlLabel-root": {
                      margin: "0px",
                      gap: "12px",
                      alignItems: "flex-start",
                    },
                  }}
                />
              ))}
            </RadioGroup>
            {error && (
              <GiveText pt="12px" pl="12px" variant="bodyXS" color="error">
                {error.message}
              </GiveText>
            )}
          </Box>
        )}
      />
      {isOtherSelected && (
        <Stack gap="8px">
          <GiveText variant="bodyS" color="primary">
            Reason
          </GiveText>
          <HFGiveInput name="customReasonText" />
        </Stack>
      )}
    </Stack>
  );
}
 
const TriggerCauseSkeleton = () => {
  return (
    <Stack gap="12px">
      {Array.from({ length: 5 }).map((_, index) => (
        <Skeleton
          key={index}
          width="80%"
          height="20px"
          variant="rectangular"
          sx={{ borderRadius: "16px" }}
        />
      ))}
    </Stack>
  );
};