All files / src/features/Merchants/MerchantSidePanel/GiveMerchantFile/hooks useDeleteDocument.tsx

50% Statements 12/24
25% Branches 1/4
25% Functions 2/8
52.17% Lines 12/23

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              56x 283x   283x           1x 1x   1x                             1x                       1x     1x     1x                                             283x       56x        
import { deleteDocumentAPI } from "@components/Settings/Business/Documents/utils";
import NiceModal from "@ebay/nice-modal-react";
import { useUploadProgress } from "@redux/slices/uploadProgressSlice";
import GiveText from "@shared/Text/GiveText";
import { delay } from "lodash";
import { GIVE_CONFIRMATION_POP_UP } from "modals/modal_names";
 
const useDeleteDocument = () => {
  const { setUploadProgress, hideUploadProgress } = useUploadProgress();
 
  const deleteDocument = (
    merchantID: number,
    document: { fileName: string; id: number },
    onEnd?: () => void,
    hideModal?: boolean,
  ) => {
    const { fileName, id } = document;
    const toastKey = `${fileName}${id}`;
 
    const onSuccess = () => {
      setUploadProgress({
        key: `${fileName}${id}`,
        data: {
          progress: 100,
          deleted: true,
          fileName: `${truncateFilename(fileName)} deleted`,
        },
      });
      delay(() => {
        hideUploadProgress(toastKey);
      }, 2000);
      onEnd?.();
    };
 
    const onError = () => {
      setUploadProgress({
        key: toastKey,
        data: {
          failed: true,
        },
      });
      delay(() => {
        hideUploadProgress(toastKey);
      }, 1000);
    };
 
    const deleteHandler = () => {
      deleteDocumentAPI(merchantID, id, onSuccess, true, true, onError);
    };
    Iif (hideModal) {
      deleteHandler();
    } else {
      NiceModal.show(GIVE_CONFIRMATION_POP_UP, {
        modalType: "delete",
        title: "Delete Document Confirmation",
 
        description: (
          <GiveText variant="bodyS" color="secondary">
            Are you sure you want to delete{" "}
            <GiveText
              sx={{ wordBreak: "break-all" }}
              component="span"
              variant="bodyS"
            >
              {fileName}
            </GiveText>
            ? This document will be permanently deleted.
          </GiveText>
        ),
        actions: {
          handleSuccess: { onClick: deleteHandler },
        },
      });
    }
  };
  return { deleteDocument };
};
export default useDeleteDocument;
 
const truncateFilename = (name: string) => {
  if (name.length > 15) return `${name.slice(0, 16)}...`;
  else return name;
};