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

88% Statements 22/25
76.92% Branches 20/26
100% Functions 5/5
88% Lines 22/25

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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279                                                                                                19x                   38x           38x             38x   38x   38x 38x   38x   38x     38x       38x                     38x   38x   38x             38x 2x           2x                   38x                                                                                                                                                                     38x                                                                       19x         61x                                             23x          
import { NameInput } from "@common/BusinessProfileInputs";
import BusinessTypeSelect from "@common/BusinessProfileInputs/BusinessTypeSelect";
import OwnershipTypeSelect from "@common/BusinessProfileInputs/OwnershipTypeSelect";
import { RHFInput, RHFTelInput } from "@common/Input";
import { Box, Grid, Stack } from "@mui/material";
import {
  ProfileSetupFormContainer,
  ProfileSetupFormActions,
  ProfileSetupFormTitle,
} from "../form.components";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as Yup from "yup";
import { VALIDATION_MESSAGES } from "@validation/messages";
import { TinType } from "@validation/types";
import CustomTaxSsnInput from "@common/BusinessProfileInputs/CustomTaxSsnInput";
import { BirthDatePicker } from "@common/DatePickers";
import moment from "moment";
import { phoneSchema, ssnSchema } from "@utils/validation.helpers";
import { gridItemsRenderer } from "@utils/rendering/nodesRenderers";
import usePercentageUpdate from "./hooks/usePercentageUpdate";
import { TBusinessStepsCommons } from "@components/ProfilePage/BusinessProfileSetupNew/types";
import { DynamicReturnType } from "./helpers/refineData";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, {
  CREATE_DENY_MESSAGE,
  EDIT_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { LINKED_BP_TOOLTIP_MESSAGE } from "@constants/stringConstants";
 
interface IProps extends TBusinessStepsCommons {
  canEdit: boolean;
  data: DynamicReturnType["businessDetails"];
  legalEntityId?: any;
}
 
type IFormInputs = {
  legalName: string;
  DBA: string;
  tinType: TinType;
  taxIDNumber: string;
  ssn: string;
  businessType: string;
  ownershipType: string;
  phoneNumber: string;
};
 
const BusinessDetailsStep = ({
  handleBack,
  submitHandler,
  statusBar,
  updateStatusBar,
  canEdit,
  data,
  isSubmitting,
  legalEntityId,
}: IProps) => {
  const isAddLEAllowed = useAccessControl({
    resource: RESOURCE_BASE.LEGAL_ENTITY,
    operation: OPERATIONS.CREATE,
    withPortal: true,
  });
 
  const isUpdateLEAllowed = useAccessControl({
    resource: RESOURCE_BASE.LEGAL_ENTITY,
    operation: OPERATIONS.UPDATE,
    withPortal: true,
  });
 
  const hasNoPermissions =
    (legalEntityId && !isUpdateLEAllowed) ||
    (!legalEntityId && !isAddLEAllowed);
  const disableInput = !canEdit || hasNoPermissions;
 
  const getTooltipMessage = () => {
    Iif (legalEntityId && !isUpdateLEAllowed) {
      return EDIT_DENY_MESSAGE;
    } else Iif (!legalEntityId && !isAddLEAllowed) {
      return CREATE_DENY_MESSAGE;
    } else Iif (!canEdit) {
      return LINKED_BP_TOOLTIP_MESSAGE;
    } else {
      return "";
    }
  };
 
  const methods = useForm<IFormInputs>({
    mode: "onChange",
    defaultValues: data,
    ...(canEdit && {
      resolver: yupResolver(schema),
    }),
  });
 
  const {
    watch,
    formState: { dirtyFields, isDirty },
  } = methods;
 
  const values = watch();
 
  usePercentageUpdate<IFormInputs>(
    values,
    dirtyFields,
    schema,
    updateStatusBar,
  );
 
  const onSubmit: SubmitHandler<IFormInputs> = (data) => {
    const customData = {
      ...data,
      ssn: data?.tinType === "ssn" ? data?.ssn : "",
      taxIDNumber: data?.tinType === "ein" ? data?.taxIDNumber : "",
    };
 
    submitHandler("businessDetails", customData, {
      makeApiCall: isDirty,
      dirtyFields: {
        ...dirtyFields,
        businessType: true,
        ownershipType: true,
      },
    });
  };
 
  const inputs = [
    {
      node: (
        <NameInput
          name="legalName"
          label="Business legal name"
          placeholder="Business legal name"
          isLegalName
          disabled={disableInput}
        />
      ),
    },
    {
      node: (
        <RHFInput
          name="DBA"
          label="Doing Business As (Optional)"
          placeholder="Doing business as..."
          fullWidth
          disabled={disableInput}
        />
      ),
    },
    {
      node: (
        <BirthDatePicker
          name="businessOpenedAt"
          label="Business Creation Date (mm/dd/yyyy)"
          maxDate={new Date()}
          minDate={null}
          openPickerOnFocus
          disabled={disableInput}
          useUTCMoment
        />
      ),
    },
    {
      node: (
        <BusinessTypeSelect
          name="businessType"
          label="Business type"
          disabled={disableInput}
        />
      ),
    },
    {
      node: (
        <CustomTaxSsnInput
          taxIdName="taxIDNumber"
          ssnName="ssn"
          businessTypeName="businessType"
          tinType="tinType"
          disabled={disableInput}
        />
      ),
    },
    {
      node: (
        <OwnershipTypeSelect
          name="ownershipType"
          label="Business ownership type"
          hasArrowDownIcon
          disabled={disableInput}
        />
      ),
    },
    {
      node: (
        <RHFTelInput
          name="phoneNumber"
          label="Business Phone Number"
          fullWidth
          disabled={disableInput}
          flagStyles={{
            width: "20px",
            height: "15px",
            borderRadius: 0,
          }}
        />
      ),
    },
  ];
 
  return (
    <FormProvider {...methods}>
      <Box
        component="form"
        flexGrow={1}
        id="business-profile-form"
        display="flex"
        onSubmit={methods.handleSubmit(onSubmit)}
      >
        <ProfileSetupFormContainer>
          <Stack direction="column" gap={4} height="min-content">
            <ProfileSetupFormTitle title="Fill in your business details" />
            <Grid container rowSpacing="12px">
              {gridItemsRenderer(inputs, {
                show: disableInput,
                message: getTooltipMessage(),
              })}
            </Grid>
          </Stack>
 
          <ProfileSetupFormActions
            secondaryAction={{
              onClick: handleBack,
            }}
            primaryAction={{
              disabled: isSubmitting,
              form: "business-profile-form",
              children: "Next",
            }}
          />
        </ProfileSetupFormContainer>
      </Box>
    </FormProvider>
  );
};
 
const schema = Yup.object().shape({
  legalName: Yup.string().required(VALIDATION_MESSAGES.REQUIRED),
  DBA: Yup.string(),
  businessOpenedAt: Yup.date()
    .transform((value) => {
      return value ? moment(value).toDate() : value;
    })
    .typeError("Enter valid date ")
    .required(VALIDATION_MESSAGES.REQUIRED)
    .max(new Date(), "Future date not allowed")
    .min(
      moment().subtract(500, "years").format("YYYY-MM-DD"),
      "Date is beyond acceptable range",
    ),
  tinType: Yup.mixed<TinType>().oneOf(["ssn", "ein"]),
  taxIDNumber: Yup.string().when("tinType", {
    is: "ein",
    then: ssnSchema("ein"),
  }),
  ssn: Yup.string().when("tinType", {
    is: "ssn",
    then: ssnSchema("ssn"),
  }),
  businessType: Yup.string().required(VALIDATION_MESSAGES.REQUIRED),
  ownershipType: Yup.string().required(VALIDATION_MESSAGES.REQUIRED),
  phoneNumber: phoneSchema().test(
    "phone-number",
    VALIDATION_MESSAGES.REQUIRED,
    (value) => !!value && value !== "+1",
  ),
});
 
export default BusinessDetailsStep;