All files / src/components/ProfilePage/BankAccountSetup OnboardingBankStatement.tsx

92.1% Statements 35/38
72.72% Branches 8/11
78.57% Functions 11/14
91.89% Lines 34/37

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246                                                            20x         11x 11x 11x 11x     11x       11x   11x 1x                             11x                     11x   11x   11x 7x 7x   7x     11x                                       14x 14x                             7x                                         20x                   21x                                   21x   21x       21x 21x 21x                 21x     20x         53x 53x     106x                                         371x                         106x                        
import { useFormContext } from "react-hook-form";
import { IFileWithMeta } from "react-dropzone-uploader";
import { Text } from "@common/Text";
import { Stack, styled } from "@mui/material";
import { palette } from "@palette";
import { useGetCurrentMerchantId } from "@hooks/common";
import { DocumentItem } from "@components/Merchants/MerchantPreview/components/DocumentItem";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { useMemo } from "react";
import {
  TBankFormInputsNew,
  TBankFormInputsOld,
} from "@hooks/onboarding/useAddOnboardingBankAccounts";
import mime from "mime";
import { useAppSelector } from "@redux/hooks";
import { selectUser } from "@redux/slices/auth/auth";
import { toUnixDateFormat } from "@utils/date.helpers";
import { UploadFileNew } from "@components/UploadFile/Rebranded/UploadFileNew";
import { AcceptAllowedGeneralDocumentsTypes } from "@hooks/upload-api/uploadHooks";
import { BA_DOCUMENTS } from "@constants/constants";
import { checkPortals } from "@utils/routing";
import { useCheckDocumentProcessing } from "@redux/slices/uploadProgressSlice";
import { UploadDocumentTypes } from "@redux/slices/uploadProgressSlice/types";
 
type Props = {
  firstAccount?: any;
  bankFiles?: { data: TMerchantDocument[]; total: number };
  bankFilesLoading?: boolean;
};
 
const OnboardingBankStatement = ({ bankFiles }: Props) => {
  const {
    setValue,
    watch,
    formState: { errors },
  } = useFormContext<TBankFormInputsNew | TBankFormInputsOld>();
  const values = watch();
  const { merchantId } = useGetCurrentMerchantId();
  const { isEnterprisePortal } = checkPortals();
 
  const documentType =
    UploadDocumentTypes[
      isEnterprisePortal ? "providerBankAccount" : "merchantBankAccount"
    ];
 
  const isConvertingDocument = useCheckDocumentProcessing(documentType);
 
  const selectFiles = (file: File) => {
    setValue(
      "files",
      {
        allFiles: values?.files?.allFiles
          ? [
              ...(values?.files
                ?.allFiles as TBankFormInputsNew["files"]["allFiles"]),
              { file, id: Date.now() },
            ]
          : [{ file, id: Date.now() }],
      },
      { shouldDirty: true, shouldValidate: true },
    );
  };
 
  const deleteFileHandlerNew = (index: number) => {
    setValue(
      "files",
      {
        ...values.files,
        allFiles: values?.files?.allFiles.filter((_, i) => i !== index),
      } as any,
      { shouldDirty: true, shouldValidate: true },
    );
  };
 
  const user = useAppSelector(selectUser);
  const userFullName =
    user.globalName.firstName + " " + user.globalName.lastName;
 
  const joinedList = useMemo(() => {
    const localList = values?.files?.allFiles.map((file, index) =>
      parseDocument(file, index, userFullName),
    );
    return [...localList, ...(bankFiles?.data || [])];
  }, [values?.files?.allFiles, bankFiles?.data]);
 
  return (
    <>
      <DocumentUploadDisclaimer />
      <Stack direction="column" gap={1} alignItems="stretch" mt="32px">
        <UploadFileNew
          uploadFunction={selectFiles}
          accept={AcceptAllowedGeneralDocumentsTypes}
          documentType={documentType}
          disabled={isConvertingDocument}
          isError={!!errors?.files}
        />
        <Stack direction="column" gap={0.5}>
          {values?.files?.allFiles?.map(
            (
              file: {
                file: File;
                id: number;
              },
              index: number,
            ) => {
              const parsedDocument = parseDocument(file, index, userFullName);
              return (
                <DocumentItem
                  key={index}
                  merchantID={merchantId}
                  list={joinedList}
                  localDeleteHandler={() => deleteFileHandlerNew(index)}
                  hiddenActionProps={{
                    hidden: false,
                  }}
                  {...parsedDocument}
                />
              );
            },
          )}
          {bankFiles?.data?.map((file) => {
            return (
              <DocumentItem
                key={file.id}
                merchantID={merchantId}
                list={joinedList}
                refetcher="get-bank-files"
                hiddenActionProps={{
                  hidden: false,
                }}
                {...file}
              />
            );
          })}
        </Stack>
      </Stack>
    </>
  );
};
 
export default OnboardingBankStatement;
 
const parseDocument = (
  localUpload:
    | {
        file: File;
        id: number;
      }
    | IFileWithMeta,
  index: number,
  userFullName: string,
): TMerchantDocument => {
  const data = {
    id: index,
    attTypeID: 0,
    attTypeName: "",
    accID: 0,
    fileMetadata: null,
    s3ObjectKey: "",
    label: "",
    notes: "",
    tag: "",
    isUploaded: false,
    bankAccountID: 0,
    legalPrincipalID: 0,
    accMemberUserID: 0,
    underwritingProfileID: 0,
    userFullName,
  } as TMerchantDocument;
 
  let additionalData = {};
 
  const { file, id } = localUpload as {
    file: File;
    id: number;
  };
  const fileType = mime.getExtension(file.type) || "";
  const uploadedAt = toUnixDateFormat(new Date(id));
  additionalData = {
    fileURL: URL.createObjectURL(file),
    fileName: file.name,
    fileType,
    createdAt: uploadedAt,
    updatedAt: uploadedAt,
    userFullName,
  };
 
  return { ...data, ...additionalData };
};
 
export const DocumentUploadDisclaimer = ({
  isFirstStep = false,
}: {
  isFirstStep?: boolean;
}) => {
  const steps = [BA_DOCUMENTS.step_2, BA_DOCUMENTS.step_3];
  return (
    <Stack gap="32px" mt="12px">
      {steps.map((step, idx) => (
        <Stack gap="16px" key={idx}>
          <Text
            fontWeight={350}
            fontSize={20}
            variant="headline"
            lineHeight="120%"
            color={palette.neutral[80]}
          >
            {isFirstStep ? step.title_v1 : step.title}
          </Text>
          <Text
            fontSize={14}
            fontWeight={350}
            variant="headline"
            lineHeight="120%"
            color={palette.warning.text}
          >
            {step.info}
          </Text>
          <StyledList>
            {step.list.map((el, i) => (
              <li key={i}>
                <Text fontSize={14} color={palette.black[100]} fontWeight={350}>
                  {el}
                </Text>
              </li>
            ))}
          </StyledList>
        </Stack>
      ))}
    </Stack>
  );
};
 
const StyledList = styled("ul")(({ theme }) => ({
  margin: 0,
  paddingLeft: "24px",
  "& li": {
    "::marker": {
      marginRight: "8px",
      fontSize: "18px",
      lineHeight: "18px",
      color: theme.palette.neutral[80],
    },
  },
}));