All files / src/components/UploadFile/Rebranded UploadFileNew.tsx

72.22% Statements 26/36
48.14% Branches 13/27
63.63% Functions 7/11
73.52% Lines 25/34

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                            21x                                 21x                               151x 151x   151x                           151x 17x     17x 17x         151x 17x           151x 151x 17x     17x         17x             17x   17x     151x                 151x   151x             151x                                                                   21x 2286x   151x                            
import * as React from "react";
import "react-dropzone-uploader/dist/styles.css";
import { Accept, ErrorCode, FileRejection, useDropzone } from "react-dropzone";
import { palette } from "@palette";
import { IStyleCustomization } from "react-dropzone-uploader";
import { MAX_UPLOAD_SIZE } from "@hooks/upload-api/uploadHooks";
import { useUploadProgress } from "@redux/slices/uploadProgressSlice";
import InputNew from "@components/UploadFile/Rebranded/InputNew";
import { getRandomNumber } from "@utils/helpers";
import { styled } from "@mui/material";
import useHEICConversion from "@components/UploadFile/hooks/useHEICConversion";
import { getCustomUploadFileText } from "./utils";
import { UploadDocumentTypes } from "@redux/slices/uploadProgressSlice/types";
 
const defaultSupportedFormatText = ".pdf, .png, .jpg, .jpeg, .webp, .heic";
 
type UploadFileProps = {
  uploadFunction: (file: any) => void;
  styles?: IStyleCustomization<React.CSSProperties>;
  disabled?: boolean;
  maxFiles?: number;
  multiple?: boolean;
  accept: Accept;
  maxSizeInBytes?: number;
  customText?: string;
  supportedFormatText?: string;
  returnAcceptedFilesAsArray?: boolean;
  isError?: boolean;
  documentType?: UploadDocumentTypes;
};
 
export const UploadFileNew = React.memo(function UploadFile({
  uploadFunction,
  styles = {
    dropzone: {},
  },
  disabled = false,
  maxFiles,
  multiple,
  accept,
  maxSizeInBytes = MAX_UPLOAD_SIZE.value,
  customText: customizedTextProp,
  supportedFormatText = defaultSupportedFormatText,
  returnAcceptedFilesAsArray,
  isError,
  documentType,
}: UploadFileProps) {
  const { setUploadProgress } = useUploadProgress();
  const { convertAndUploadHEICFiles } = useHEICConversion();
 
  const showErrorSnackbar = (
    file: FileRejection,
    error: "tooManyFiles" | "tooLarge" | "unsuported",
  ) => {
    setUploadProgress({
      key: `${getRandomNumber(1000000, 100000000)}`,
      data: {
        fileName: file.file.name,
        ...(error !== "tooManyFiles" && { size: file.file.size }),
        [error]: true,
      },
    });
  };
 
  const onUpload = (files: File[]) => {
    Iif (returnAcceptedFilesAsArray) {
      uploadFunction(files);
    } else {
      files.forEach((file) => {
        uploadFunction(file);
      });
    }
  };
 
  const convertAndUpload = async (acceptedFiles: File[]) => {
    await convertAndUploadHEICFiles({
      files: acceptedFiles,
      documentType,
      onComplete: onUpload,
    });
  };
  const inputRef = React.useRef<HTMLInputElement | null>(null);
  const onDrop = (acceptedFiles: File[], rejectedFiles: FileRejection[]) => {
    const hasTooManyFilesError = rejectedFiles.some((file) =>
      file.errors.some((error) => error.code === ErrorCode.TooManyFiles),
    );
    Iif (hasTooManyFilesError) {
      showErrorSnackbar(rejectedFiles[0], "tooManyFiles");
      return;
    }
 
    rejectedFiles.forEach((file) => {
      if (file.errors[0].code === ErrorCode.FileTooLarge) {
        showErrorSnackbar(file, "tooLarge");
      } else if (file.errors[0].code === ErrorCode.FileInvalidType) {
        showErrorSnackbar(file, "unsuported");
      }
    });
    convertAndUpload(acceptedFiles);
 
    Eif (inputRef.current) inputRef.current.value = "";
  };
 
  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    onDrop,
    disabled,
    multiple,
    maxFiles,
    accept,
    noClick: true,
    maxSize: maxSizeInBytes,
  });
  const { dropzone } = styles;
 
  const customText = getCustomUploadFileText({
    maxSizeInBytes,
    multiple,
    maxFiles,
    supportedFormatText,
  });
 
  return (
    <StyledDropzone
      {...getRootProps({})}
      style={{
        height: 200,
        width: "100%",
        minWidth: 330,
        borderRadius: 12,
        margin: 0,
        justifyContent: "center",
        cursor: "pointer",
        display: "flex",
        justifyItems: "center",
        alignItems: "center",
        ...dropzone,
        ...(disabled && {
          cursor: "not-allowed",
          pointerEvents: "none",
          opacity: 0.7,
        }),
      }}
      isDragActive={isDragActive}
      aria-disabled={disabled}
      isError={isError}
    >
      <InputNew
        {...getInputProps()}
        ref={inputRef}
        customText={customizedTextProp || customText || ""}
      />
    </StyledDropzone>
  );
});
 
const StyledDropzone = styled("div", {
  shouldForwardProp: (prop) => prop !== "isDragActive" && prop !== "isError",
})<{ isDragActive: boolean; isError?: boolean }>(
  ({ isDragActive, isError }) => ({
    border: isDragActive
      ? `2px solid ${palette.neutral[60]}`
      : `2px dashed ${palette.neutral[60]}`,
    ...(isError && {
      border: `2px solid ${palette.error.main}`,
    }),
    backgroundColor: "white",
    "&:hover": {
      border: `2px solid ${palette.neutral[60]}`,
      backgroundColor: "white",
    },
  }),
);