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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | 18x 18x 18x 18x 18x 9x 9x 7x 18x 18x 18x 18x 18x 18x 18x 9x 18x 18x 18x 18x 18x 90x 3x 18x 7x 7x 7x 18x 3x | import { useCallback, useEffect } from "react";
import { Box, Grid } from "@mui/material";
import { yupResolver } from "@hookform/resolvers/yup";
import { Form, FormProvider, useForm } from "react-hook-form";
import { NameInput } from "@common/BusinessProfileInputs";
import { BirthDatePicker } from "@common/DatePickers";
import { RHFTelInput } from "@common/Input";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import { palette } from "@palette";
import {
calculateCompletionPercentage,
newSchema,
schema,
} from "./form/schema";
import { FormInputs } from "./form/form.type";
import {
getDefaultValues,
getIsOwnerBoxDisabled,
getIsOwnerCheckedByDefault,
} from "./form/form.const";
import { InformationProps } from "./types";
import { isEmpty } from "lodash";
import { styled } from "@mui/material";
import useCheckPAH from "@hooks/common/useCheckPAH";
import { NonResidentInputs } from "@common/NonResidentInput/NonResidentInputs";
import { getServicePhoneNumber } from "@utils/helpers";
import { formatInUTCMoment } from "@utils/date.helpers";
/*
Hide the modal is is in control mode
The modal appears only if the account holder was already invited, confirmed and then logged in
owner attr shoudl be send anymore
*/
export function Information({
checkValidity,
updateProgressBar,
data,
onSubmit,
leType,
leApproved,
fullOwnershipReleased,
hasLE,
hasOwner,
owner,
isController,
principal,
isApproved,
}: InformationProps) {
const { generateOwnerData } = useGenerateOwnerData();
const methods = useForm<FormInputs>({
resolver: yupResolver(newSchema),
defaultValues:
(data as FormInputs) ?? getDefaultValues(leType, fullOwnershipReleased),
mode: "onChange",
});
const isOwnerVal = methods.watch("isOwner");
const isPrincipalSaved = principal && !isEmpty(principal);
useEffect(() => {
Iif (!isEmpty(data)) return methods.reset(data);
if (!isEmpty(owner) && isEmpty(data))
methods.reset(
generateOwnerData(owner, principal, leType, fullOwnershipReleased),
);
}, [owner, data, principal]);
const isOwnerCheckDisabled =
getIsOwnerBoxDisabled(leType, fullOwnershipReleased) ||
leApproved ||
!isController ||
isPrincipalSaved;
const { isLoggedInPAH } = useCheckPAH();
const isDisabledApproved = isApproved || !isLoggedInPAH;
const isTaxExemptOrGovtAgency = [
"tax_exempt_organization",
"government_agency",
].includes(leType);
const inputList = [
{
input: (
<NameInput
name="base.firstName"
label="First name"
placeholder="First name"
disabled={isDisabledApproved}
data-testid="first-name-input"
/>
),
},
{
input: (
<NameInput
name="base.lastName"
label="Last name"
placeholder="Last name"
disabled={isDisabledApproved}
data-testid="last-name-input"
/>
),
},
{
input: (
<BirthDatePicker
name="base.dob"
placeholder="MM/DD/YYYY"
label="Date of Birth"
onBlur={() => {
methods.trigger("base.dob");
}}
disabled={isDisabledApproved}
useUTCMoment
/>
),
},
{
input: (
<RHFTelInput
onBlur={() => {
methods.trigger("base.phoneNumber");
}}
name="base.phoneNumber"
label="Phone Number"
fullWidth
disabled={!isLoggedInPAH}
/>
),
},
{
input: (
<NonResidentInputs
namePrefix="base"
inputsDisabled={!isLoggedInPAH}
isInverseResidence={true}
excludeCountries={["US"]}
/>
),
},
];
const {
formState: { isValid },
} = methods;
useEffect(() => {
checkValidity(isValid);
}, [isValid]);
const base = methods.getValues("base");
const ownership = methods.getValues("whenIsOwner");
const onUpdateProgressBar = useCallback(() => {
const { address, ...rest } = ownership;
type _base = keyof typeof base;
type _address = keyof typeof address;
type _rest = keyof typeof rest;
const a = (Object.keys(base) as _base[]).filter((k) => base[k]).length;
const b = (Object.keys(address) as _address[]).filter(
(k) => address[k],
).length;
const c = (Object.keys(rest) as _rest[]).filter((k) => rest[k]).length;
let total = Object.keys(base).length;
let currentProgress = a - 1; //1 step for api to be successfull
if (methods.getValues("isOwner")) {
//substract 2 from current (isOwner and remove one between EIN or SSN)
//substract 3 from total (is current with 1 substracted from Conuntry which is predefined and constant)
total += Object.keys(address).length;
total += Object.keys(rest).length;
currentProgress += b;
currentProgress += c;
total -= 3;
currentProgress -= 2;
}
if (methods.watch("base.phoneNumber") === "+1") {
//ODDDD behaviour of the input to keep the prefix on the form state
currentProgress -= 1;
}
updateProgressBar((currentProgress * 100) / total);
}, [base, ownership]);
const handleNewProgressBar = useCallback(() => {
const vals = methods.watch("base");
const v = calculateCompletionPercentage(vals, newSchema.fields.base);
updateProgressBar(v);
}, [base]);
return (
<FormProvider {...methods}>
<Form
id="confirm-primary-account-holder"
onSubmit={() => {
methods.handleSubmit((e) => {
const payload = {
...e,
base: {
...e?.base,
isManager: true,
},
};
onSubmit(payload);
})();
}}
onBlur={handleNewProgressBar}
onChange={(e) => {
//Because on autocompletion of input fields, the values do not get updated, we check on on blur and onchange for checkkoxes
if (
(e.target as any).getAttribute("name") === "base.isManager" ||
(e.target as any).getAttribute("name") === "isOwner"
) {
handleNewProgressBar();
}
}}
>
<Box>
<Grid container rowSpacing={1} columnSpacing={2}>
{inputList.map(({ input }, index) => (
<Grid key={index} item xs={12}>
<FadeUpWrapper delay={100 * (index + 1)}>{input}</FadeUpWrapper>
</Grid>
))}
</Grid>
</Box>
</Form>
</FormProvider>
);
}
const useGenerateOwnerData = () => {
const generateOwnerData = (
data: any,
principal: any,
leType: string,
fullOwnershipReleased: number,
): FormInputs => {
const user = data;
const address = principal?.address;
return {
isOwner: !isEmpty(principal)
? true
: fullOwnershipReleased === 100
? false
: getIsOwnerCheckedByDefault(leType),
base: {
dob: formatInUTCMoment(user?.dateOfBirth) || null,
firstName: user?.firstName,
isManager: data?.isManagerialAuthority,
lastName: user?.lastName,
phoneNumber: getServicePhoneNumber(user?.phoneNumber),
isNotUSResident: !!user?.citizenship,
citizenship: user?.citizenship || "",
isNotResidentInCitizenshipCountry: !!user?.countryOfResidence,
countryOfResidence: user?.countryOfResidence || "",
} as FormInputs["base"],
whenIsOwner: {
tinType: principal?.tinType ?? "ssn",
address: {
city: address?.city || "",
country: address?.country || "US",
line1: address?.line1,
state: address?.state,
zip: address?.zip,
},
ein:
principal?.tinType === "ein"
? `xxxxx${principal?.taxIDNumberLast4}`
: "",
ownership:
leType === "individual_sole_proprietorship"
? "100"
: ["tax_exempt_organization", "government_agency"].includes(leType)
? "0"
: `${principal?.ownershipPercentage}`,
ssn:
principal?.tinType === "ssn"
? `xxxxx${principal?.taxIDNumberLast4}`
: "",
},
};
};
return { generateOwnerData };
};
const StyledTaxInput = styled(Box)(() => ({
"& .MuiInputBase-root.Mui-disabled": {
backgroundColor: `${palette.liftedWhite[100]} !important`,
border: `1px solid ${palette.liftedWhite[200]}`,
"& p": {
WebkitTextFillColor: `${palette.black[100]} !important`,
},
},
"& .taxid-input .MuiInputBase-root.Mui-disabled": {
backgroundColor: `${palette.liftedWhite[200]} !important`,
boxShadow: "none",
border: "none",
"& input": {
marginTop: "18px",
WebkitTextFillColor: palette.black[100],
color: palette.black[100],
},
},
}));
|