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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | 109x 89x 109x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 37x 37x 37x 37x 505x 46x 39x 46x 521x 521x 521x 521x 10x 10x 10x 10x 521x 37x 37x 646x 521x 37x 37x 37x 37x 37x 37x 726x 521x 1x 1x 1x 1149x 521x 30x 30x 30x 852x 521x 521x 5x 5x 5x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 256x 256x 256x 150x 256x 521x 256x 521x 71x 521x | import { customInstance } from "@services/api";
import { useEffect, useMemo } from "react";
import { useQuery, useQueryClient } from "react-query";
import { updatePermissions } from "@redux/slices/app";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { useGetCurrentMerchantId } from "@hooks/common";
import { TMerchantDoublePanel } from "./types";
import { checkPortals } from "@utils/routing";
import {
PROVIDER_PANEL_KEYS,
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS,
GENERAL_STALE_TIME_15,
} from "features/Merchants/MerchantSidePanel/constants";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { QKEY_GET_MERCHANT_MSP_STATUS } from "@constants/queryKeys";
import { selectMerchantTab } from "@redux/slices/enterprise/merchants";
import { TabNames } from "@features/Merchants/MerchantsTable/hooks/useTabs";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { useUnderwriterPermissions } from "@features/Permissions/AccessControl/hooks";
import { useMerchantDataParser } from "../helpers/parsers";
import { useGetLinkedLegalEntity } from "@features/Merchants/MerchantSidePanel/GiveMerchantFile/hooks/useGetLinkedLegalEntity";
import { useMerchantFilesQuery } from "./useMerchantFilesQuery";
import { useGetConversationStats } from "@features/GiveConversation/hooks/useManageApi";
import { useConversationPermission } from "@features/GiveConversation/hooks/useConversationaPermission";
type Props = {
id: number;
isEnterprise?: boolean;
doublePanel?: TMerchantDoublePanel;
isMerchantFileOpen?: boolean;
isAgreementOpen?: boolean;
refetchOnMount?: boolean;
enabled?: boolean;
};
const setPermissionError = (
promise: PromiseSettledResult<any>,
permissionKey: string,
dispatch: any,
) => {
Iif (promise?.status === "rejected" && promise?.reason?.not_authorized) {
dispatch(updatePermissions({ [permissionKey]: true }));
}
};
export const useMerchantDataPreview = ({
id,
isEnterprise,
doublePanel,
isMerchantFileOpen,
isAgreementOpen,
refetchOnMount = false,
enabled = true,
}: Props) => {
const dispatch = useAppDispatch();
const {
isMerchantStatusRefactorEnabled,
isNewApprovalFlowEnabled,
isNewConversationsEnabled,
} = useGetFeatureFlagValues();
const { merchantId: accountID } = useGetCurrentMerchantId();
const queryClient = useQueryClient();
const { isEnterprisePortal, isAcquirerPortal } = checkPortals();
const tab = useAppSelector(selectMerchantTab);
const { merchant_underwriting, modify_merchant } = useEnterprisePermissions();
const { isUpdateUnderwriterAllowed } = useUnderwriterPermissions();
const { merchantDataParser } = useMerchantDataParser();
const hasUpdateAssignePermission = isEnterprisePortal
? merchant_underwriting && isUpdateUnderwriterAllowed
: isUpdateUnderwriterAllowed;
const canFetchFiles = !isEnterprisePortal || modify_merchant;
// Base merchant data query
const merchantQuery = useQuery(
[MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET, id],
async () => {
const response = await customInstance({ url: `merchants/${id}` });
const promise = {
status: "fulfilled",
value: response,
} as PromiseSettledResult<any>;
setPermissionError(promise, "merchant_data", dispatch);
return response;
},
{
refetchOnWindowFocus: false,
refetchOnMount,
enabled: Boolean(id) && enabled,
staleTime: GENERAL_STALE_TIME_15,
// 🔧 FIX: Add stale-while-revalidate for instant panel rendering
placeholderData: (prev: unknown) => prev,
onSuccess: (data: any) => {
if (
data?.onbSponsorStateName === "approved" &&
data?.onbStateName !== "suspended" &&
!data?.preventOtherRefetch
) {
queryClient.invalidateQueries(
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_CHANGELOG,
);
}
Iif (isEnterprise && !isEnterprisePortal && !data?.preventOtherRefetch)
queryClient.invalidateQueries(PROVIDER_PANEL_KEYS.CHALLENGE_STATS);
},
},
);
const isPendingMerchant = merchantQuery?.data?.onbStateName === "onboarding";
const isProviderType = merchantQuery.data?.type === "enterprise";
//if there's no merchant file, it means the prop is undefined (old panel & provider) so we need to fetch it all together
const skipUseExtraCheck =
isMerchantFileOpen === undefined || isPendingMerchant;
// Bank accounts query
const bankAccountsQuery = useQuery(
[MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_BANK_ACCOUNTS, id],
async () => {
const response = await customInstance({
url: `/merchants/${id}/bank-accounts`,
});
const promise = {
status: "fulfilled",
value: response,
} as PromiseSettledResult<any>;
setPermissionError(promise, "bank_accounts_list", dispatch);
return response?.data || [];
},
{
refetchOnWindowFocus: false,
refetchOnMount,
enabled:
Boolean(id) && (skipUseExtraCheck || isMerchantFileOpen) && enabled,
staleTime: GENERAL_STALE_TIME_15,
},
);
interface AcquirerMSAResponse {
enterpriseMSAVersion?: string;
submerchantMSAVersion?: string;
updatedAt?: string;
}
const acquirerQuery: any = useQuery(
[MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_ACQUIRER, id],
async () => {
const response = (await customInstance({
url: `/merchants/${id}/msa-version`,
})) as AcquirerMSAResponse;
// Note: isProviderType is derived from merchantQuery.data, but we return both versions
// The parser will select the correct one based on merchant type
return {
enterpriseMSAVersion: response?.enterpriseMSAVersion,
submerchantMSAVersion: response?.submerchantMSAVersion,
msaLastUpdatedAt: response?.updatedAt,
};
},
{
refetchOnWindowFocus: false,
refetchOnMount,
staleTime: GENERAL_STALE_TIME_15,
placeholderData: (prev: unknown) => prev, // Show stale data immediately
enabled: Boolean(id && enabled),
},
);
// Stats or Enterprise query based on conditions
const statsOrEnterpriseQuery = useQuery(
[
isEnterprise && !isEnterprisePortal
? PROVIDER_PANEL_KEYS.GET
: MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_STATS,
id,
],
async () => {
const url =
isEnterprise && !isEnterprisePortal
? `/enterprises?filter=accID:${id}`
: `/merchants/${id}/stats-base`;
const response = await customInstance({ url });
Eif (!isEnterprise && !isEnterprisePortal) {
const promise = {
status: "fulfilled",
value: response,
} as PromiseSettledResult<any>;
setPermissionError(promise, "merchant_stats", dispatch);
}
return response;
},
{
refetchOnWindowFocus: false,
refetchOnMount: false,
enabled:
isEnterprise && !isEnterprisePortal
? Boolean(id) && enabled
: Boolean(id) &&
![TabNames.kyb, TabNames.underwriting, TabNames.sponsor].includes(
tab,
) &&
enabled,
staleTime: GENERAL_STALE_TIME_15,
// 🔧 FIX: Add stale-while-revalidate
placeholderData: (prev: unknown) => prev,
},
);
//Parent query
const parentQuery = useQuery(
[MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_PARENT, id],
async () => {
const url = `merchants/${accountID}`;
const response = await customInstance({ url });
return response;
},
{
refetchOnWindowFocus: false,
refetchOnMount: false,
// 🔧 FIX: Use stale-while-revalidate instead of Infinity
staleTime: GENERAL_STALE_TIME_15,
placeholderData: (prev: unknown) => prev,
enabled: Boolean(id && (isEnterprise || isProviderType) && enabled),
},
);
// Assignees query based on conditions
const assigneesQuery = useQuery(
[MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_ASSIGNEES, id],
async () => {
const url = `merchants/${id}/assignees`;
const response = await customInstance({ url });
return response;
},
{
refetchOnWindowFocus: false,
refetchOnMount: false,
// 🔧 FIX: Use stale-while-revalidate instead of Infinity to keep assignees updated
staleTime: GENERAL_STALE_TIME_15,
placeholderData: (prev: unknown) => prev,
enabled: Boolean(
id &&
isNewApprovalFlowEnabled &&
!isEnterprise &&
hasUpdateAssignePermission &&
enabled,
),
},
);
// Legal Entity query
// 🔧 FIX: Remove waterfall - derive legalEntityID from merchant query data
const legalEntityID = merchantQuery.data?.legalEntityID;
const legalEntityQuery = useQuery(
[MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY, id, legalEntityID],
async () => {
const response = await customInstance({
url: `merchants/${id}/legal-entities/${legalEntityID}`,
});
return response;
},
{
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: GENERAL_STALE_TIME_15,
enabled:
Boolean(id && legalEntityID) &&
enabled &&
(skipUseExtraCheck || isMerchantFileOpen || isAgreementOpen),
onSuccess: (data) => {
Iif (
doublePanel === "manageUnderwriting" &&
isEnterprise &&
!isEnterprisePortal
) {
queryClient.invalidateQueries(PROVIDER_PANEL_KEYS.GET_CHALLENGES);
}
},
},
);
const { documentsQuery, loadNextFilesPage } = useMerchantFilesQuery({
id,
setPermissionError,
refetchOnMount,
enabled:
Boolean(id) &&
(skipUseExtraCheck || isMerchantFileOpen) &&
enabled &&
canFetchFiles,
});
// Pending Legal Entity Query to get LE email. TODO: remove this query once BE makes the fix to return email
// 🔧 FIX: Derive from merchant query data to avoid waterfall
const {
pendingLegalEntityID,
pendingLegalEntityStatus,
onbSectionName,
processorName,
processorSupportStatusName,
} = merchantQuery.data || {};
const pendingLegalEntityQuery = useGetLinkedLegalEntity({
merchantID: id,
pendingLegalEntityID: pendingLegalEntityID,
enabled:
Boolean(
id && pendingLegalEntityID && pendingLegalEntityStatus === "pending",
) &&
enabled &&
(skipUseExtraCheck || isMerchantFileOpen),
});
const { hasAccessToViewConversation } = useConversationPermission();
const conversationStatsQuery = useGetConversationStats({
merchantID: id,
options: {
enabled: isNewConversationsEnabled && hasAccessToViewConversation,
}, //only fetch user has permission, otherwise there's no conversation anyway
});
// MSP Status query
// 🔧 FIX: Derive from merchant query data to avoid waterfall
const isMSPRequestEnabled =
onbSectionName === "sponsor" &&
processorName &&
(processorName === "rs2_ches"
? processorSupportStatusName === "enabled"
: true);
const mspStatusQuery = useQuery(
[QKEY_GET_MERCHANT_MSP_STATUS, id, isMSPRequestEnabled],
async () =>
await customInstance({
url: `/merchants/${id}/msp/is-ready`,
method: "GET",
}),
{
enabled: Boolean(id && isMSPRequestEnabled && enabled),
staleTime: GENERAL_STALE_TIME_15,
},
);
const linkedBankAccount = merchantQuery.data?.linkedBankAccount
? [
{
...merchantQuery.data.linkedBankAccount,
enterprise: true,
isLinked: true,
},
]
: [];
// 🔧 FIX: Only block panel on critical merchant query
// Other queries load independently and their sections handle their own loading states
const isLoading = merchantQuery.isLoading;
const isMerchantFileLoading =
bankAccountsQuery.isLoading ||
documentsQuery.isLoading ||
legalEntityQuery.isLoading ||
pendingLegalEntityQuery.isLoading;
const isRefetching =
merchantQuery.isRefetching ||
bankAccountsQuery.isRefetching ||
documentsQuery.isRefetching ||
acquirerQuery.isRefetching ||
statsOrEnterpriseQuery.isRefetching ||
assigneesQuery.isRefetching ||
parentQuery.isRefetching ||
legalEntityQuery.isRefetching ||
mspStatusQuery.isRefetching ||
pendingLegalEntityQuery.isRefetching ||
conversationStatsQuery.isRefetching;
// Combine all query results to be passed down to the parser
// 🔧 FIX: Allow partial data - sections handle their own loading/error states
const combinedData = useMemo(() => {
// Return partial data even if merchant query hasn't completed yet
// This allows individual sections to handle their own loading states
const acquirerData = acquirerQuery.data;
const msaVersion = (() => {
if (!acquirerData) return undefined;
return isEnterprise || isProviderType
? acquirerData.enterpriseMSAVersion
: acquirerData.submerchantMSAVersion;
})();
return {
merchant: merchantQuery.data
? {
...merchantQuery.data,
pendingLegalEntityEmail:
pendingLegalEntityQuery?.data?.controllerOwnerEmail,
}
: undefined,
aquirerTcVersion: acquirerData
? {
msaVersion,
msaLastUpdatedAt: acquirerData.msaLastUpdatedAt,
}
: undefined,
legalEntity: legalEntityQuery.data,
bankAccounts: bankAccountsQuery.data,
parent: parentQuery.data || null,
stats: isEnterprise
? statsOrEnterpriseQuery.data?.data?.[0]
: statsOrEnterpriseQuery.data,
documents: documentsQuery.data,
principals: legalEntityQuery.data?.principals || [],
merchantAssignees:
isNewApprovalFlowEnabled && !isEnterprise ? assigneesQuery.data : null,
conversationStats: conversationStatsQuery.data,
};
}, [
isLoading,
isMerchantFileLoading,
isRefetching,
merchantQuery.data,
acquirerQuery.data,
statsOrEnterpriseQuery.data,
assigneesQuery.data,
parentQuery.data,
documentsQuery.data,
conversationStatsQuery.data,
isEnterprise,
isProviderType,
isNewApprovalFlowEnabled,
pendingLegalEntityQuery.data,
legalEntityQuery.data,
]); //we need to re-calculate the combined data after each query completes
const customData = useMemo(() => {
return merchantDataParser(
{
...combinedData,
mspStatus: mspStatusQuery.data,
bankAccounts: [...(bankAccountsQuery.data || []), ...linkedBankAccount],
},
accountID,
true,
isMerchantStatusRefactorEnabled && !isEnterprise,
);
}, [combinedData]); //combinedData already includes the logic to recalculate after loading or refetch, no need to pass down anything else
// fees config in merchant response for acquirer portal is not the same as we have it on provider portal
useEffect(() => {
Iif (merchantQuery.isSuccess) merchantQuery.refetch();
}, [isAcquirerPortal, isEnterprisePortal]);
return {
data: customData,
merchant: merchantQuery.data,
isMerchantLoading: merchantQuery.isLoading,
isLoading: isEnterprise ? isLoading || isMerchantFileLoading : isLoading,
isMerchantFileLoading,
isRefetching,
queries: {
merchantQuery,
bankAccountsQuery,
legalEntityQuery,
documentsQuery,
acquirerQuery,
statsOrEnterpriseQuery,
assigneesQuery,
parentQuery,
mspStatusQuery,
pendingLegalEntityQuery,
conversationStatsQuery,
},
loadNextFilesPage,
};
};
|