All files / src/components/common/BusinessOwnerIDUploader BusinessOwnerIDUploader.tsx

56.52% Statements 13/23
33.33% Branches 6/18
66.66% Functions 4/6
56.52% Lines 13/23

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                                                          19x                   6x         6x   6x   6x 6x       6x 6x   6x         6x 6x                                                                                                                           6x                                 6x                                                        
import { palette } from "@palette";
import Dropzone from "react-dropzone-uploader";
import { UploaderState } from "@components/Merchants/MerchantPreview/components/PrimaryAccountHolder/PAHUploaderMachine";
import { PAHUploaderLayout } from "@components/Merchants/MerchantPreview/components/PrimaryAccountHolder/PAHUploaderComponents";
import { isTestEnvironment } from "@constants/constants";
import { useBusinessOwnerIDUploader } from "@components/Merchants/MerchantPreview/hooks/useBusinessOwnerIDUploader";
import { BusinessOwnerIDInput } from "./BusinessOwnerIDInput";
import { useDropZoneStyles } from "@components/Merchants/MerchantPreview/components/PrimaryAccountHolder/PAHUploader";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { Controller, useFormContext } from "react-hook-form";
import { Stack } from "@mui/material";
import { Text } from "../Text";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { useIsValidFile } from "@hooks/upload-api/useIsValidFile";
import { MAX_UPLOAD_SIZE } from "@hooks/upload-api/uploadHooks";
import useHEICConversion from "@components/UploadFile/hooks/useHEICConversion";
import { UploadDocumentTypes } from "@redux/slices/uploadProgressSlice/types";
import { getFilesFromEvent } from "@shared/FileUpload/utils";
 
type Props = {
  merchantId: number;
  idFile?: TMerchantDocument;
  disabled?: boolean;
  onDelete?: () => void;
  idImageUrl?: string;
  documentType?: UploadDocumentTypes;
  sx?: any;
};
 
const BusinessOwnerIDUploader = ({
  merchantId,
  idFile,
  disabled,
  onDelete,
  idImageUrl,
  documentType,
  sx,
}: Props) => {
  const { deleteID, idUrl, downloadFile, previewDocument, updateIdUrl } =
    useBusinessOwnerIDUploader({
      merchantId,
      idFile,
      idImageUrl,
    });
  const { convertAndUploadHEICFiles } = useHEICConversion();
 
  const { isMobileView } = useCustomTheme();
 
  const isFileUploaded = !!idUrl;
  const dropZoneStyles = useDropZoneStyles({
    border: isFileUploaded ? "none" : "dashed",
  });
 
  const { control } = useFormContext();
  const { isDropzoneImageFileValid } = useIsValidFile();
 
  return (
    <Controller
      name={"files"}
      control={control}
      render={({ field: { onChange }, fieldState: { error } }) => {
        const errorMessage = (error as any)?.allFiles?.message;
        return (
          <Stack gap={1.5} minHeight="104px">
            <Dropzone
              disabled={disabled}
              classNames={{ ...dropZoneStyles }}
              styles={{
                dropzone: {
                  height: "104px",
                  borderRadius: 12,
                  justifyContent: "center",
                  backgroundColor: palette.neutral[5],
                  position: "relative",
                  borderColor: errorMessage ? palette.filled.red : undefined,
                  ...sx,
                },
                dropzoneDisabled: {
                  opacity: 1,
                },
              }}
              onChangeStatus={(fileWithMeta, status, allFiles) => {
                const changeStatus = isTestEnvironment ? "done" : status; //Ensure onChange is called on first instance within jest
                if (!isDropzoneImageFileValid(fileWithMeta, changeStatus)) {
                  return;
                }
 
                const currFile = allFiles[allFiles.length - 1];
 
                const handleSave = (files: File[]) => {
                  const [newFile] = files;
                  const file = {
                    ...currFile,
                    file: newFile,
                    meta: {
                      ...currFile?.meta,
                      name: newFile.name,
                      size: newFile.size,
                      type: newFile.type,
                      previewUrl: URL.createObjectURL(newFile),
                    },
                  };
 
                  updateIdUrl(file.meta.previewUrl ?? "");
                  onChange(
                    {
                      fileWithMeta: file,
                      status: changeStatus,
                      allFiles: [file],
                    },
                    { shouldDirty: true },
                  );
                };
 
                convertAndUploadHEICFiles({
                  files: [currFile.file],
                  documentType,
                  onComplete: handleSave,
                });
              }}
              multiple={false}
              canCancel={false}
              getFilesFromEvent={getFilesFromEvent}
              LayoutComponent={(props) => (
                <PAHUploaderLayout
                  {...props}
                  disabled={!!disabled}
                  fileUrl={idUrl}
                  state={
                    isFileUploaded
                      ? UploaderState.ON_UPLOADED
                      : UploaderState.ON_INITIAL
                  }
                  sx={{
                    "&:hover": null,
                    background: `linear-gradient(180deg, rgba(12, 12, 12, 0.24) 0%, rgba(12, 12, 12, 0.82) 70%), url(${idUrl}) lightgray 50% / cover no-repeat`,
                  }}
                />
              )}
              maxSizeBytes={MAX_UPLOAD_SIZE.value}
              InputComponent={(props) => (
                <BusinessOwnerIDInput
                  {...props}
                  isMobile={isMobileView}
                  fileName={idUrl}
                  state={
                    isFileUploaded
                      ? UploaderState.ON_UPLOADED
                      : UploaderState.ON_INITIAL
                  }
                  fileUrl={idUrl}
                  downloadHandler={downloadFile}
                  deleteDocument={onDelete ? onDelete : deleteID}
                  disabled={!!disabled}
                  previewDocument={previewDocument}
                />
              )}
            />
            {errorMessage && (
              <Text color={palette.neutral[70]}>{errorMessage}</Text>
            )}
          </Stack>
        );
      }}
    />
  );
};
 
export default BusinessOwnerIDUploader;