All files / src/hooks/common/documents useDocumentHandlers.ts

25.58% Statements 11/43
7.14% Branches 3/42
12.5% Functions 1/8
28.94% Lines 11/38

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                      69x                 215x 215x   215x   215x         215x                                                                       215x                     215x                                 215x                 215x                                   215x                      
import { TMerchantDocument } from "@components/Merchants/MerchantPreview/data.types";
import { deleteDocument, downloadDocument, updateFile } from "./utils";
import useDocumentPreview from "./useDocumentPreview";
import { useCallback } from "react";
import { useQueryClient } from "react-query";
import { TDocument, TFileNavigation } from "@common/FilePreview/types";
import useFilePermissions from "./useFilePermissions";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { QKEY_IDENTIFICATION_FILES } from "@constants/queryKeys";
 
const useDocumentHandlers = (
  merchantID: number,
  list?: TMerchantDocument[],
  isEnterprise = false,
  refetcher?: () => void,
  localDeleteHandler?: () => void, //deletehandler to be used from preview modal in case the file is local only
  localOnly?: boolean, //if the file is only a state, and not yet uploaded anywhere
  isDeleteHidden?: boolean,
) => {
  const { isUpdateAllowed, isDeleteAllowed } = useFilePermissions(isEnterprise);
  const queryClient = useQueryClient();
 
  const { isDesktopView } = useCustomTheme();
 
  const handleDownload = useCallback((file: TDocument | null) => {
    if (!file) return;
    downloadDocument({ fileName: file.name, fileURL: file.URL }, isDesktopView);
  }, []);
 
  const handleDelete = useCallback(
    (file: TDocument | null, navigation?: TFileNavigation) => {
      if (!file) return;
 
      const refetchQuery = (skipRefetch = false) => {
        if (!skipRefetch) {
          //no need to refetch if file is only in local state
          refetcher && refetcher();
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_FILES,
            merchantID,
          ]);
          queryClient.invalidateQueries(QKEY_IDENTIFICATION_FILES);
        }
 
        if (!navigation?.showNext && !navigation?.showPrev) {
          return navigation?.handleClose && navigation.handleClose();
        }
 
        if (navigation?.showNext) {
          navigation.changeFile("next", file);
        } else {
          navigation?.changeFile("prev", file);
        }
      };
      if (localOnly && localDeleteHandler) {
        localDeleteHandler();
        refetchQuery(true);
      } else {
        deleteDocument(merchantID, { fileName: file.name, id: file.id }, () =>
          refetchQuery(),
        );
      }
    },
    [],
  );
  const { handlePreview } = useDocumentPreview({
    list,
    merchantID,
    handlers: {
      handleDownload,
      ...(localOnly && !localDeleteHandler ? {} : { handleDelete }), //if the file is local, and no deletor provided, it is not possible to delete it
    },
    isEnterprise,
    isDeleteHidden: isDeleteHidden,
  });
 
  const deleteDocumentHandler = (
    selectedDocument?: TMerchantDocument,
    onEnd?: () => void,
  ) => {
    if (!selectedDocument) return;
    if (localOnly && localDeleteHandler) {
      localDeleteHandler();
      onEnd?.();
    } else {
      deleteDocument(
        merchantID,
        { fileName: selectedDocument?.fileName || "", id: selectedDocument.id },
        onEnd,
      );
    }
  };
 
  const editDocumentHandler = async (
    merchantID: number,
    documentID: number,
    data: any,
  ) => {
    if (!documentID) return;
    updateFile(merchantID, documentID, data);
  };
 
  const downloadSingleDocsHandler = async (
    documentData?: TMerchantDocument,
    isDirectDownload?: boolean,
  ) => {
    if (!documentData) return;
    downloadDocument(
      {
        fileName: documentData?.fileName || "undefined",
        fileURL:
          typeof documentData.fileURL === "string"
            ? documentData.fileURL
            : URL.createObjectURL(documentData.fileURL),
      },
      isDesktopView,
      isDirectDownload,
    );
  };
 
  return {
    previewHandler: handlePreview,
    downloadHandler: downloadSingleDocsHandler,
    deleteHandler: deleteDocumentHandler,
    editHandler: editDocumentHandler,
    isDeleteAllowed,
    isUpdateAllowed,
  };
};
 
export default useDocumentHandlers;