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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | 10x 40x 40x 53x 36x 36x 36x 36x 36x 6x 6x 6x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 1x 1x 3x 3x 3x 1x 36x 36x 36x 36x 13x 1x 36x 10x | import {
DeepPartial,
DeepMap,
FormProvider,
SubmitHandler,
} from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { GiveNameInput } from "@common/BusinessProfileInputs";
import { IBusinessProfileFormValues } from "@features/GiveOnboarding/types/form";
import { forwardRef, useEffect } from "react";
import { businessProfileSchema } from "./schema";
import { IStep } from "@features/GiveOnboarding/types";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import GiveBusinessTypeSelect from "@common/BusinessProfileInputs/GiveBusinessTypeSelect";
import GiveOwnershipTypeSelect from "@common/BusinessProfileInputs/GiveOwnershipTypeSelect";
import { ControlledDatePicker } from "@sections/PayBuilder/Forms/DateAndLocation/components/ControlledDatePicker";
import { HFGiveTelephone } from "@shared/HFInputs/HFGiveTelephone/HFGiveTelephone";
import GiveTaxIdInput from "@shared/GiveInputs/GiveTaxIdInput";
import { Box } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { useFormStep } from "@features/GiveOnboarding/hooks/useFormStep";
import { useGetCurrentMerchantId } from "@hooks/common";
import {
useCreateBusinessProfileMutation,
useDeleteCompletedSteps,
usePatchBusinessDetailsMutation,
} from "@features/GiveOnboarding/mutations";
import { checkTaxIDAvailability } from "@components/Merchants/CreateMerchantPanel/modals/utils";
import {
QKEY_BUSINESS_PROFILE_BY_ID,
QKEY_GET_MERCHANT_BY_ID,
} from "@constants/queryKeys";
import { useQueryClient } from "react-query";
import { useSelector } from "react-redux";
import { RootState } from "@redux/types/store";
import { defaultValues } from "./const";
import { PagesContainer } from "../../pages.atoms";
interface Props {
data_key: IStep;
legalEntityID?: number;
hasExistingTaxId?: boolean;
hasBeenPreviouslyLinked?: boolean;
merchantName?: string;
}
const BusinessProfile = forwardRef((props: Props, ref: any) => {
const { merchantId } = useGetCurrentMerchantId();
const globalFormData = useSelector(
(state: RootState) => state.onboardingWizard.formData,
);
const { mutate: patchBusinessDetailsMutation } =
usePatchBusinessDetailsMutation({
merchantId,
});
const { mutate: createBusinessProfileMutation } =
useCreateBusinessProfileMutation(
merchantId,
props.legalEntityID,
props.hasExistingTaxId,
);
const { mutateAsync: deleteCompletedSteps } = useDeleteCompletedSteps({
merchantId,
});
const queryClient = useQueryClient();
const handleSubmitDetails = async (
formData: {
data: IBusinessProfileFormValues;
dirtyFields: Partial<
Readonly<DeepMap<DeepPartial<IBusinessProfileFormValues>, boolean>>
>;
},
onParentValid?: SubmitHandler<IBusinessProfileFormValues>,
onParentInvalid?: (error: any) => void,
) => {
const { businessProfileDataAux, tinType, ssn, taxIDNumber, ...rest } =
formData.data;
const shouldCheckTaxId =
formData.dirtyFields.tinType ||
(tinType === "ein" && formData.dirtyFields.taxIDNumber) ||
(tinType === "ssn" && formData.dirtyFields.ssn);
if (shouldCheckTaxId) {
const taxIdNumberAttr = tinType === "ein" ? "taxIDNumber" : "ssn";
const nonFormattedTaxID = formData.data[taxIdNumberAttr].replace(
/(\s|-)/g,
"",
);
try {
const res = await checkTaxIDAvailability(nonFormattedTaxID);
//is is linked true and id defined, redirect to the third screen where the merchant has to wait for the owner aproval
//check src/components/Merchants/CreateMerchantPanel/modals/useBusinessProfileModal.ts
//to link ID 00-5418999
if (res.isLinked && res.id) {
const updatedFormData = {
...formData.data,
businessProfileDataAux: {
...businessProfileDataAux,
showLinkedBusinessProfile: true,
},
};
await patchBusinessDetailsMutation(
{
legalEntityID: res.id,
},
{
onSuccess: (response: any) => {
Eif (
response.pendingLegalEntityID &&
response.pendingLegalEntityStatus === "pending"
) {
queryClient.invalidateQueries(QKEY_BUSINESS_PROFILE_BY_ID);
onParentValid?.({
...updatedFormData,
businessProfileDataAux: {
wasFilledAnExistingTaxID: true,
showLinkedBusinessProfile: false,
},
});
queryClient.invalidateQueries([
QKEY_GET_MERCHANT_BY_ID,
response?.accID,
]);
}
},
onError: (error) => {
onParentInvalid?.({ type: "api", error });
},
},
);
return; // Exit after handling the linked case
}
Eif (res.isDeclinedOrNotApproved) {
methods.setError(taxIdNumberAttr, {
message:
"This business profile has been declined or suspended and cannot be added",
});
onParentInvalid?.({ type: "taxIDCheck", error: res });
return;
}
// If not linked, fall through to the mutation logic below
} catch (error) {
console.error("Error checking Tax ID availability:", error);
// Handle potential errors from checkTaxIDAvailability
return; // Stop further execution if tax ID check fails
}
}
let finalMutationPayload: Partial<IBusinessProfileFormValues>;
if (!props.legalEntityID || props.hasExistingTaxId) {
// props.legalEntityID is undefined. Per user: this is a POST and dirty fields logic should not happen.
// So, construct payload from all of formData.data, excluding businessProfileDataAux.
finalMutationPayload = {
...rest,
tinType,
...(tinType === "ein" ? { taxIDNumber } : { ssn }),
};
} else {
// props.legalEntityID is defined. Apply existing "dirty fields only" logic.
const { data, dirtyFields } = formData; // data is formData.data
const dirtyOnlyPayload: Partial<IBusinessProfileFormValues> = {};
for (const key of Object.keys(dirtyFields) as Array<
keyof IBusinessProfileFormValues
>) {
// Skip businessProfileDataAux as it's not part of the actual business profile entity for mutation
Iif (key === "businessProfileDataAux") {
continue;
}
Iif (key === "ssn") {
if (data.tinType === "ssn") {
(dirtyOnlyPayload as any)[key] = data[key];
}
continue; // Skip if tinType is not 'ssn'
}
Iif (key === "taxIDNumber") {
if (data.tinType === "ein") {
(dirtyOnlyPayload as any)[key] = data[key];
}
continue; // Skip if tinType is not 'ein'
}
(dirtyOnlyPayload as any)[key] = data[key];
}
finalMutationPayload = dirtyOnlyPayload;
}
await createBusinessProfileMutation(finalMutationPayload, {
onSuccess: async (response: any) => {
if (props.hasBeenPreviouslyLinked) {
await deleteCompletedSteps({
stepNames: ["business_address", "business_owners"],
});
await queryClient.invalidateQueries([
"onboarding-progress",
merchantId,
]);
}
onParentValid?.(
props.hasBeenPreviouslyLinked
? {
...formData.data,
businessProfileDataAux: {
...globalFormData.business_profile?.businessProfileDataAux,
hasBeenPreviouslyLinked: false,
},
}
: formData.data,
{
nextStep: "business_address",
prevStep: "business_profile",
updateFormDataPayloads: [
{
key: "merchant_info",
data: {
...globalFormData.merchant_info,
legalEntityID: response?.id,
},
},
],
} as any,
);
await queryClient.invalidateQueries([
QKEY_GET_MERCHANT_BY_ID,
response?.controllerID,
]);
queryClient.invalidateQueries(QKEY_BUSINESS_PROFILE_BY_ID);
},
onError: (error) => {
onParentInvalid?.({ type: "api", error });
},
});
};
const { methods } = useFormStep<IBusinessProfileFormValues>(ref, {
data_key: props.data_key,
resolver: yupResolver(businessProfileSchema),
defaultValues: {
...defaultValues,
name: props?.merchantName,
tinType: "ein",
},
onSubmitStepData: handleSubmitDetails,
});
const { watch, setValue } = methods;
const type = watch("type");
useEffect(() => {
if (type === "sole_proprietorship") {
setValue("ownershipType", "private");
}
}, [type, setValue]);
return (
<FormProvider {...methods}>
<PagesContainer
sx={{
'input[name="businessOpenedAt"]::placeholder': {
textTransform: "uppercase",
},
}}
>
<GiveNameInput
name="name"
label="Legal Name"
disabled={false}
isLegalName
data-testid="business-legal-name"
/>
<HFGiveInput
name="DBA"
label="Doing Business As (Optional)"
fullWidth
data-testid="business-dba"
disabled={false}
/>
<ControlledDatePicker
name="businessOpenedAt"
label="Business Creation Date"
maxDate={new Date()}
disabled={false}
data-testid="business-creation-date"
hidePlaceholder={false}
useUTCMoment
/>
<Box>
<GiveText variant="bodyS" mb="16px">
Federal Tax ID
</GiveText>
<GiveTaxIdInput
ssnName="ssn"
taxIdName="taxIDNumber"
businessTypeName="type"
tinType="tinType"
disabled={false}
data-testid="business-tax-id"
/>
</Box>
<GiveBusinessTypeSelect
name="type"
label="Business Type"
disabled={false}
/>
<GiveOwnershipTypeSelect
name="ownershipType"
label="Business Ownership Type"
businessTypeName="type"
disabled={type === "sole_proprietorship"}
data-testid="business-ownership-type"
/>
<HFGiveTelephone
disabled={false}
name="phoneNumber"
label="Business Phone Number"
fullWidth
data-testid="business-phone"
/>
</PagesContainer>
</FormProvider>
);
});
BusinessProfile.displayName = "BusinessProfile";
export { BusinessProfile };
|