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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 6x 2x 2x 2x 6x 2x 6x 1x 1x 6x 2x 6x | import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { useAppDispatch } from "@redux/hooks";
import { resetAllFilters } from "@redux/slices/dynamicFilterSlice";
import {
BANK_ACCOUNTS_MODAL_V2,
GIVE_CONFIRMATION_POP_UP,
GIVE_CREATE_MERCHANT_AGREEMENT_MODAL,
GIVE_MERCHANT_AGREEMENT_MODAL,
MERCHANT_PREVIEW_PANEL_MODAL,
NOTIFICATION_SIDE_PANEL, // TODO REMOVE THIS
PROFILE_MODAL,
} from "modals/modal_names";
import { useEffect, useRef } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { useGetCurrentMerchantId } from "./common";
import { useAccountSelection } from "@pages/Login/modals/hooks/useAccountSelectionModal";
import { urlRoleMap } from "@constants/routing";
import { Role } from "./common/useUser/types";
import useOpenGiveNotification from "@features/GiveNotificationModal/hooks/useOpenGiveNotification";
import { ENTERPRISE_PATHS, REDIRECT_PATHS } from "@routes/paths";
import useRedirect from "./common/router/useRedirect";
import useAccountSwitcher from "./useAccountSwitcher";
import { useQueryParams } from "./useQueryParams";
import { QUERY_PARAM_KEYS } from "@constants/queryParamKeys";
import { checkPortals } from "@utils/routing";
const QUERY_PARAMS = [
QUERY_PARAM_KEYS.notificationMessageID,
QUERY_PARAM_KEYS.merchantID,
QUERY_PARAM_KEYS.accountID,
QUERY_PARAM_KEYS.parentID,
QUERY_PARAM_KEYS.bankAccountID,
QUERY_PARAM_KEYS.agreement,
];
const getRedirectUrl = ({
isTransfersPage,
isManageMoneyPage,
}: {
isTransfersPage: boolean;
isManageMoneyPage: boolean;
}) => {
if (isTransfersPage) return ENTERPRISE_PATHS.TRANSFERS;
if (isManageMoneyPage) return ENTERPRISE_PATHS.MANAGE_MONEY;
return undefined;
};
export default function useManageQueryRedirect() {
const location = useLocation();
const dispatch = useAppDispatch();
const { handleOpenConversation } = useOpenGiveNotification();
const navigate = useNavigate();
const { merchantId, selectedUser } = useGetCurrentMerchantId();
const { accountList } = useAccountSelection();
const { hide } = useModal(GIVE_CONFIRMATION_POP_UP);
const didRedirectRef = useRef(false);
const {
isAnyTransfersPage: isTransfersPage,
isManageMoney: isManageMoneyPage,
isEnterprisePortal,
} = checkPortals();
const isWelcomePage = location.pathname.includes("merchant/welcome");
const isSwitchAccountPage =
isWelcomePage || isTransfersPage || isManageMoneyPage;
const isNewBankAccountPage = location.pathname.includes(
REDIRECT_PATHS.NEW_BANK_ACCOUNT,
);
const { handleAccountSwitch, findAccountIndex } = useAccountSwitcher();
const {
queryParams,
getMany: getManyQueryParams,
get: getQueryParam,
remove: removeQueryParams,
set: setQueryParam,
} = useQueryParams();
const {
[QUERY_PARAM_KEYS.notificationMessageID]: notificationMessageID,
[QUERY_PARAM_KEYS.merchantID]: accountIDTemp1,
[QUERY_PARAM_KEYS.accountID]: accountIDTemp2,
[QUERY_PARAM_KEYS.parentID]: parentID,
[QUERY_PARAM_KEYS.bankAccountID]: bankAccountID,
[QUERY_PARAM_KEYS.agreement]: agreement,
} = getManyQueryParams(QUERY_PARAMS);
const redirect = (url: string, keepQueryParams = true) => {
navigate(
{
pathname: url,
search: keepQueryParams ? queryParams.toString() : "",
},
{ replace: true },
);
didRedirectRef.current = true;
};
const onRedirectCancel = () => {
redirect("/", false);
};
const accountID = accountIDTemp1 || accountIDTemp2;
const { redirectToModal } = useRedirect();
useEffect(() => {
Eif (isNewBankAccountPage || accountList.length < 1) return; //there has to be at least one account available
const isMerchantDifferent =
accountID && merchantId && `${merchantId}` !== accountID;
if (
isMerchantDifferent &&
notificationMessageID &&
didRedirectRef.current === false //to prevent reopening the modal after redirection
) {
handleAccountSwitch(accountID, { notificationMessageID });
const selectedUserRole = selectedUser?.merchType as Role;
redirect(`/${selectedUserRole}/${urlRoleMap[selectedUserRole]}`);
return;
}
if (isSwitchAccountPage && accountID) {
const targetIndex = findAccountIndex(accountID);
const targetMerchant = accountList[targetIndex];
const shouldSwitch = isMerchantDifferent && !targetMerchant?.selected;
if (shouldSwitch) {
handleAccountSwitch(
accountID,
{
showToast: true,
redirectUrl: getRedirectUrl({
isTransfersPage,
isManageMoneyPage,
}),
},
onRedirectCancel,
);
} else {
removeQueryParams([
QUERY_PARAM_KEYS.merchantID,
QUERY_PARAM_KEYS.accountID,
]);
}
}
if (notificationMessageID && !isMerchantDifferent) {
const initialThreadId = Number(notificationMessageID);
handleOpenConversation({ initialTab: "messages", initialThreadId });
removeQueryParams([QUERY_PARAM_KEYS.notificationMessageID]);
}
}, [
notificationMessageID,
accountID,
isSwitchAccountPage,
accountList, //accountList has to be fetched
]);
//handle the redirection to Account modal, security tab from email
useEffect(() => {
Iif (!merchantId) return;
const accountSecurity = getQueryParam(QUERY_PARAM_KEYS.accountSecurity);
Iif (accountSecurity === "true") {
NiceModal.show(PROFILE_MODAL, {
initialTab: "Security",
});
removeQueryParams([QUERY_PARAM_KEYS.accountSecurity]);
}
}, [location.search, merchantId]);
//handle the redirection to agreement modal from email
useEffect(() => {
Eif (!merchantId || !agreement) return;
const isMerchantDifferent =
accountID && merchantId && `${merchantId}` !== accountID;
if (agreement === "true") {
if (isMerchantDifferent) {
handleAccountSwitch(
accountID,
{
showToast: true,
redirectUrl: getRedirectUrl({
isTransfersPage,
isManageMoneyPage,
}),
},
onRedirectCancel,
);
return;
}
const modal = isEnterprisePortal
? GIVE_CREATE_MERCHANT_AGREEMENT_MODAL
: GIVE_MERCHANT_AGREEMENT_MODAL;
NiceModal.show(modal, {
merchantId,
});
removeQueryParams([
QUERY_PARAM_KEYS.agreement,
QUERY_PARAM_KEYS.merchantID,
]);
}
}, [location.search, merchantId]);
useEffect(() => {
Eif (
accountList.length < 1 ||
!isNewBankAccountPage ||
!bankAccountID ||
!accountID ||
!merchantId ||
didRedirectRef.current
)
return;
const targetAccountID = parentID ?? accountID;
const emailReceiverIndex = findAccountIndex(targetAccountID);
const emailReceiver = accountList[emailReceiverIndex];
const isMerchantDifferent = `${merchantId}` !== targetAccountID;
const isTargetProvider = ["provider", "enterprise"].includes(
emailReceiver?.merchType,
);
/** Helper: Open the correct modal or redirect */
const handleShowModalOrPreview = () => {
hide();
const selectedUserRole = selectedUser?.merchType as Role;
const shouldOpenBankAccountModal = accountID === targetAccountID;
if (!shouldOpenBankAccountModal && selectedUserRole === "merchant") {
redirect("/", false);
return;
}
if (shouldOpenBankAccountModal) {
NiceModal.show(BANK_ACCOUNTS_MODAL_V2, {
bankAccountID: Number(bankAccountID),
});
} else {
redirectToModal(MERCHANT_PREVIEW_PANEL_MODAL, {
id: accountID,
initialContent: "merchantFile",
isProvider: isTargetProvider,
});
}
// --- Step 3: Navigation & query params ---
// If we open the bank account modal, redirect to the bank account page.
// Otherwise, redirect to the merchants page.
const urlRedirect = shouldOpenBankAccountModal
? urlRoleMap[selectedUserRole]
: "merchants";
removeQueryParams(
[
QUERY_PARAM_KEYS.merchantID,
QUERY_PARAM_KEYS.parentID,
QUERY_PARAM_KEYS.bankAccountID,
],
{
sync: false,
},
);
if (bankAccountID && !shouldOpenBankAccountModal) {
// Set this query param to scroll to the bank account section
// and expand the accordion in merchant details.
setQueryParam(QUERY_PARAM_KEYS.scrollToBankAccountID, bankAccountID, {
sync: false,
});
}
redirect(`/${selectedUserRole}/${urlRedirect}`);
};
// --- Main Flow ---
if (isMerchantDifferent) {
handleAccountSwitch(
targetAccountID,
{ showToast: true },
onRedirectCancel,
);
} else {
handleShowModalOrPreview();
}
}, [
isNewBankAccountPage,
merchantId,
accountID,
bankAccountID,
parentID,
accountList,
]);
useEffect(() => {
dispatch(resetAllFilters());
}, [location]);
return;
}
|