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

70.58% Statements 24/34
51.72% Branches 15/29
57.14% Functions 4/7
70.58% Lines 24/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                                  22x         221x   221x   221x   221x   221x   221x   221x 30x 3x   30x                   221x 221x     221x 221x                             221x                         221x         221x     1x   1x               1x     221x 4x     221x                    
import { IFileWithMeta } from "react-dropzone-uploader";
import { downloadDocument } from "@hooks/common/documents/utils";
import { useFormContext } from "react-hook-form";
import { useEffect, useState } from "react";
import { extractExtensionFromURL } from "@utils/helpers";
import useDocumentPreviewV2 from "@shared/FilePreview/hooks/useDocumentPreviewV2";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { getOwnerIdImage } from "@components/Merchants/MerchantPreview/utils";
 
type Props = {
  merchantId: number;
  fileId?: number;
  idFile?: TMerchantDocument;
  idImageUrl?: string;
};
 
export const useBusinessOwnerIDUploader = ({
  merchantId,
  idFile,
  idImageUrl,
}: Props) => {
  const [idUrl, setIdUrl] = useState("");
 
  const { isDesktopView } = useCustomThemeV2();
 
  const { setValue, watch } = useFormContext();
 
  const { files = { allFiles: [] } } = watch();
 
  const file = files.allFiles[0] || {};
 
  const isLocalFile = !!file.meta;
 
  useEffect(() => {
    if (idFile) {
      setValue("files", { allFiles: [idFile] });
    }
    setIdUrl(
      getOwnerIdImage(
        isLocalFile,
        file.meta?.previewUrl,
        idFile?.fileURL as string,
        idImageUrl,
      ),
    );
  }, [file.meta?.previewUrl, isLocalFile, idImageUrl]);
 
  const idName = isLocalFile ? file.meta?.name : file?.fileName;
  const idFileType = isLocalFile
    ? file?.meta?.type.split("/")[1]
    : file?.fileType;
  const idFileId = isLocalFile ? file?.meta?.id : file.id;
  const deleteID = async () => {
    setValue("files", { allFiles: [] }, { shouldDirty: true });
    setIdUrl("");
 
    if (isLocalFile) {
      files?.allFiles?.forEach((file: IFileWithMeta) => {
        if (typeof file?.remove === "function") {
          file.remove();
        }
      });
    } else if (idFile) {
      setValue("uploadedfiles", watch("files"), { shouldDirty: true });
    }
  };
 
  const downloadFile = async () => {
    if (idUrl) {
      await downloadDocument(
        {
          fileName: idName || "business-owner",
          fileURL: idUrl,
        },
        isDesktopView,
        true,
      );
    }
  };
 
  const { handlePreview } = useDocumentPreviewV2({
    entityId: merchantId,
    handleLocalDelete: deleteID,
  });
 
  const previewDocument = () => {
    // if no file url, skip preview
    // if (!idUrl) return;
    const extension = idFileType ? "" : extractExtensionFromURL(idUrl);
 
    const fileToPreview = {
      fileURL: idUrl,
      fileName: idName || "business-owner." + extension,
      fileType: idFileType || extension,
      id: idFileId || 1,
      tag: "legal_principal",
    } as TMerchantDocument;
 
    handlePreview(fileToPreview, true);
  };
 
  const updateIdUrl = (url: string) => {
    setIdUrl(url);
  };
 
  return {
    deleteID,
    downloadFile,
    file,
    previewDocument,
    isLocalFile,
    idUrl,
    updateIdUrl,
  };
};