All files / src/features/Merchants/MerchantSidePanel/GiveMerchantFile/bankAccounts GiveBankAccountItemBody.tsx

83.33% Statements 20/24
70.27% Branches 26/37
77.77% Functions 7/9
86.36% Lines 19/22

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                                                    43x                       3x   3x                                                     3x 3x   3x     3x       3x         3x             18x   18x                                                                               43x                     3x                                     43x                 18x                 43x 3x 3x                   43x                            
import { IMerchantBankAccount } from "@components/Merchants/MerchantPreview/data.types";
import { Box, Stack } from "@mui/material";
import { ReactNode } from "react";
import { styled, useAppTheme } from "@theme/v2/Provider";
import GiveButton from "@shared/Button/GiveButton";
import { GiveSectionItem } from "../GiveSectionItem";
import {
  ArrowRightIcon,
  PencilSimpleIcon,
  WarningIcon,
} from "@phosphor-icons/react";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { EDIT_DENY_MESSAGE } from "@constants/permissions";
import { capitalize, isFunction } from "lodash";
import GiveUploadStack from "@shared/FileUpload/GiveUploadStack";
import GiveText from "@shared/Text/GiveText";
import { GiveUploadItemProps } from "@shared/FileUpload/types";
import { PLAID_ACCOUNT_INFO } from "modals/modal_names";
import NiceModal from "@ebay/nice-modal-react";
import { findBankByRoutingNumber } from "@utils/bank_list";
import { CustomGridContainer } from "./CustomGridContainer";
import { checkPortals } from "@utils/routing";
import { ISectionItem } from "@features/TransactionPanel/types";
 
type DataItem = { title: string; value: string; hidden?: boolean };
 
type TBankAccountBody = {
  account: IMerchantBankAccount;
  onEdit?: (account: IMerchantBankAccount) => void;
  disabled?: boolean;
  documents?: GiveUploadItemProps[];
  isProviderLinked?: boolean;
  withWrapper?: boolean;
};
const BankAccountBody = ({
  account,
  onEdit,
  disabled,
  documents,
  withWrapper = true,
}: TBankAccountBody) => {
  const {
    isManageMoney,
    isEnterprisePortal,
    isMerchantPortal,
    isAcquirerManageMoney,
  } = checkPortals();
 
  const items: DataItem[] = [
    {
      title: "Created",
      value: account?.created?.replaceAll(" ", ""),
    },
    {
      title: "Account Type",
      value: capitalize(account?.type),
    },
    {
      title: "Business Name on Account",
      value: account?.name,
    },
    {
      title: "Account Number",
      value: account?.accountNumber,
    },
    {
      title: "Routing Number",
      value: account?.routingNumber,
    },
    {
      title: "Bank Name",
      value:
        account?.bankName || findBankByRoutingNumber(account?.routingNumber),
    },
  ];
  const { authPlaidInfo } = account || {};
  const isProviderLinked = account.enterprise || account.isLinked;
 
  const hiddenEdit = isManageMoney && isProviderLinked;
 
  const isDeleteBAHidden =
    isMerchantPortal ||
    isEnterprisePortal ||
    (account?.status === "approved" && isManageMoney);
 
  const handleViewPlaidInfo = () => {
    if (!authPlaidInfo) return;
    NiceModal.show(PLAID_ACCOUNT_INFO, { data: authPlaidInfo });
  };
 
  return (
    <Stack spacing={0.5} width={"100%"}>
      {documents &&
        documents?.length < 1 &&
        !isProviderLinked &&
        !isAcquirerManageMoney && <AlertItem />}
      {items
        .filter((entry) => !entry.hidden)
        .map((item, index) => (
          <DetailsItem
            key={index}
            item={item}
            highlight={index % 2 !== 0}
            withWrapper={withWrapper}
          />
        ))}
      <Stack
        paddingX={withWrapper ? "16px" : 0}
        gap="16px"
        marginBottom="16px !important"
      >
        {documents && documents?.length > 0 && (
          <GiveUploadStack items={documents} isHideDelete={isDeleteBAHidden} />
        )}
        {withWrapper && (
          <Stack direction="row" justifyContent="space-between" gap="10px">
            {!hiddenEdit && (
              <CustomButton
                lable="Edit"
                onClick={isFunction(onEdit) ? () => onEdit(account) : undefined}
                endIcon={<PencilSimpleIcon size={14} />}
                disabled={disabled}
              />
            )}
            {account?.plaid && authPlaidInfo && (
              <CustomButton
                lable="Plaid Information"
                onClick={handleViewPlaidInfo}
                endIcon={<ArrowRightIcon size={18} />}
                disabled={disabled}
              />
            )}
          </Stack>
        )}
      </Stack>
    </Stack>
  );
};
 
const CustomButton = ({
  lable,
  onClick,
  endIcon,
  disabled = false,
}: {
  lable: string;
  onClick?: () => void;
  endIcon: ReactNode;
  disabled?: boolean;
}) => (
  <GiveTooltip
    width="compact"
    alignment="left"
    title={EDIT_DENY_MESSAGE}
    color="default"
    disableHoverListener={!disabled}
  >
    <GiveButton
      sx={{ width: "100%", border: "none" }}
      disabled={disabled}
      onClick={onClick}
      label={lable}
      endIcon={endIcon}
      variant="filled"
      color="light"
      size="small"
    />
  </GiveTooltip>
);
const DetailsItem = ({
  item,
  highlight,
  withWrapper,
}: {
  item: ISectionItem;
  highlight: boolean;
  withWrapper?: boolean;
}) => {
  return (
    <Box padding={withWrapper ? "16px 16px 0px 32px" : "16px 0px 0px 16px"}>
      <CustomGridContainer container spacing={2} highlight={highlight}>
        <GiveSectionItem item={item} />
      </CustomGridContainer>
    </Box>
  );
};
 
const AlertItem = () => {
  const { palette } = useAppTheme();
  return (
    <AlertContainer>
      <WarningIcon color={palette.primitive?.warning[100]} size={24} />
      <GiveText variant="bodyS" color="warning">
        A bank statement needs to be uploaded.
      </GiveText>
    </AlertContainer>
  );
};
 
const AlertContainer = styled(Box)(({ theme }) => ({
  boxSizing: "content-box",
  padding: "12px",
  margin: "0 16px 10px 16px !important",
  gap: "12px",
  maxHeight: "36px",
  borderRadius: "8px",
  display: "flex",
  flexDirection: "row",
  alignItems: "center",
  backgroundColor: theme.palette.primitive?.warning[10],
}));
 
export default BankAccountBody;