All files / src/features/GiveOnboarding/pages/Bank/Linked index.tsx

71.42% Statements 20/28
50% Branches 12/24
57.14% Functions 4/7
73.07% Lines 19/26

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                                                                                2x 2x 2x 2x     2x     2x   2x         2x       2x         2x   2x   2x                             2x                           2x                                                                         2x                                           12x                                                             10x                     10x 12x                          
import moment from "moment";
import { Box, Grid } from "@mui/material";
import { StepViewContainer } from "@features/GiveOnboarding/container/StepViewContainer";
import GiveText from "@shared/Text/GiveText";
import RESOURCE_BASE, {
  EDIT_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
 
import { LinkBreakIcon } from "@phosphor-icons/react";
import GiveButton from "@shared/Button/GiveButton";
import { styled } from "@theme/v2/Provider";
import GiveChip from "@shared/Chip/GiveChip";
import {
  composePermission,
  useAccessControl,
} from "@features/Permissions/AccessControl";
import { checkPortals } from "@utils/routing";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { DELETE_CONFIRMATION_MODAL } from "modals/modal_names";
import NiceModal from "@ebay/nice-modal-react";
import { useUpdateBankAccountSettings } from "@components/Merchants/MerchantPreview/hooks/useMerchantBankAccounts";
import { useGetCurrentMerchantId } from "@hooks/common";
import { findBankByRoutingNumber } from "@utils/bank_list";
import { IApiData } from "@features/GiveOnboarding/types/form";
import { useQueryClient } from "react-query";
import { QKEY_LIST_ALL_BANK_ACCOUNTS } from "@constants/queryKeys";
import {
  setCurrentSubstep,
  updateFormData,
} from "@redux/slices/onboardingWizard";
import { useDispatch } from "react-redux";
import { IStep } from "@features/GiveOnboarding/types";
import { useDeleteCompletedSteps } from "@features/GiveOnboarding/mutations";
 
interface Props {
  data: Pick<IApiData["data"], "business_bank_connect" | "merchant_info">;
}
 
function Linked(props: Props) {
  const queryClient = useQueryClient();
  const { merchantId } = useGetCurrentMerchantId();
  const dispatch = useDispatch();
  const { handleUpdateBankAccountSettings } = useUpdateBankAccountSettings(
    merchantId as number,
  );
  const { mutateAsync: deleteCompletedSteps } = useDeleteCompletedSteps({
    merchantId,
  });
  const { isAcquirerEnterprises } = checkPortals();
 
  const resource = composePermission(
    isAcquirerEnterprises ? RESOURCE_BASE.ENTERPRISE : RESOURCE_BASE.MERCHANT,
    RESOURCE_BASE.BANK_ACCOUNT,
  );
 
  const isDeleteAllowed = useAccessControl({
    resource,
    operation: OPERATIONS.DELETE,
  });
  const isUpdateAllowed = useAccessControl({
    resource,
    operation: OPERATIONS.UPDATE,
  });
 
  const firstBankAccount = props?.data?.business_bank_connect;
 
  const initialBankAccountNumber = firstBankAccount?.accountNumber || "";
 
  const firstAccount = {
    id: firstBankAccount?.bankDataAux?.id || "",
    name: firstBankAccount?.name || "",
    createdAt: firstBankAccount?.bankDataAux?.createdAt
      ? moment
          .unix(firstBankAccount?.bankDataAux?.createdAt)
          .utc()
          .format("DD/MM/YYYY")
      : null,
    accountType: firstBankAccount?.type?.toLowerCase() || "checking",
    routingNumber: firstBankAccount?.bankDataAux?.routingNumber || "",
    accountNumber: initialBankAccountNumber,
    notes: firstBankAccount?.notes || "",
  };
 
  const details = [
    { label: "Created", value: firstAccount?.createdAt },
    { label: "Account Type", value: firstAccount?.accountType },
    { label: "Business Name on Account", value: firstAccount?.name },
    { label: "Account Number", value: firstAccount?.accountNumber },
    { label: "Routing Number", value: firstAccount?.routingNumber },
    {
      label: "Bank Name",
      value:
        firstBankAccount?.bankDataAux?.bankName ||
        findBankByRoutingNumber(firstAccount?.routingNumber as string),
    },
  ];
 
  const handleDelete = () => {
    if (!firstAccount?.id) return;
    NiceModal.show(DELETE_CONFIRMATION_MODAL, {
      variant: "bank_account",
      itemName: firstBankAccount?.name,
      deleteHandler: () => {
        handleUpdateBankAccountSettings(
          { linkedAccountID: null },
          async () => {
            queryClient.setQueryData(QKEY_LIST_ALL_BANK_ACCOUNTS, undefined);
            await deleteCompletedSteps({
              stepNames: [
                "business_bank_connect",
                "business_bank_statement",
                "business_bank_confirm",
              ],
            });
            await queryClient.invalidateQueries([
              "onboarding-progress",
              merchantId,
            ]);
            dispatch(
              updateFormData({
                key: "business_bank_connect",
                data: {
                  bankDataAux: {
                    isLinked: false,
                  },
                },
              }),
            );
          },
        );
      },
    });
  };
 
  return (
    <StepViewContainer>
      <StyledBox>
        <Box
          display="flex"
          justifyContent="space-between"
          alignItems="center"
          mb={3}
        >
          <GiveText variant="bodyM" sx={{ display: "block" }}>
            {props?.data?.merchant_info?.merchantName || "-"}
          </GiveText>
          <GiveChip
            label="Linked"
            variant="light"
            color="success"
            sx={{ height: "24px", fontSize: "12px" }}
            size="large"
          />
        </Box>
        <Grid container spacing={2}>
          {details.map((item) => (
            <Item key={item.label} {...item} />
          ))}
          <GiveTooltip
            disableHoverListener={isUpdateAllowed}
            disableFocusListener={isUpdateAllowed}
            title={EDIT_DENY_MESSAGE}
            fluidWidth
          >
            <GiveButton
              onClick={handleDelete}
              label="Remove Bank Account"
              startIcon={<LinkBreakIcon size={18} />}
              variant="outline"
              color="destructive"
              size="medium"
              disabled={!isDeleteAllowed}
              sx={{
                borderRadius: "8px",
                marginLeft: "16px",
                marginTop: "16px",
              }}
            />
          </GiveTooltip>
        </Grid>
      </StyledBox>
    </StepViewContainer>
  );
}
 
export default Linked;
 
const StyledBox = styled(Box)(({ theme }) => ({
  padding: "20px",
  borderRadius: theme.customs?.radius?.medium,
  border: `1px solid ${theme.palette?.border?.primary}`,
}));
 
interface ItemProps {
  label: string;
  value: string | null;
}
 
const Item = ({ label, value }: ItemProps) => (
  <Grid item xs={12} sm={6} sx={{ pb: 1 }}>
    <GiveText
      fontSize={14}
      color="secondary"
      sx={{ display: "block", mb: 0.5 }}
    >
      {label}
    </GiveText>
    <GiveText fontWeight={400} fontSize={14}>
      {value || "-"}
    </GiveText>
  </Grid>
);