All files / src/features/GiveOnboarding/pages/Identity/Details index.tsx

83.33% Statements 10/12
100% Branches 0/0
50% Functions 2/4
83.33% Lines 10/12

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                                                                  9x 31x 31x 30x   30x   30x                   1x                               30x                           30x                                                                                                   9x      
import {
  DeepPartial,
  DeepMap,
  FormProvider,
  SubmitHandler,
} from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { identityDetailsSchema } from "./schema";
import { GiveNameInput } from "@common/BusinessProfileInputs";
import { ControlledDatePicker } from "@sections/PayBuilder/Forms/DateAndLocation/components/ControlledDatePicker";
import { subYears } from "date-fns";
import {
  MAXIMUM_REQUIRED_AGE,
  MINIMUM_REQUIRED_AGE,
} from "@constants/constants";
import { HFGiveTelephone } from "@shared/HFInputs/HFGiveTelephone/HFGiveTelephone";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
import useCountryCodes from "@hooks/common/useCountryCodes";
import { IdentityDetailsFormValues } from "@features/GiveOnboarding/types/form";
import { forwardRef } from "react";
import useManageApi from "@sections/VerifyAccountHolder_v2/hooks/useManageApi";
import { useGetCurrentMerchantId } from "@hooks/common";
import { IStep } from "@features/GiveOnboarding/types";
import { useFormStep } from "@features/GiveOnboarding/hooks/useFormStep";
import { PagesContainer } from "../../pages.atoms";
import useMasqueradeReducer from "@hooks/Reducers/useMasqueradeReducer";
import { getMomentDate } from "@utils/date.helpers";
import moment from "moment";
 
interface Props {
  data_key: IStep;
}
 
const IDentityDetails = forwardRef((props: Props, ref: any) => {
  const { merchantId } = useGetCurrentMerchantId();
  const { onSubmitInformation } = useManageApi(merchantId);
  const { isMasqueradeMode } = useMasqueradeReducer();
 
  const { selectCountriesList } = useCountryCodes([""]);
 
  const handleSubmitIdentityDetails = async (
    formData: {
      data: IdentityDetailsFormValues;
      dirtyFields: Partial<
        Readonly<DeepMap<DeepPartial<IdentityDetailsFormValues>, boolean>>
      >;
    },
    onParentValid?: SubmitHandler<IdentityDetailsFormValues>,
    onParentInvalid?: (error: any) => void,
  ) => {
    await onSubmitInformation(
      { base: formData.data },
      () => {
        onParentValid?.({
          ...formData.data,
          dateOfBirth: getMomentDate(
            moment.utc(formData.data.dateOfBirth).toDate(),
          ).toDate(),
        }); // Signal success to parent
      },
      (apiError) => {
        onParentInvalid?.({ type: "api", error: apiError }); // Signal API error to parent
      },
    );
  };
 
  const { methods } = useFormStep<IdentityDetailsFormValues>(ref, {
    data_key: props.data_key,
    resolver: yupResolver(identityDetailsSchema),
    defaultValues: {
      countryOfResidence: "US",
      citizenship: "US",
      dateOfBirth: null,
      phoneNumber: "",
      firstName: "",
      lastName: "",
    },
    onSubmitStepData: handleSubmitIdentityDetails,
  });
 
  return (
    <FormProvider {...methods}>
      <PagesContainer testId="identity-details">
        <GiveNameInput
          name="firstName"
          label="First Name"
          disabled={isMasqueradeMode}
        />
        <GiveNameInput
          name="lastName"
          label="Last Name"
          disabled={isMasqueradeMode}
        />
        <ControlledDatePicker
          name="dateOfBirth"
          label="Date of Birth"
          disableFuture
          minDate={subYears(new Date(), MAXIMUM_REQUIRED_AGE)}
          maxDate={subYears(new Date(), MINIMUM_REQUIRED_AGE)}
          defaultPickerValue={subYears(new Date(), MINIMUM_REQUIRED_AGE)}
          disabled={isMasqueradeMode}
          useUTCMoment
        />
        <HFGiveTelephone
          name="phoneNumber"
          label="Mobile Phone"
          disabled={isMasqueradeMode}
          fullWidth
        />
        <HFGiveSelect
          withSearchBar
          name="countryOfResidence"
          label="Country of Residence"
          placeholder="Country of Residence"
          options={selectCountriesList}
          disabled={isMasqueradeMode}
        />
        <HFGiveSelect
          withSearchBar
          name="citizenship"
          label="Country of Citizenship"
          placeholder="Country of Citizenship"
          options={selectCountriesList}
          disabled={isMasqueradeMode}
        />
      </PagesContainer>
    </FormProvider>
  );
});
 
IDentityDetails.displayName = "IDentityDetails";
 
export { IDentityDetails };