All files / src/features/MerchantPortal/ManageMoney/hooks useBankAccounts.tsx

75% Statements 24/32
44.44% Branches 8/18
50% Functions 4/8
79.31% Lines 23/29

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                                1x                   14x 14x 14x   14x 14x   14x 1x 1x     14x       14x       14x 14x   14x 10x       14x       14x               14x   14x           14x                       14x                         14x                                                                 14x                                  
import NiceModal from "@ebay/nice-modal-react";
import { useListBankAccounts } from "@hooks/merchant-api/manage-money";
import { SxProps } from "@mui/material";
import {
  GIVE_EDIT_BANK_ACCOUNT_MODAL,
  UPLOAD_STATEMENT_MODAL_V2,
} from "modals/modal_names";
import { checkPortals } from "@utils/routing";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useMemo, useState } from "react";
import { useBankAccountPermissions } from "./useBankAccountPermissions";
import { EDIT_DENY_MESSAGE } from "@constants/permissions";
import { IMerchantBankAccount } from "@components/Merchants/MerchantPreview/data.types";
import { useTargetRef } from "@hooks/common/useObserveHTMLNode";
import useStatusChangeBankAccount from "@features/Merchants/MerchantSidePanel/GiveMerchantFile/hooks/useStatusChangeBankAccount";
 
export const useBankAccounts = () => {
  const {
    allRows,
    setPage,
    loadingRef,
    totalRows,
    page,
    isLoading,
    refetch,
    isAllowedAddAccounts,
  } = useListBankAccounts(false);
  const { merchantId } = useGetCurrentMerchantId();
  const { isEnterprisePortal } = checkPortals();
 
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
  const [selectedRowID, setSelectedRowID] = useState<number | null>(null);
 
  const onOpenMenu = (event: React.MouseEvent<HTMLElement>, id: number) => {
    setSelectedRowID(id);
    setAnchorEl(event.currentTarget);
  };
 
  const onCloseMenu = () => {
    setAnchorEl(null);
  };
 
  const { isUpdateAllowed, isDeleteAllowed } = useBankAccountPermissions({
    isEnterprise: isEnterprisePortal,
  });
 
  const bankAccountsLength = allRows.length;
  const isEmpty = !isLoading && bankAccountsLength === 0;
 
  const bankAccount = useMemo(
    () => allRows?.find((b) => b.id === selectedRowID),
    [selectedRowID, allRows],
  );
 
  const isLinkedBASelected = Boolean(
    bankAccount?.id === selectedRowID && bankAccount?.isLinked,
  );
 
  const { handleDelete } = useStatusChangeBankAccount({
    isLinkedBASelected,
    bankName: bankAccount?.name,
    merchantId,
    selectedRowID,
    refetch,
  });
 
  const targetRef = useTargetRef({ page, totalRows, setPage, loadingRef });
 
  const handleUploadStatement = () => {
    if (!selectedRowID) return;
 
    NiceModal.show(UPLOAD_STATEMENT_MODAL_V2, { bankAccountId: selectedRowID });
  };
 
  const handleEdit = (account?: IMerchantBankAccount) => {
    if (!selectedRowID && !account) return;
 
    NiceModal.show(GIVE_EDIT_BANK_ACCOUNT_MODAL, {
      merchantId,
      data: account?.id ? account : bankAccount,
      bankAccountId: account?.id ? account.id : selectedRowID,
      isLinkedBA: false,
      isEnterprise: isEnterprisePortal,
    });
  };
 
  const handleCreateAccount = () => {
    NiceModal.show(GIVE_EDIT_BANK_ACCOUNT_MODAL, { merchantId });
  };
 
  const options: {
    text: string;
    hidden: boolean;
    onClick: () => void;
    disabled?: boolean;
    sx?: SxProps;
    tooltipTitle?: string;
    disableTooltip?: boolean;
    type?: "default" | "destructive" | "label" | "divider";
  }[] = [
    {
      text: `Edit`,
      hidden: isLinkedBASelected,
      disabled: !isUpdateAllowed,
      onClick: handleEdit,
      tooltipTitle: EDIT_DENY_MESSAGE,
      disableTooltip: isUpdateAllowed,
    },
    {
      text: `Upload Statement`,
      hidden:
        isLinkedBASelected ||
        Boolean(
          bankAccount?.status &&
            !["upload", "update_requested"].includes(bankAccount?.status),
        ),
      disabled: !isUpdateAllowed,
      onClick: handleUploadStatement,
      tooltipTitle: EDIT_DENY_MESSAGE,
      disableTooltip: isUpdateAllowed,
    },
    {
      text: "Delete",
      hidden: isLinkedBASelected,
      disabled: !isDeleteAllowed,
      onClick: handleDelete,
      type: "destructive",
      tooltipTitle: EDIT_DENY_MESSAGE,
      disableTooltip: isUpdateAllowed,
    },
  ];
 
  return {
    allRows,
    isLoading,
    handleCreateAccount,
    isEmpty,
    onOpenMenu,
    isUpdateAllowed,
    handleEdit,
    bankAccountsLength,
    onCloseMenu,
    anchorEl,
    options,
    isLinkedBASelected,
    targetRef,
    isAllowedAddAccounts
  };
};