All files / src/features/GiveOnboarding/hooks useWizardStep.ts

75.51% Statements 37/49
48.14% Branches 13/27
90% Functions 9/10
75.51% Lines 37/49

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                              9x       134x 134x 271x   134x 271x   134x   134x   134x 2x     134x 9x               134x       4x       4x 4x                                                                 4x   4x     4x 4x     4x     134x 2x                   2x                   2x 2x                       134x 19x 10x 7x       4x 4x       3x     9x 9x       134x         134x                    
import { useRef, useState } from "react";
import { WizardFormData } from "@features/GiveOnboarding/types/form";
import { StepHandle } from "@features/GiveOnboarding/types/handlers";
import { IDirection, IHandleNavigation, IStep } from "../types";
import { useDispatch, useSelector } from "react-redux";
import { updateFormData } from "@redux/slices/onboardingWizard";
import { showMessage } from "@common/Toast";
import { RootState } from "@redux/types/store";
import { useAppSelector } from "@redux/hooks";
 
interface Props {
  handleNavigation: IHandleNavigation;
  onHandleLastStepNavigation: any;
}
 
export const useWizardStep = ({
  handleNavigation,
  onHandleLastStepNavigation,
}: Props) => {
  const stepRef = useRef<StepHandle>(null);
  const currentSubstep = useSelector(
    (state: RootState) => state.onboardingWizard.currentSubstep,
  );
  const businessOwnerData = useAppSelector(
    (state) => state.onboardingWizard.formData.business_owners,
  );
  const [isLoading, setIsLoading] = useState(false);
 
  const dispatch = useDispatch();
 
  const onSubmitValidated = () => {
    setIsLoading(true);
  };
 
  const persistFormData = () => {
    dispatch(
      updateFormData({
        key: currentSubstep as keyof WizardFormData,
        data: stepRef.current?.methods?.getValues(),
      }),
    );
  };
 
  const onFormValidated = (
    data: any,
    config = { updateFormDataPayloads: [], prevStep: "" as IStep },
  ) => {
    const key = (stepRef.current?.data_key ?? config.prevStep) as
      | keyof WizardFormData
      | undefined;
 
    Eif (key) {
      dispatch(
        updateFormData(
          key === "business_address"
            ? [
                {
                  key: "business_owners",
                  data: {
                    ...businessOwnerData,
                    address: {
                      ...data,
                      line1: data.address,
                    },
                  },
                },
                {
                  key: key,
                  data: data,
                },
              ]
            : [
                ...config.updateFormDataPayloads,
                {
                  key: key,
                  data: data,
                },
              ],
        ),
      );
    }
 
    // Conditional navigation:
    // If it's the business_profile step and wasFilledAnExistingTaxID is true,
    // we stay on the current step to show the linked profile information.
    const isBusinessProfileStep = key === "business_profile";
    const filledExistingTaxID =
      isBusinessProfileStep &&
      data?.businessProfileDataAux?.wasFilledAnExistingTaxID === true;
    // For all other cases, or if not an existing tax ID on business profile, navigate next.
    Eif (!filledExistingTaxID) {
      handleNavigation("next", config);
    }
 
    setIsLoading(false);
  };
 
  const onFormInvalidated = (error: any) => {
    Iif (error?.type === "api") {
      showMessage(
        "Error",
        error?.error?.response?.data?.message ||
          "Something went wrong while saving informations.",
      );
      setIsLoading(false);
      return;
    }
 
    Iif (error?.type === "upload") {
      showMessage(
        "Error",
        error?.error?.message ||
          "Failed to upload the document. Please try again.",
      );
      setIsLoading(false);
      return;
    }
 
    Eif (error?.type === "formValidation") {
      return;
    }
 
    if (error?.type === "taxIDCheck") {
      setIsLoading(false);
      return;
    }
 
    setIsLoading(false);
    handleNavigation("next");
  };
 
  const onNavigate = async (direction: IDirection) => {
    if (direction === "next") {
      if (stepRef.current?.submit) {
        await stepRef.current.submit({
          onInvalid: onFormInvalidated,
          onSubmitValidated: onSubmitValidated,
          onValid: (data) => {
            onFormValidated(data);
            onHandleLastStepNavigation(); // only handle the refetch and success state of last step when its valid
          },
        });
      } else {
        handleNavigation(direction);
      }
    } else {
      persistFormData();
      handleNavigation(direction);
    }
  };
 
  const onLogout = () => {
    //for now we should not do anything. procced directely with the log-out
    return;
  };
 
  return {
    stepRef,
    isLoading,
    onLogout,
    onNavigate,
    onFormValidated,
    onFormInvalidated,
    persistFormData,
  };
};