All files / src/features/Merchants/MerchantSidePanel/components/PrimaryAccountHolderSection IDUploader.tsx

75% Statements 6/8
83.33% Branches 10/12
60% Functions 3/5
75% Lines 6/8

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                                                                                  63x                                               68x                 68x   68x                                                       25x                                                       43x                                                      
import Dropzone from "react-dropzone-uploader";
import { IDUploaderInput, IDUploaderLayout } from "./IDUploaderComponents";
import IDUploaderActions from "./IDUploaderActions";
import { UPLOAD_DENY_MESSAGE } from "@constants/permissions";
import { usePAHUploader } from "@components/Merchants/MerchantPreview/hooks/usePAHUploader";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { TOwnerFile } from "@components/Merchants/MerchantPreview/components/PrimaryAccountHolder/types";
import { useAppTheme } from "@theme/v2/Provider";
import { UploaderState } from "@components/Merchants/MerchantPreview/components/PrimaryAccountHolder/PAHUploaderMachine";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { UploadDocumentTypes } from "@redux/slices/uploadProgressSlice/types";
import { getFilesFromEvent } from "@shared/FileUpload/utils";
import { MAX_UPLOAD_SIZE } from "@hooks/upload-api/uploadHooks";
 
type Props = {
  type: TOwnerFile;
  merchantId: number;
  docUrl: string;
  isMobile?: boolean;
  disabled?: boolean;
  isOFACMatch?: boolean;
  downloadHandler: (
    documentData?: TMerchantDocument | undefined,
  ) => Promise<void>;
  fileId: number | undefined;
  previewDocument: ({
    tmpUrl,
    type,
    fileName,
  }: {
    tmpUrl?: string | undefined;
    type?: TOwnerFile | undefined;
    fileName?: string;
  }) => void;
  isUploadAllowed: boolean;
  isDeleteAllowed: boolean;
  isDeleteHidden?: boolean;
  documentType?: UploadDocumentTypes;
  setLocalDocs?: (data: any) => void;
};
 
const IDUploader = ({
  type,
  merchantId,
  docUrl,
  downloadHandler,
  isMobile,
  previewDocument,
  fileId,
  isDeleteAllowed,
  isUploadAllowed,
  disabled = false,
  isDeleteHidden = false,
  documentType,
  setLocalDocs,
  isOFACMatch,
}: Props) => {
  // Logic of upload, snackbars and state dispatch
  const {
    fileUrl,
    state,
    fileName,
    handleChangeStatus,
    _deleteDocument,
    isUploadable,
  } = usePAHUploader({
    docUrl,
    merchantId,
    type,
    fileId,
    documentType,
    setLocalDocs,
  });
 
  const theme = useAppTheme();
 
  return (
    <>
      <Dropzone
        key={
          state === UploaderState.ON_UPLOADED && !!fileUrl
            ? `uploaded-${fileUrl}`
            : "uploading"
        }
        disabled={!isUploadable || disabled}
        styles={{
          dropzone: {
            flex: 1,
            overflow: "hidden",
            borderRadius: 12,
            justifyContent: "center",
            backgroundColor: theme.palette.primitive?.transparent["darken-5"],
            border: "none",
          },
          dropzoneDisabled: {
            opacity: 1,
          },
        }}
        onChangeStatus={handleChangeStatus}
        multiple={false}
        canCancel={false}
        LayoutComponent={
          state === UploaderState.ON_UPLOADED
            ? (props) => (
                <IDUploaderLayout
                  {...props}
                  type={type}
                  fileUrl={fileUrl}
                  actions={
                    <IDUploaderActions
                      type={type}
                      downloadHandler={downloadHandler}
                      deleteDocument={_deleteDocument}
                      previewDocument={() =>
                        previewDocument({ tmpUrl: fileUrl, type, fileName })
                      }
                      disabled={{
                        delete: !isDeleteAllowed || isOFACMatch,
                      }}
                      hidden={{
                        delete: isDeleteHidden,
                      }}
                      isOFACMatch={isOFACMatch}
                    />
                  }
                />
              )
            : undefined
        }
        PreviewComponent={() => <></>}
        maxSizeBytes={MAX_UPLOAD_SIZE.value}
        InputComponent={(props) => (
          <GiveTooltip
            width="compact"
            alignment="left"
            title={UPLOAD_DENY_MESSAGE}
            color="default"
            disableHoverListener={isUploadAllowed}
          >
            <IDUploaderInput
              {...props}
              isMobile={isMobile}
              fileName={fileName}
              type={type}
              state={state}
              fileUrl={fileUrl}
              previewDocument={previewDocument}
              isUploadable={isUploadable}
              disabled={disabled}
            />
          </GiveTooltip>
        )}
        getFilesFromEvent={getFilesFromEvent}
      />
    </>
  );
};
 
export default IDUploader;