All files / src/features/GiveOnboarding index.tsx

97.14% Statements 34/35
90.62% Branches 29/32
87.5% Functions 7/8
97.14% Lines 34/35

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                                                  9x         139x 134x   134x   134x 134x 271x     134x   134x   134x           134x     134x   134x       134x   134x             134x                           134x       134x 45x     134x 43x 43x       134x         134x           134x 134x     134x   134x                                     134x 2x       132x   132x                   1x                         1x                                            
import { Grid } from "@mui/material";
import { resetFormData } from "@redux/slices/onboardingWizard";
import { RootState } from "@redux/types/store";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { MobileHeader } from "./components/MobileHeader";
import { PageLoader } from "./components/PageLoader";
import { baseSteps } from "./consts";
import { Body } from "./container/Body";
import GiveOnboardingModal from "./container/Modal";
import { Navigation } from "./container/Navigation";
import { IStep } from "./types";
import { openLogoutModal } from "./utils";
import { useOnboardingSteps } from "@features/GiveOnboarding/queries";
import IntroContent from "./container/IntroContent";
import { useGetApiData } from "./hooks/useGetApiData";
import { useIdentityOnlyMode } from "./hooks/useIdentityOnlyMode";
import { useWizardStep } from "./hooks/useWizardStep";
import { useHandleNavigation } from "./hooks/useHandleNavigation";
import { useIsMutating } from "react-query";
import { checkPortals } from "@utils/routing";
import NiceModal from "@ebay/nice-modal-react";
import { MERCHANT_ONBOARDING_FINISH_MODAL } from "modals/modal_names";
 
const GiveOnboarding = () => {
  /*
     DO NOT ADD EVER HERE FORMDATA RELATED LOGIC, ESPECIALY THE SELECTOR
  */
 
  const finishModal = NiceModal.useModal(MERCHANT_ONBOARDING_FINISH_MODAL);
  const { isMobileView: mobile, isTabletView } = useCustomThemeV2();
 
  const isMobileOrTabletView = mobile || isTabletView;
 
  const dispatch = useDispatch();
  const currentSubstep = useSelector(
    (state: RootState) => state.onboardingWizard.currentSubstep,
  );
 
  const [isStarted, setIsStarted] = useState(false);
 
  const apiData = useGetApiData({ currentSubstep, isStarted });
 
  const isIdentityOnly = Boolean(
    apiData.data?.merchant_info?.isPahReassignmentIdentityOnly,
  );
  // PAH006 SS-4 follow-up: the PAH has uploaded identity. Same owner-status
  // signal the portal gate (OnboardingStatusWrapper) reads, so the wizard
  // closes exactly when the portal underneath unblocks — never onto a blank.
  const isIdentitySubmitted = Boolean(
    apiData.data?.merchant_info?.isPahReassignmentIdentitySubmitted,
  );
  useIdentityOnlyMode(isIdentityOnly);
 
  const { data: stepsData, isLoading: isStepsLoading } = useOnboardingSteps(
    apiData.data.merchant_info.merchantId,
  );
 
  const { isMerchantPortal } = checkPortals();
 
  const completedSteps: IStep[] = stepsData?.completedSteps ?? [];
 
  const {
    handleNavigation,
    onHandleLastStepNavigation,
    currentIndex,
    groupedSteps,
  } = useHandleNavigation({
    apiData,
    completedSteps,
    currentSubstep,
    setIsStarted,
  });
 
  // check if modal is open. PAH006 SS-4 (AC005): an already-approved merchant
  // (isOnboardingCompleted === true) re-onboarding a reassigned PAH must still
  // open the wizard for the Identity-only flow, so honor isIdentityOnly too.
  // PAH006 SS-4 follow-up: the BE keeps isIdentityOnly true until underwriting
  // approves, but the PAH is done once they upload identity — so close the
  // wizard on submission and let them into the portal directly.
  const isModalOpen =
    (apiData.data?.merchant_info?.isOnboardingCompleted === false ||
      (isIdentityOnly && !isIdentitySubmitted)) &&
    !finishModal.visible;
 
  useEffect(() => {
    setIsStarted(completedSteps.length > 0);
  }, [completedSteps.length]);
 
  useEffect(() => {
    return () => {
      dispatch(resetFormData());
    };
  }, [dispatch, apiData.data.merchant_info.merchantId]);
 
  const { ...wizardStepProps } = useWizardStep({
    handleNavigation,
    onHandleLastStepNavigation,
  });
 
  const mobileHeaderTitle = !isStarted
    ? "Introduction"
    : currentSubstep in baseSteps
    ? baseSteps[currentSubstep].label
    : "";
 
  const isUpdatingAccount = useIsMutating({ mutationKey: ["putAccount"] });
  const isLoadingUpdatingAccount = isUpdatingAccount > 0 && !isMerchantPortal;
 
  const hasLoadedInitialData =
    apiData.data.merchant_info.merchantId !== undefined;
  const isLoading =
    apiData.loadings.merchant_info ||
    apiData.loadings.business_bank_connect ||
    apiData.loadings.business_profile ||
    isStepsLoading ||
    isLoadingUpdatingAccount;
 
  // Only show the full-screen loader until the initial merchant data is in.
  // <PageLoader/> is position:fixed and covers the whole viewport (drawer
  // included), so once we already have the merchant data we must not paint it
  // again — otherwise the still-loading wizard sub-queries (steps/profile) keep
  // it up and flash the whole portal white on entry, even for a merchant whose
  // onboarding is complete (modal stays closed).
  //
  // This guard also can't unmount an already-open modal on a later refetch, but
  // NOT because !hasLoadedInitialData implies the modal is closed — it doesn't
  // (useGetApiData defaults isOnboardingCompleted to false pre-load, so
  // isModalOpen is actually true during the initial fetch). The real reason is
  // that hasLoadedInitialData becomes permanently true once accID is set, so
  // `isLoading && !hasLoadedInitialData` can never fire again after the first load.
  if (isLoading && !hasLoadedInitialData) {
    return <PageLoader />;
  }
 
  const isFormDirty =
    wizardStepProps.stepRef.current?.methods?.formState?.isDirty;
 
  return (
    <GiveOnboardingModal
      isLoading={isStepsLoading}
      open={isModalOpen}
      width={mobile ? "100%" : "70%"}
    >
      <Grid container sx={{ height: "100%" }}>
        {!isMobileOrTabletView && isStarted && (
          <Navigation
            groupedSteps={groupedSteps}
            openLogoutModal={() => openLogoutModal({ isStarted })}
            persistFormData={wizardStepProps.persistFormData}
            isFormDirty={isFormDirty}
          />
        )}
        <Body
          apiData={apiData}
          currentSubstep={currentSubstep}
          currentIndex={currentIndex}
          {...wizardStepProps}
          mainContent={
            !isStarted ? (
              <IntroContent
                openLogoutModal={() => openLogoutModal({ isStarted })}
              />
            ) : null
          }
          topContent={
            isMobileOrTabletView ? (
              <MobileHeader
                title={mobileHeaderTitle}
                openLogoutModal={() => openLogoutModal({ isStarted })}
                groupedSteps={groupedSteps}
                isIntroModalEnabled={!isStarted}
                isFormDirty={isFormDirty}
              />
            ) : null
          }
        />
      </Grid>
    </GiveOnboardingModal>
  );
};
 
export default GiveOnboarding;