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 | 691x 691x 691x 691x 691x 691x 691x 691x 691x 691x 691x 691x 691x | import { useMutation, useQueryClient } from "react-query";
// modal
import { customInstance } from "@services/api";
import { showMessage } from "@common/Toast";
import {
selectMasqueradeModeHistory,
setMasqueradeMode,
} from "@redux/slices/app";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { selectMasquerade } from "@redux/slices/enterprise/merchants";
import NiceModal from "@ebay/nice-modal-react";
import { MERCHANT_PREVIEW_PANEL_MODAL } from "modals/modal_names";
import { Role } from "@hooks/common/useUser/types";
import { getMasqueradeData } from "@hooks/common/masquerade/useLocalMasquerade";
import {
QKEY_LIST_ACQUIRER_MERCHANTS,
QKEY_LIST_ENTERPRISES,
} from "@constants/queryKeys";
import useMasqueradeModal from "./useMasqueradeModal";
type TMasqueradeData = Record<string, any> & {
accID?: number;
name?: string;
imageURL?: string;
origin?: string;
role: Role;
nextRoute: string;
};
export default function useMasquerade() {
const dispatch = useAppDispatch();
const historyControlMode = useAppSelector(selectMasqueradeModeHistory);
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const navigate = useNavigate();
const { skipModal } = useAppSelector(selectMasquerade);
const showMasqueradeModal = useMasqueradeModal();
const queryClient = useQueryClient();
const putMasqueradeMutation = useMutation((id: number) => {
return customInstance({
url: `masquerade/${id}`,
method: "PUT",
});
});
const deleteMasqueradeMutation = useMutation((data: any) => {
return customInstance({
url: "masquerade",
method: "DELETE",
}).then(() => {
return historyControlMode;
});
});
const masqueradeHandler = (
origin: "provider" | "acquirer",
rowData?: TMasqueradeData,
onSuccess?: () => void,
) => {
const data = rowData || ({} as TMasqueradeData);
if (anchorEl) setAnchorEl(null);
if (!data?.accID) return;
putMasqueradeMutation.mutate(data.accID, {
onError: (error: any) => {
showMessage("Error", `${error.response?.data?.message}`);
},
onSuccess: async (res: any) => {
/*
We need to remove completelly the sidepanel from tree when going on control-mode
If we keep it, then the sidepanel, mounted by the previous screen (example provider list on acquirer),
will get rendered by the new screen (example merchant list in provider) on the acquirer ID and we get 403 permission error
*/
NiceModal.remove(MERCHANT_PREVIEW_PANEL_MODAL);
dispatch(
setMasqueradeMode({
id: data?.accID || 0,
name: data?.name || "",
origin: data?.origin ?? `/${origin}/merchants`,
imgSrc: data?.imageURL || "",
masqueradeUserRole: data?.role ?? Role.MERCHANT,
historyControlMode: historyControlMode?.concat({
origin: data?.origin ?? `/${origin}/merchants`,
id: data?.accID,
role: data?.role ?? Role.MERCHANT,
}),
firstName: data?.owner?.firstName,
}),
);
navigate(data.nextRoute, {
state: {
status: data?.sponsorStatusName || data?.providerStatus,
paymentCount: data?.hasCreatedProducts || 0,
},
});
if (!data.nextRoute.includes("/provider")) {
await queryClient.invalidateQueries([
"get-campaign-stats",
data.accID,
]);
await queryClient.invalidateQueries("list-all-payment-forms");
}
if (!skipModal) {
showMasqueradeModal(data?.name);
}
if (onSuccess) onSuccess();
},
});
};
const resetMasqueradeMode = (origin = "") => {
dispatch(
setMasqueradeMode({
id: "",
name: "",
origin,
imgSrc: "",
masqueradeUserRole: "",
historyControlMode: [],
}),
);
};
const clearMasquerade = async (origin = "") => {
const storageMasquerade = getMasqueradeData();
if (storageMasquerade?.id) {
// BE may return 401/403 if session changed or permissions differ; we still need to clear local state
try {
await deleteMasqueradeMutation.mutateAsync({});
} catch (e) {
// swallow and continue clearing client-side masquerade state
}
}
queryClient.removeQueries(QKEY_LIST_ENTERPRISES);
queryClient.removeQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
resetMasqueradeMode(origin);
};
return {
putMasqueradeMutation,
deleteMasqueradeMutation,
masqueradeHandler,
clearMasquerade,
resetMasqueradeMode,
};
}
|