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

65% Statements 13/20
0% Branches 0/2
14.28% Functions 1/7
65% Lines 13/20

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                                                                              2x 2x     2x             2x   2x                                                                                                     2x             2x   2x 2x     2x   2x                                                                                                                                           9x   9x      
import { forwardRef } from "react";
import { Box, Grid } from "@mui/material";
import {
  DeepPartial,
  DeepMap,
  FormProvider,
  SubmitHandler,
} from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
 
import { GiveProvinceInput, GiveZipCodeInput } from "@common/AddressInputs";
import { GiveHFStreetAddress } from "@common/RHFStreetAddress/GiveHFStreetAddress";
import { StepViewContainer } from "@features/GiveOnboarding/container/StepViewContainer";
import { useProvinces } from "@components/ProfilePage/BusinessProfileSetupNew/hooks/useProvinces";
import { useGetCurrentMerchantId } from "@hooks/common";
import useCountryCodes from "@hooks/common/useCountryCodes";
 
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
import GiveText from "@shared/Text/GiveText";
import { GiveInput } from "@shared/GiveInputs/GiveInput";
 
import { IStep } from "@features/GiveOnboarding/types";
import { businessAddressSchema } from "./schema";
import { useFormStep } from "@features/GiveOnboarding/hooks/useFormStep";
import { IBusinessAddressFormValues } from "@features/GiveOnboarding/types/form";
import { defaultValues } from "./const";
import {
  useCreateBusinessProfileMutation,
  usePatchBusinessDetailsMutation,
} from "@features/GiveOnboarding/mutations";
 
interface Props {
  data_key: IStep;
  legalEntityID?: number;
  hasExistingTaxId?: boolean;
}
 
function Address(props: Props, ref: any) {
  const { merchantId } = useGetCurrentMerchantId();
  const { selectCountriesList } = useCountryCodes();
 
  const { mutate: createBusinessProfileMutation } =
    useCreateBusinessProfileMutation(
      merchantId,
      props.legalEntityID,
      props.hasExistingTaxId,
    );
 
  const { mutate: patchMerchantMutation } =
    usePatchBusinessDetailsMutation({ merchantId });
 
  const handleSubmitDetails = async (
    formData: {
      data: IBusinessAddressFormValues;
      dirtyFields: Partial<
        Readonly<DeepMap<DeepPartial<IBusinessAddressFormValues>, boolean>>
      >;
    },
    onParentValid?: SubmitHandler<IBusinessAddressFormValues>,
    onParentInvalid?: (error: any) => void,
  ) => {
    const { address, ...rest } = formData.data;
    
    // First, create/update the business profile with legal entity address
    await createBusinessProfileMutation(
      {
        address: {
          ...rest,
          line1: address,
        },
      }, // Mutation payload uses 'rest'
      {
        onSuccess: () => {
          // Then, update the merchant endpoint with the address data
          patchMerchantMutation(
            {
              address: {
                country: formData.data.country,
                city: formData.data.city,
                line1: formData.data.address,
                state: formData.data.state,
                zip: formData.data.zip,
              },
            },
            {
              onSuccess: () => {
                // On success, pass the original formData.
                onParentValid?.(formData.data);
              },
              onError: (error) => {
                onParentInvalid?.({ type: "api", error });
              },
            },
          );
        },
        onError: (error) => {
          onParentInvalid?.({ type: "api", error });
        },
      },
    );
  };
 
  const { methods } = useFormStep<IBusinessAddressFormValues>(ref, {
    data_key: props.data_key,
    resolver: yupResolver(businessAddressSchema),
    defaultValues: defaultValues,
    onSubmitStepData: handleSubmitDetails,
  });
 
  const { watch } = methods;
 
  const values = watch();
  const selectedCountry = values.country;
 
  const { provinces, isDisabledByProvinceLogic } =
    useProvinces(selectedCountry);
 
  return (
    <FormProvider {...methods}>
      <StepViewContainer>
        <HFGiveSelect
          name="country"
          label="Country"
          options={selectCountriesList}
          disabled
        />
        <GiveHFStreetAddress
          name="address"
          label="Street Address"
          disabled={false}
          fullWidth
        />
        <Grid container spacing={3}>
          <Grid item xs={12} sm={4}>
            <HFGiveInput name="city" label="City" disabled={false} fullWidth />
          </Grid>
          <Grid item xs={12} sm={4}>
            <GiveProvinceInput
              name="state"
              disabled={isDisabledByProvinceLogic}
              isUS={selectedCountry === "US"}
              multiChoiseCountry={
                provinces?.[selectedCountry as keyof typeof provinces] as any
              }
            />
          </Grid>
          <Grid item xs={12} sm={4}>
            <GiveZipCodeInput
              disabled={false}
              name="zip"
              label="ZIP"
              letterSpacing="4px"
              isUSResident={selectedCountry === "US"}
            />
          </Grid>
        </Grid>
        <GiveText color="warning" fontSize="14px">
          Important: Your business address cannot be a P.O. Box. If your
          business operates from your residence, please provide a brief
          explanation below.
        </GiveText>
        <Box width="100%">
          <GiveInput
            disabled={false}
            label="Note (Optional)"
            fullWidth
            height={400}
            multiline
            name="notes"
            onChange={(e) => methods.setValue("notes", e.target.value || "")}
            value={values.notes}
            rows={6}
            maxLength={300}
            displayCounter
            InputProps={{
              sx: {
                marginTop: "15px",
              },
            }}
            placeholder="Briefly explain if your business operates from a residential address..."
          />
        </Box>
      </StepViewContainer>
    </FormProvider>
  );
}
 
const BusinessAddress = forwardRef(Address);
 
BusinessAddress.displayName = "BusinessAddress";
 
export default forwardRef(Address);