All files / src/features/GiveOnboarding/pages/Business/Profile index.tsx

100% Statements 19/19
88.88% Branches 8/9
100% Functions 3/3
100% Lines 19/19

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                                                                        10x         12x 12x 12x 23x     12x 1x                     1x                                         1x         1x     12x     12x   12x   12x 2x                         10x 1x                             9x                             10x      
import { StepViewContainer } from "@features/GiveOnboarding/container/StepViewContainer";
import { BusinessProfile } from "@features/GiveOnboarding/pages/Business/Profile/Form";
import { IStep } from "@features/GiveOnboarding/types";
import {
  IBusinessProfileFormValues,
  IBusinessAddressFormValues,
  IMerchantInfo,
} from "@features/GiveOnboarding/types/form";
import { forwardRef } from "react";
import { LinkedBusinessProfile } from "./LinkedBusinessProfile";
import { ExistingTaxIdWarningScreen } from "./ExistingTaxIdWarningScreen";
import {
  setCurrentSubstep,
  updateFormData,
} from "@redux/slices/onboardingWizard";
import { RootState } from "@redux/types/store";
import { useSelector, useDispatch } from "react-redux";
import { useQueryClient } from "react-query";
import { defaultValues as businessProfileDefaultValues } from "@features/GiveOnboarding/pages/Business/Profile/const";
import { defaultValues as businessAddressDefaultValues } from "@features/GiveOnboarding/pages/Business/Address/const";
import { defaultValues as businessOwnersDefaultValues } from "@features/GiveOnboarding/pages/Business/Owners/consts";
import { QKEY_BUSINESS_PROFILE_BY_ID } from "@constants/queryKeys";
 
interface Props {
  data_key: IStep;
  legalEntityID?: number;
  pendingTaxIdNumber?: string;
  hasExistingTaxId?: boolean;
  apiData: {
    business_address?: IBusinessAddressFormValues;
    merchant_info?: IMerchantInfo;
  };
}
 
type ForwardedRefType = any;
 
const BusinessProfileRoot = forwardRef<ForwardedRefType, Props>(
  (
    { data_key, legalEntityID, pendingTaxIdNumber, hasExistingTaxId, apiData },
    ref,
  ) => {
    const dispatch = useDispatch();
    const queryClient = useQueryClient();
    const formData = useSelector(
      (state: RootState) => state.onboardingWizard.formData,
    );
 
    const onChangeTaxId = async () => {
      const updatedBusinessProfile = {
        ...businessProfileDefaultValues,
        ssn: "",
        name: apiData.merchant_info?.merchantName ?? "",
        businessProfileDataAux: {
          wasFilledAnExistingTaxID: false,
          showLinkedBusinessProfile: false,
          hasBeenPreviouslyLinked: true,
        },
      };
 
      dispatch(
        updateFormData([
          {
            key: "merchant_info",
            data: { ...formData.merchant_info, legalEntityID: 0 },
          },
          {
            key: "business_profile",
            data: updatedBusinessProfile,
          },
          {
            key: "business_address",
            data: businessAddressDefaultValues,
          },
          {
            key: "business_owners",
            data: businessOwnersDefaultValues,
          },
        ]),
      );
 
      queryClient.setQueryData(
        [QKEY_BUSINESS_PROFILE_BY_ID, legalEntityID],
        undefined,
      );
 
      dispatch(setCurrentSubstep("stay_in_business_profile" as IStep));
    };
 
    const businessProfile = formData.business_profile;
 
    const wasFilledAnExistingTaxID =
      businessProfile?.businessProfileDataAux?.wasFilledAnExistingTaxID;
    const showLinkedBusinessProfileFlag =
      businessProfile?.businessProfileDataAux?.showLinkedBusinessProfile;
 
    if (wasFilledAnExistingTaxID) {
      return (
        <ExistingTaxIdWarningScreen
          taxId={
            pendingTaxIdNumber ||
            formData.business_profile?.taxIDNumber ||
            formData.business_profile?.ssn
          }
          onChangeTaxId={onChangeTaxId}
          email={apiData?.merchant_info?.pendingLegalEntityEmail}
        />
      );
    }
 
    if (showLinkedBusinessProfileFlag) {
      return (
        <StepViewContainer>
          <LinkedBusinessProfile
            data={{
              business_profile:
                formData.business_profile as IBusinessProfileFormValues,
              business_address:
                apiData.business_address as IBusinessAddressFormValues,
            }}
            onChangeTaxId={onChangeTaxId}
          />
        </StepViewContainer>
      );
    }
 
    return (
      <BusinessProfile
        data_key={data_key}
        legalEntityID={legalEntityID}
        hasExistingTaxId={hasExistingTaxId}
        ref={ref}
        merchantName={apiData.merchant_info?.merchantName}
        hasBeenPreviouslyLinked={
          businessProfile?.businessProfileDataAux?.hasBeenPreviouslyLinked
        }
      />
    );
  },
);
 
BusinessProfileRoot.displayName = "BusinessProfileRoot";
 
export { BusinessProfileRoot };