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 | 9x 134x 134x 134x 271x 134x 134x 134x 271x 134x 134x 45x 45x 45x 540x 45x 540x 540x 540x 540x 540x 540x 45x 540x 540x 134x 134x 45x 45x 45x 194x 45x 45x 134x 91x 134x 91x 134x 16x 8x 8x 8x 8x 35x 8x 8x 8x 8x 8x 8x 7x 8x 8x 8x 8x 8x 8x 8x 8x 7x 8x 134x 4x 4x 4x 134x | import { useDispatch, useSelector } from "react-redux";
import { useQueryClient } from "react-query";
import { setCurrentSubstep } from "@redux/slices/onboardingWizard";
import { IDirection, IStep } from "../types";
import { SetStateAction, Dispatch, useEffect, useMemo, useRef } from "react";
import { baseSteps } from "../consts";
import { RootState } from "@redux/types/store";
import NiceModal from "@ebay/nice-modal-react";
import { MERCHANT_ONBOARDING_FINISH_MODAL } from "modals/modal_names";
import {
QKEY_GET_MERCHANT_BY_ID,
QKEY_USER_PERMISSIONS,
} from "@constants/queryKeys";
import { useGetCurrentMerchantId } from "@hooks/common";
import { groupStepsByParent } from "../utils";
import { useGetApiData } from "./useGetApiData";
type IHandleNavigation = {
apiData: ReturnType<typeof useGetApiData>;
completedSteps: IStep[];
currentSubstep: IStep;
setIsStarted: Dispatch<SetStateAction<boolean>>;
};
export const useHandleNavigation = ({
apiData,
completedSteps,
currentSubstep,
setIsStarted,
}: IHandleNavigation) => {
const dispatch = useDispatch();
const client = useQueryClient();
const formData = useSelector(
(state: RootState) => state.onboardingWizard.formData,
);
const wasFilledAnExistingTaxID =
formData.business_profile?.businessProfileDataAux
?.wasFilledAnExistingTaxID ??
apiData.data.business_profile?.businessProfileDataAux
?.wasFilledAnExistingTaxID;
const isBankAccountConnectLinked =
formData.business_bank_connect?.bankDataAux?.isLinked ??
apiData.data.business_bank_connect?.bankDataAux?.isLinked;
const hiddenStepKeys = useSelector(
(state: RootState) => state.onboardingWizard.hiddenStepKeys,
);
const { merchantId } = useGetCurrentMerchantId();
const steps = useMemo(() => {
let firstUncompletedFound = false;
const allStepKeys = Object.keys(baseSteps) as IStep[];
const visibleStepKeys = allStepKeys.filter(
(key) => !hiddenStepKeys.has(key),
);
return visibleStepKeys.reduce((acc, key) => {
const completed = completedSteps.includes(key as IStep);
const isLinkedAndwaitForConfirmationBP =
key === "business_profile" && wasFilledAnExistingTaxID;
const isLinkedBankAccount =
key === "business_bank_connect" && isBankAccountConnectLinked;
const isLinkedBPNotNextInLine =
isLinkedAndwaitForConfirmationBP &&
visibleStepKeys.includes("business_address");
const isNextInLine =
!completed &&
!firstUncompletedFound &&
!(isLinkedBPNotNextInLine || isLinkedBankAccount);
if (isNextInLine) {
firstUncompletedFound = true;
}
acc[key as IStep] = {
...baseSteps[key as IStep],
completed: completed,
isNextInLine,
forceOnClick: isLinkedAndwaitForConfirmationBP || isLinkedBankAccount,
};
return acc;
}, {} as Record<IStep, (typeof baseSteps)[IStep] & { completed: boolean; isNextInLine: boolean; forceOnClick: boolean }>);
}, [
completedSteps,
hiddenStepKeys,
wasFilledAnExistingTaxID,
isBankAccountConnectLinked,
]);
const stayInStepRef = useRef(false);
useEffect(() => {
Iif (stayInStepRef.current) {
stayInStepRef.current = false;
return;
}
//On Mount, there is nothing.Once next step in line was decided, we need to set the current substep
Iif (currentSubstep.startsWith("stay_in_")) {
stayInStepRef.current = true;
dispatch(
setCurrentSubstep(currentSubstep.replace("stay_in_", "") as IStep),
);
return;
}
const firstInLine = Object.keys(steps).find(
(s) => steps[s as IStep].isNextInLine,
) as IStep | undefined;
Eif (firstInLine) {
dispatch(setCurrentSubstep(firstInLine));
}
}, [steps]);
const groupedSteps = useMemo(
() => groupStepsByParent(steps, currentSubstep),
[steps, currentSubstep],
);
const currentIndex = useMemo(
() => Object.keys(steps).indexOf(currentSubstep),
[currentSubstep, steps],
);
const handleNavigation = async (direction: IDirection, config: any) => {
if (direction === "start") {
setIsStarted(true);
return;
}
const substepKeys = Object.keys(steps) as IStep[];
const firstInLine = Object.keys(steps).find(
(s) => steps[s as IStep].isNextInLine,
) as IStep | undefined;
const deltaIndex = direction === "back" ? -1 : 1;
const condition =
direction === "back"
? currentIndex > 0
: currentIndex < substepKeys.length - 1;
Eif (condition) {
const logicallyNextInline = substepKeys[currentIndex + deltaIndex];
const nextStepInLine = steps[logicallyNextInline as IStep];
if (deltaIndex === 1) {
client.invalidateQueries(["onboarding-progress"]);
}
//find step imediate after BP
//if back is press and the active step is the one after, navigate to BP
//this logic only if wasFilledAnExistingTaxID is true
const businessProfileStepIndex = substepKeys.indexOf(
"business_profile" as IStep,
);
const stepAfterBp =
businessProfileStepIndex < substepKeys.length - 1
? substepKeys[businessProfileStepIndex + 1]
: undefined;
Iif (wasFilledAnExistingTaxID) {
if (
direction === "back" &&
currentSubstep === stepAfterBp &&
stepAfterBp
) {
dispatch(setCurrentSubstep("business_profile"));
return;
}
}
//same thing, for bank account cconect
const bankAccountConnectStepIndex = substepKeys.indexOf(
"business_bank_connect" as IStep,
);
const stepAfterBAC =
bankAccountConnectStepIndex < substepKeys.length - 1
? substepKeys[bankAccountConnectStepIndex + 1]
: undefined;
Iif (isBankAccountConnectLinked) {
if (
direction === "back" &&
currentSubstep === stepAfterBAC &&
stepAfterBAC
) {
dispatch(setCurrentSubstep("business_bank_connect"));
return;
}
}
Iif (config?.nextStep) {
dispatch(setCurrentSubstep(config.nextStep));
return;
}
// in some cases, like on a linked BP and pending, the step is not completed but we need to proceed further
if (direction === "next") {
Iif (
currentSubstep === "business_profile" ||
currentSubstep === "business_bank_connect"
) {
if (
apiData.data?.business_profile?.businessProfileDataAux
?.wasFilledAnExistingTaxID ||
apiData.data?.business_profile?.businessProfileDataAux
?.showLinkedBusinessProfile ||
apiData.data?.business_bank_connect?.bankDataAux?.isLinked
) {
dispatch(setCurrentSubstep(logicallyNextInline));
return;
}
}
}
dispatch(
setCurrentSubstep(
//navigate one step per time, usually throught competed ones, only when direction is back. On next, navigative to the first uncompleted
nextStepInLine?.completed && direction === "back"
? logicallyNextInline
: firstInLine && logicallyNextInline !== firstInLine
? firstInLine
: logicallyNextInline,
),
);
}
};
// The onHandleLastStepNavigation function is triggered when the user is on the last step of the onboarding process.
// It invalidates queries to refresh merchant data and shows a completion modal if onboarding is finished.
const onHandleLastStepNavigation = () => {
const stepKeys = Object.keys(steps);
const isLastStep = currentIndex === stepKeys.length - 1;
Iif (isLastStep) {
// Check both the apiData and the query cache for the latest onboarding status
const cachedData: any = client.getQueryData([
QKEY_GET_MERCHANT_BY_ID,
merchantId,
]);
const isOnboardingCompleted =
cachedData?.isOnboardingCompleted ||
apiData?.data?.merchant_info?.isOnboardingCompleted;
// If we're on the last step (agreement_sign) and signature is submitted,
// show the finish modal immediately before invalidating queries
// This prevents the onboarding modal from closing before the finish modal shows
if (isOnboardingCompleted || currentSubstep === "agreement_sign") {
NiceModal.show(MERCHANT_ONBOARDING_FINISH_MODAL);
// Invalidate queries after showing the modal to refresh data in the background
// Use setTimeout to ensure modal shows before any potential page reload
setTimeout(() => {
client.invalidateQueries([QKEY_GET_MERCHANT_BY_ID, merchantId]);
client.invalidateQueries(QKEY_USER_PERMISSIONS);
}, 100);
return;
}
}
};
return {
handleNavigation,
onHandleLastStepNavigation,
currentIndex,
groupedSteps,
steps,
};
};
|