All files / src/features/MerchantPortal/ManageMoney/modals/BankAccounts GiveBankAccountsModal.tsx

91.66% Statements 11/12
68.75% Branches 11/16
80% Functions 4/5
90% Lines 9/10

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                                  1x 15x 14x 14x                               14x   14x                                                                                                                                     6x                                                                                   6x           8x                  
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import NiceModal from "@ebay/nice-modal-react";
import GiveBankAccountItem from "@features/Merchants/MerchantSidePanel/GiveMerchantFile/bankAccounts/GiveBankAccountItem";
import { Box, Stack } from "@mui/material";
import GiveButton from "@shared/Button/GiveButton";
import GiveEmptyStateWrapper from "@shared/EmptyState/GiveEmptyStateWrapper";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useGetCurrentMerchantId } from "@hooks/common";
import { styled } from "@theme/v2/Provider";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import { useBankAccounts } from "../../hooks/useBankAccounts";
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { NOT_ALLOWED_TO_ADD_BANK_ACCOUNT_TOOLTIP_MESSAGE } from "@constants/stringConstants";
import { FileAttachmentType } from "@hooks/upload-api/uploadHooks";
 
const GiveBankAccountsModal = NiceModal.create(() => {
  const { open, onClose } = useNiceModal();
  const { isMobileView } = useCustomThemeV2();
  const { merchantId } = useGetCurrentMerchantId();
 
  const {
    allRows,
    isLoading,
    handleCreateAccount,
    isEmpty,
    onOpenMenu,
    isUpdateAllowed,
    handleEdit,
    bankAccountsLength,
    onCloseMenu,
    anchorEl,
    options,
    targetRef,
    isAllowedAddAccounts,
  } = useBankAccounts();
 
  return (
    <GiveBaseModal
      open={open}
      title="Bank Accounts"
      width={isMobileView ? "100%" : "720px"}
      height="fit-content"
      onClose={onClose}
      showFooter={!isEmpty}
      sx={{
        ...(isMobileView && {
          "& .MuiDialogActions-root": {
            display: "flex",
            flexDirection: "row",
            width: "365px",
            marginLeft: "auto",
            justifyContent: "flex-end",
          },
        }),
      }}
      buttons={
        isEmpty ? undefined : (
          <Stack direction="row" spacing={1.5} justifyContent="flex-end">
            <GiveButton
              variant="ghost"
              size="large"
              label="Cancel"
              onClick={onClose}
            />
            <GiveTooltip
              title={NOT_ALLOWED_TO_ADD_BANK_ACCOUNT_TOOLTIP_MESSAGE}
              placement="top"
              disableHoverListener={isAllowedAddAccounts}
            >
              <AddButton
                size="large"
                variant="filled"
                label="Add Bank Account"
                sx={{ border: "none" }}
                onClick={handleCreateAccount}
                disabled={!isAllowedAddAccounts}
              />
            </GiveTooltip>
          </Stack>
        )
      }
    >
      {isLoading ? (
        <Box
          minHeight="300px"
          display="flex"
          alignItems="center"
          justifyContent="center"
        >
          <LoadingSpinner sx={{ height: "30%" }} />
        </Box>
      ) : (
        <GiveEmptyStateWrapper
          isEmpty={isEmpty}
          section="bank-accounts-with-action"
          action={{
            handleAction: handleCreateAccount,
          }}
          giveEmptyStateSx={{ paddingTop: "44px" }}
          iconContainerSx={{ width: "56px", height: "56px" }}
        >
          <ListContainer>
            {allRows?.map((account, idx) => (
              <GiveBankAccountItem
                key={account.id}
                account={account}
                onOpenMenu={account.isLinked ? undefined : onOpenMenu}
                disabled={isUpdateAllowed}
                onEdit={handleEdit}
                isFirst={idx === 0}
                isLast={idx === bankAccountsLength - 1}
                merchantId={merchantId}
                localDocuments={
                  account.uploadedBankFiles?.map((doc) => ({
                    ...doc,
                    tag: FileAttachmentType.BankAccountConfirm,
                    updatedAt: new Date().getTime() / 1000,
                  })) as any
                }
              />
            ))}
          </ListContainer>
        </GiveEmptyStateWrapper>
      )}
      {bankAccountsLength ? (
        <div
          ref={targetRef}
          style={{
            width: 1,
            height: 1,
            visibility: "hidden",
          }}
        ></div>
      ) : null}
      <ContextualMenu
        handleClose={onCloseMenu}
        anchorEl={anchorEl}
        color={isMobileView ? "primary" : "tertiary"}
        texture={isMobileView ? "solid" : "blurred"}
        options={options}
      />
    </GiveBaseModal>
  );
});
 
const ListContainer = styled(Box)(({ theme }) => ({
  borderRadius: "20px",
  border: `1px solid ${theme.palette.border?.primary}`,
  width: "100%",
}));
 
const AddButton = styled(GiveButton)(({ theme }) => ({
  border: "none",
  whiteSpace: "nowrap",
  [theme.breakpoints.down("v2_sm")]: {
    minWidth: "160px !important",
  },
}));
 
export default GiveBankAccountsModal;