All files / src/features/GiveOnboarding/pages/Bank BankFilesUploader.tsx

50% Statements 12/24
54.54% Branches 12/22
40% Functions 2/5
55% Lines 11/20

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                                                                                                                      5x 5x 5x 5x 5x 5x   5x 3x     5x                                                 5x                             5x                                                                                                                                                    
import GiveUploadArea from "@shared/FileUpload/GiveUploadArea";
import { DocumentSection } from "../pages.atoms";
import { AttachmentsUpload } from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/Sections.atoms";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import TaskCard from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/TaskCard";
import {
  AcceptAllowedGeneralDocumentsTypes,
  FileAttachmentType,
  MAX_UPLOAD_SIZE,
  useUploadFiles,
} from "@hooks/upload-api/uploadHooks";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { checkPortals } from "@utils/routing";
import { UploadSimpleIcon } from "@phosphor-icons/react";
import { Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { isEmpty } from "lodash";
import { useAppTheme } from "@theme/v2/Provider";
import { useQueryClient } from "react-query";
import { Dispatch, SetStateAction, useEffect } from "react";
import { Accept } from "react-dropzone/.";
import { showMessage } from "@common/Toast";
 
interface Props {
  bankFiles?: TMerchantDocument[];
  merchantId: number;
  resourceID?: number;
  title: string;
  isFormError?: boolean;
  tag: FileAttachmentType.BankAccountConfirm | FileAttachmentType.BankAccount;
  slug?: string;
  onSuccess?: () => void;
  setIsLoading?: Dispatch<SetStateAction<boolean>>;
  allowMultiple?: boolean;
  max?: number;
  onLocalUpload?: (file: File | File[]) => void;
  onLocalDelete?: (id: string | number, name: string, isExist: boolean) => void;
  isLoading?: boolean;
  accept?: Accept;
}
 
export default function BankFilesUploader({
  bankFiles,
  merchantId,
  resourceID,
  title,
  isFormError,
  tag,
  slug,
  onSuccess,
  setIsLoading,
  allowMultiple,
  max = 5,
  onLocalUpload,
  onLocalDelete,
  isLoading,
  accept = AcceptAllowedGeneralDocumentsTypes,
}: Props) {
  const { isEnterprisePortal } = checkPortals();
  const { isMobileView, isMobileDevice } = useCustomThemeV2();
  const { palette } = useAppTheme();
  const { handleUpload, isLoading: isLoadingUpload } = useUploadFiles();
  const isNoBankFile = isEmpty(bankFiles);
  const queryClient = useQueryClient();
 
  useEffect(() => {
    if (setIsLoading) setIsLoading(isLoadingUpload);
  }, [isLoadingUpload]);
 
  const uploadFunction = async (file: File | File[]) => {
    const allFiles = [file];
    const filesToUpload = allFiles.map((file) => ({
      file: file as File,
    }));
    if (!resourceID) return showMessage("Error", "No bank account found.");
    if (onLocalUpload) {
      onLocalUpload(file);
      return;
    }
    await handleUpload(
      {
        list: filesToUpload,
        merchantId,
        resourceID: resourceID,
        attachmentType: tag,
        label: tag,
        tag: FileAttachmentType.BankAccountConfirm,
      },
      slug,
    );
    if (onSuccess) onSuccess();
  };
 
  const uploadMessage = (
    <Stack justifyContent="center" gap="8px">
      {!isFormError && !isMobileDevice && !isMobileView && (
        <GiveText textAlign="center" color="secondary" variant="bodyXS">
          Drag and drop your files here or{" "}
          <GiveText component="span" variant="bodyXS" color="link">
            click to browse
          </GiveText>
        </GiveText>
      )}
      <GiveText color="secondary" variant="bodyXS">
        {`Up to ${MAX_UPLOAD_SIZE.label} each (.png, .jpg, .jpeg, .webp, .heic)`}
      </GiveText>
    </Stack>
  );
 
  return (
    <TaskCard
      title={title}
      containerSx={{ width: "100%" }}
      actions={
        isNoBankFile || !allowMultiple
          ? []
          : [
              {
                element: (
                  <AttachmentsUpload
                    merchantID={merchantId}
                    isEnterprise={isEnterprisePortal}
                    customElement={
                      <GiveIconButton
                        variant="ghost"
                        size="small"
                        Icon={UploadSimpleIcon}
                      />
                    }
                    resourceID={resourceID}
                    attachmentType={tag}
                    sx={{
                      backgroundColor: "transparent",
                      padding: 0,
                      height: "auto",
                      width: "auto",
                    }}
                  />
                ),
              },
            ]
      }
    >
      <>
        {isNoBankFile ? (
          <GiveUploadArea
            height="113px"
            message={uploadMessage}
            disabled={isLoadingUpload}
            uploadFunction={uploadFunction}
            accept={accept}
            maxFiles={max}
            multiple
            maxSizeInBytes={MAX_UPLOAD_SIZE.value}
            backgroundColor="transparent"
            title="Upload file"
            titleSx={{ fontSize: "14px", color: palette.text.secondary }}
            showSelectFileButton={false}
            iconBg={palette.primitive?.transparent["darken-5"]}
            warningText={
              isFormError
                ? "Uploading a bank account statement is required"
                : ""
            }
          />
        ) : (
          <DocumentSection
            ownerFiles={bankFiles}
            onDeleteSuccess={() => {
              queryClient.invalidateQueries([
                "onboarding-progress",
                merchantId,
              ]);
            }}
            deleteLocalFile={onLocalDelete}
            isLoading={isLoading}
            allowDelete
          />
        )}
      </>
    </TaskCard>
  );
}