All files / src/hooks/merchant-api/manage-money useListBankAccounts.ts

77.77% Statements 14/18
78.94% Branches 15/19
42.85% Functions 3/7
82.35% Lines 14/17

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                                                                                                                                                          14x 14x 14x                       14x 14x 14x   14x             2x             14x 14x   14x             14x   9x           14x                                        
import React, { useMemo, useRef, useState } from "react";
import { useGetBankAccounts } from "@services/api/products/manage-money";
import { TagType } from "@common/Tag";
import { ROWS_PER_PAGE, usePagination } from "@hooks/common/usePagination";
import { useCachedList } from "@hooks/common/useCachedList";
import { detectMobile } from "@utils/index";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useAppSelector } from "@redux/hooks";
import { sorting as sortingReducer } from "@redux/slices/fundraisers";
import { useAccessControl } from "features/Permissions/AccessControl";
 
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { QKEY_LIST_ALL_BANK_ACCOUNTS } from "@constants/queryKeys";
import { useMerchantBankAccountsParser } from "@components/Merchants/MerchantPreview/helpers/parsers";
 
export type BankAccount = {
  id: string;
  name: string;
  bank: string;
  accountName: string;
  status: TagType;
  statusDescription: string;
  bankName: string;
  isDefault: boolean;
  nickname: string;
  notes: string;
  numberLast4: string;
  routingNumber: string;
  type: string;
  authPlaidInfo: string;
  createdWithPlaid?: boolean;
  isLinked?: boolean;
  enterprise?: boolean;
  approvedByID: number;
  approvedByName: string;
  approvedByEmail: string;
  approvedAt: number | null;
  declinedByID: number;
  declinedByName: string;
  declinedByEmail: string;
  declinedAt: number | null;
  createdAt: number;
  updatedAt: number;
  deletedAt: number | null;
};
 
export type NewBankAccount = {
  id: string;
  type: string;
  status: string;
  statusDisplayName: string;
  name: string;
  nickname: string;
  bankName: string;
  notes: string;
  numberLast4: string;
  routingNumber: string;
  isDefault: boolean;
  createdWithPlaid: boolean;
  statementURL: string;
  authPlaidInfo: any;
  identityPlaidInfo: any;
  isLinked: boolean;
  approvedByID: number;
  approvedByName: string;
  approvedByEmail: string;
  approvedAt: number | null;
  declinedByID: number;
  declinedByName: string;
  declinedByEmail: string;
  declinedAt: number | null;
  createdAt: number | null;
  updatedAt: number | null;
  deletedAt: number | null;
};
 
function useListBankAccounts(useMobileData = true) {
  const loadingRef = useRef<boolean>(false);
  const { merchantId } = useGetCurrentMerchantId();
  const canCreateBA = useAccessControl({
    resource: RESOURCE_BASE.BANK_ACCOUNT,
    operation: OPERATIONS.CREATE,
    withPortal: true,
  });
 
  /*
        Some lists are inside a modal, therefore, is very likely the the modal
        is inside a page that also has a list. Because the sort is handle only once
        in redux, we want to avoid rerenders in the main page when a sort is 
        applyed to the secondary list
      */
  const { page, setPage } = usePagination(0, "");
  const sorting = useAppSelector(sortingReducer);
  const { merchantBankAccountsParser } = useMerchantBankAccountsParser();
 
  const { data, isLoading, refetch, isRefetching } = useGetBankAccounts(
    { page, sorting, merchantId },
    {
      refetchOnWindowFocus: false,
      enabled: Boolean(merchantId),
 
      onSuccess(_data) {
        setTimeout(() => {
          loadingRef.current = false;
        }, 700);
      },
    },
  );
 
  const { allData } = useCachedList(QKEY_LIST_ALL_BANK_ACCOUNTS, false, page);
  const usedData = detectMobile() && useMobileData ? allData : data?.data ?? [];
 
  const handlePageChange = (
    event: React.ChangeEvent<unknown>,
    value: number,
  ) => {
    setPage(value);
  };
 
  const formatedRows = useMemo(
    () =>
      usedData.length
        ? merchantBankAccountsParser(usedData as BankAccount[])
        : [],
    [usedData],
  );
 
  return {
    allRows: formatedRows,
    currentPageRows: formatedRows,
    handlePageChange,
    totalRows: data?.total ?? 0,
    page,
    setPage: () => setPage((current) => current + 1),
    loadingRef,
    rowsPerPage: ROWS_PER_PAGE,
    isLoading:
      isLoading ||
      isRefetching ||
      (formatedRows.length === 0 && (data?.total ?? 0) > 0),
    sorting,
    refetch,
    isAllowedAddAccounts: data?.isAllowedAddAccounts && canCreateBA,
  };
}
 
export default useListBankAccounts;