All files / src/components/ProfilePage/BusinessProfileSetup BusinessProfileSetup.tsx

100% Statements 34/34
91.17% Branches 31/34
100% Functions 7/7
100% Lines 32/32

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                                                        19x 44x   44x   44x           44x 44x   44x   44x 44x                     44x   44x       44x   3x       3x 1x 1x   2x           44x                                                                           44x                                                                                                   44x           176x                   19x       44x         44x 44x   44x         176x           19x       44x   44x       44x             44x    
import { Stack } from "@mui/material";
import KotoStepper from "@common/SignUp/KotoStepper";
import BusinessDetailsStep from "./BusinessDetailsStep";
import BusinessAddressStep from "./BusinessAddressStep";
import BusinessOwner from "./BusinessOwner";
import { HiddenComponent } from "@containers/HiddenComponent";
import useBusinessProfileSteps from "./hooks/useBusinessProfileSteps";
import { checkPortals } from "@utils/routing";
import EnterpriseInfo from "./EnterpriseInfo";
import { useMemo } from "react";
import { refineData } from "./helpers/refineData";
import useBusinessDetailsData from "./hooks/useBusinessDetailsData";
import {
  TSetFormValues,
  TBusinessStepsCommons,
} from "@components/ProfilePage/BusinessProfileSetupNew/types";
import { generateNameLabelPlaceholder } from "@features/GiveSignup/helpers";
 
type TData = {
  merchant: any;
  legalEntity: any;
};
 
type Props = {
  backLink: () => void;
  data?: TData;
};
 
const BusinessProfileSetup = ({ backLink, data }: Props) => {
  const { isEnterprisePortal } = checkPortals();
 
  const legalEntityId = data?.merchant?.legalEntityID;
 
  const canEdit = data?.legalEntity
    ? typeof data.legalEntity?.canEdit === "boolean"
      ? data.legalEntity?.canEdit
      : true
    : true;
 
  const allSteps = getAllSteps(data?.merchant?.class?.name, isEnterprisePortal);
  const steps = getDefaultSteps(allSteps, isEnterprisePortal);
 
  const defaultStep = allSteps.BUSINESS_DETAILS;
 
  const parsedData = useMemo(() => refineData(data), [data]);
  const providerPrimaryAccoundHolderData = isEnterprisePortal
    ? parsedData.enterpriseInfo.primaryAccountHolder
    : undefined;
  const {
    stepperConfig,
    handleUpdateStatusValue,
    statusBar,
    step,
    isLoaded,
    handleBack,
    handleNext,
  } = useBusinessProfileSteps(steps, defaultStep);
 
  const { setFormValues, isLoading: isSubmitting } = useBusinessDetailsData({
    legalEntityId,
  });
 
  const submitHandler: TSetFormValues = (section, values, options) => {
    const nextHandler =
      section === "enterpriseInfo" || section === "merchantInfo"
        ? backLink
        : handleNext;
 
    if (!options?.makeApiCall) {
      nextHandler();
      return;
    }
    setFormValues(section, values, {
      ...options,
      handleNext: nextHandler,
    });
  };
 
  const commonProps: Record<string, TBusinessStepsCommons> = {
    [allSteps.BUSINESS_DETAILS]: {
      handleBack: backLink,
      submitHandler,
      statusBar: statusBar[0]?.barValue || 0,
      updateStatusBar: handleUpdateStatusValue(allSteps.BUSINESS_DETAILS),
      isSubmitting,
    },
    [allSteps.BUSINESS_ADDRESS]: {
      handleBack,
      submitHandler,
      statusBar: statusBar[1]?.barValue || 0,
      updateStatusBar: handleUpdateStatusValue(allSteps.BUSINESS_ADDRESS),
      isSubmitting,
    },
    [allSteps.ADD_BUSINESS_OWNER]: {
      handleBack,
      submitHandler,
      statusBar: statusBar[2]?.barValue || 0,
      updateStatusBar: handleUpdateStatusValue(allSteps.ADD_BUSINESS_OWNER),
      isSubmitting,
    },
    [allSteps.MERCHANT_INFO]: {
      handleBack,
      submitHandler,
      statusBar: statusBar[3]?.barValue || 0,
      updateStatusBar: handleUpdateStatusValue(allSteps.MERCHANT_INFO),
      isSubmitting,
    },
    [allSteps.ENTERPRISE_INFO]: {
      handleBack,
      submitHandler,
      statusBar: statusBar[3]?.barValue || 0,
      updateStatusBar: handleUpdateStatusValue(allSteps.ENTERPRISE_INFO),
      isSubmitting,
    },
  };
 
  const components = [
    {
      node: (
        <BusinessDetailsStep
          {...commonProps[allSteps.BUSINESS_DETAILS]}
          canEdit={canEdit}
          legalEntityId={legalEntityId}
          data={parsedData.businessDetails}
        />
      ),
      visible: step === allSteps.BUSINESS_DETAILS,
    },
    {
      node: (
        <BusinessAddressStep
          {...commonProps[allSteps.BUSINESS_ADDRESS]}
          canEdit={canEdit}
          legalEntityId={legalEntityId}
          data={parsedData.businessAddress}
        />
      ),
      visible: step === allSteps.BUSINESS_ADDRESS,
    },
    {
      node: (
        <BusinessOwner
          {...commonProps[allSteps.ADD_BUSINESS_OWNER]}
          canEdit={
            typeof data?.legalEntity?.canEdit === "boolean"
              ? data?.legalEntity?.canEdit
              : true
          }
          data={parsedData.businessOwners}
          businessType={data?.legalEntity?.type?.name}
          primaryAccountHolder={providerPrimaryAccoundHolderData}
        />
      ),
      visible: step === allSteps.ADD_BUSINESS_OWNER,
    },
    {
      node: (
        <EnterpriseInfo
          {...commonProps[allSteps.ENTERPRISE_INFO]}
          data={parsedData.enterpriseInfo}
        />
      ),
      visible: step === allSteps.ENTERPRISE_INFO && isEnterprisePortal,
    },
  ];
 
  return (
    <Stack direction="column" justifyContent="stretch" height="inherit">
      <Stack direction="row" alignItems="center" gap={2}>
        <KotoStepper sx={{ flexGrow: 1 }} {...stepperConfig} />
      </Stack>
      {components.map(({ node, visible }, index) => (
        <HiddenComponent key={index} hidden={!visible || !isLoaded}>
          {node}
        </HiddenComponent>
      ))}
    </Stack>
  );
};
 
export default BusinessProfileSetup;
 
const getDefaultSteps = (
  allSteps: Record<string, string>,
  isEnterprisePortal: boolean,
) => {
  const businessSteps = [
    allSteps.BUSINESS_DETAILS,
    allSteps.BUSINESS_ADDRESS,
    allSteps.ADD_BUSINESS_OWNER,
  ];
  const merchantStep = [allSteps.MERCHANT_INFO];
  const enterpriseStep = [allSteps.ENTERPRISE_INFO];
 
  const steps = [
    ...businessSteps,
    ...(isEnterprisePortal ? enterpriseStep : merchantStep),
  ];
 
  return steps.map((name) => ({
    label: name,
    barValue: 0,
  }));
};
 
const getAllSteps = (
  merchantDisplayInfo: string,
  isEnterprisePortal: boolean,
) => {
  const isProviderDefault = !merchantDisplayInfo && isEnterprisePortal;
 
  const displayName = isProviderDefault
    ? "Provider"
    : generateNameLabelPlaceholder(merchantDisplayInfo);
 
  const allSteps: Record<string, string> = {
    BUSINESS_DETAILS: "Business details",
    BUSINESS_ADDRESS: "Business address",
    ADD_BUSINESS_OWNER: "Add business owner",
    MERCHANT_INFO: `${displayName} Info`,
    ENTERPRISE_INFO: `${displayName} Info`,
  };
  return allSteps;
};