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 | 30x 126x 126x 126x 126x 126x 126x 126x 35x 126x 126x 126x 126x 126x 1x 1x 126x 126x 126x 126x 126x | import React, { useMemo } from "react";
import { SxProps } from "@mui/material";
import NiceModal from "@ebay/nice-modal-react";
import { EDIT_DENY_MESSAGE } from "@constants/permissions";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { checkPortals } from "@utils/routing";
import {
IMerchantBankAccount,
TMerchantBankAccountSettings,
} from "@components/Merchants/MerchantPreview/data.types";
import GiveEditBankAccountModal from "@components/Merchants/MerchantPreview/modals/GiveEditBankAccountModal";
import GiveConfigureBankAccountModal from "@components/Merchants/MerchantPreview/modals/GiveConfigureBankAccountModal";
import useStatusChangeBankAccount from "./useStatusChangeBankAccount";
import { useBankAccountPermissions } from "@features/MerchantPortal/ManageMoney/hooks/useBankAccountPermissions";
export const useBankAccount = (
bankAccounts: IMerchantBankAccount[],
merchantId?: number,
parentAccID?: number,
bankAccountSettings?: TMerchantBankAccountSettings,
onCloseModal?: (data?: IMerchantBankAccount, id?: number) => void,
isProvider?: boolean,
) => {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const [selectedRowID, setSelectedRowID] = React.useState<number | null>(null);
const { isAcquirerEnterprises } = checkPortals();
// Use passed isProvider prop, fallback to portal detection
const shouldUseProviderAccounts = isProvider ?? isAcquirerEnterprises;
const { isAddAllowed, isDeleteAllowed, isUpdateAllowed } =
useBankAccountPermissions({ isEnterprise: shouldUseProviderAccounts });
const { merchant_underwriting } = useEnterprisePermissions();
const bankAccount = useMemo(
() => bankAccounts?.find((b) => b.id === selectedRowID),
[selectedRowID, bankAccounts],
);
const isLinkedBASelected =
bankAccount?.id === bankAccountSettings?.linkedAccountID;
const { handleDelete, handleApprove, handleDecline, handleRequestUpdate } =
useStatusChangeBankAccount({
merchantId,
selectedRowID,
bankName: bankAccount?.name,
isLinkedBASelected,
onCloseModal,
});
const onOpenMenu = (event: React.MouseEvent<HTMLElement>, id: number) => {
setSelectedRowID(id);
setAnchorEl(event.currentTarget);
};
const onCloseMenu = () => {
setAnchorEl(null);
};
const handleEdit = (account?: IMerchantBankAccount) => {
Iif (!selectedRowID && !account?.id) return;
NiceModal.show(GiveEditBankAccountModal as any, {
merchantId,
data: account?.id ? account : bankAccount,
bankAccountId: account?.id ? account.id : selectedRowID,
onClose: onCloseModal,
parentAccID,
isLinkedBA: account?.id ? account.isLinked : isLinkedBASelected,
isEnterprise: shouldUseProviderAccounts,
allowTransfers: bankAccountSettings?.allowTransfers,
allowBankAccounts: bankAccountSettings?.allowBankAccounts,
});
};
const manageBankAccountsHandler = () => {
NiceModal.show(GiveConfigureBankAccountModal, {
parentAccID,
merchantId,
data: bankAccountSettings,
});
};
const handleAddBankAccount = () => {
if (!isAddAllowed) return;
NiceModal.show(GiveEditBankAccountModal, {
merchantId,
onClose: onCloseModal,
isDefault: bankAccounts.length === 0,
parentAccID,
isLinkedBA: bankAccountSettings?.isLinkAccount,
isEnterprise: shouldUseProviderAccounts,
allowTransfers: bankAccountSettings?.allowTransfers,
allowBankAccounts: bankAccountSettings?.allowBankAccounts,
});
};
const handleEditOption = () => {
if (!selectedRowID) return;
handleEdit();
};
const options: {
text: string;
hidden: boolean;
onClick: () => void;
disabled?: boolean;
sx?: SxProps;
tooltipTitle?: string;
disableTooltip?: boolean;
type?: "default" | "destructive" | "label" | "divider";
}[] = [
{
text: "Request an update",
hidden: !merchantId || !merchant_underwriting || isLinkedBASelected,
disabled:
["upload", "update_requested"].includes(bankAccount?.status || "") ||
!isUpdateAllowed,
onClick: handleRequestUpdate,
tooltipTitle: EDIT_DENY_MESSAGE,
disableTooltip: isUpdateAllowed,
},
{
text: "Approve",
hidden: !merchantId || !merchant_underwriting || isLinkedBASelected,
disabled: bankAccount?.status === "approved" || !isUpdateAllowed,
onClick: handleApprove,
tooltipTitle: EDIT_DENY_MESSAGE,
disableTooltip: isUpdateAllowed,
},
{
text: "Decline",
hidden: !merchantId || !merchant_underwriting || isLinkedBASelected,
disabled:
bankAccount?.status === "approved" ||
bankAccount?.status === "declined" ||
!isUpdateAllowed,
onClick: handleDecline,
tooltipTitle: EDIT_DENY_MESSAGE,
disableTooltip: isUpdateAllowed,
},
{
text: `Edit${isLinkedBASelected ? " Link Bank Account" : ""}`,
hidden: false,
disabled: !isUpdateAllowed,
onClick: handleEditOption,
tooltipTitle: EDIT_DENY_MESSAGE,
disableTooltip: isUpdateAllowed,
},
{
text: isLinkedBASelected ? "Remove Bank Account" : "Delete",
hidden: false,
disabled: !isDeleteAllowed,
onClick: handleDelete,
type: "destructive",
tooltipTitle: EDIT_DENY_MESSAGE,
disableTooltip: isUpdateAllowed,
},
];
return {
anchorEl,
onOpenMenu,
onCloseMenu,
options,
manageBankAccountsHandler,
handleAddBankAccount,
isAddBankAccountAllowed: isAddAllowed,
handleEdit,
isUpdateAllowed,
};
};
|