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 | 34x 34x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 211x 19x 211x 19x 19x 19x 3x 3x 16x 211x 211x 4x 4x 4x 4x 4x 211x 4x 211x 211x 211x | import { useEffect, useRef, useState } from "react";
import {
TCreateBusinessProfile,
FormType,
IUseBusinessProfileModal,
} from "../types";
import { businessProfileDefaultValues } from "@validation/merchant";
import { TBusinessOwner } from "@components/Merchants/MerchantPreview/data.types";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { useUpdateMerchantInfo } from "@components/Merchants/MerchantPreview/hooks/useUpdateMerchantInfo";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import {
composePermission,
useAccessControl,
} from "features/Permissions/AccessControl";
import { yupResolver } from "@hookform/resolvers/yup";
import { SubmitHandler, useForm } from "react-hook-form";
import {
businessAddressParser,
useBusinessProfileParser,
useMerchantBusinessOwnersParser,
} from "@components/Merchants/MerchantPreview/helpers/parsers";
import { getLegalEntityPrincipals } from "../helpers";
import { getServicePhoneNumber } from "@utils/helpers";
import { checkTaxIDAvailability } from "./utils";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { getLegalEntityByID } from "@services/api/businessProfile";
import { SAVE_CHANGES_MODAL } from "modals/modal_names";
import { isEmpty, pick } from "lodash";
import moment from "moment";
import { getBusinessProfileSchema } from "../schemas/BusinessProfileSchema";
type FormInputs = TCreateBusinessProfile;
export type { TinType } from "@validation/types";
const defaultValues = businessProfileDefaultValues;
const useBusinessProfileModal = ({
data,
onClose,
merchantId,
legalEntityID,
formType = FormType.CREATE_MERCHANT,
isEdit = false,
isBPLinked,
}: IUseBusinessProfileModal) => {
const { businessProfileParser } = useBusinessProfileParser();
const { merchantBusinessOwnersParser } = useMerchantBusinessOwnersParser();
const modal = useModal();
const open = modal.visible;
const checkedTaxID = useRef({
taxId: data?.taxID || data?.ssn || "",
isDeclinedOrNotApproved: false,
isLinked: false,
});
const { isOnboardingLinkBPEnabled } = useGetFeatureFlagValues();
const { handleSubmit, isLoading } = useUpdateMerchantInfo(
merchantId || 0,
legalEntityID,
);
const merchantType =
formType === FormType.CREATE_MERCHANT
? RESOURCE_BASE.MERCHANT
: RESOURCE_BASE.ENTERPRISE;
const isUpdateAllowed = useAccessControl({
resource: composePermission(merchantType, RESOURCE_BASE.LEGAL_ENTITY),
operation: OPERATIONS.UPDATE,
});
const canEdit =
!isEdit || (isEdit && isUpdateAllowed) || (isEdit && !merchantId);
const canEditWithLegalEntity =
!isEdit ||
(isEdit && isUpdateAllowed && !isBPLinked) ||
(isEdit && !merchantId);
const showAccessDeniedTooltip = isEdit && !isUpdateAllowed && !!merchantId;
const schema = getBusinessProfileSchema({
isOnboardingLinkBPEnabled,
checkedTaxID,
checkTaxIDAvailability,
});
const methods = useForm<FormInputs>({
mode: "onChange",
reValidateMode: "onChange",
resolver: yupResolver(schema),
defaultValues,
});
const {
reset,
watch,
formState: { isValid, isDirty, errors, dirtyFields },
} = methods;
const values = watch();
const [selectedProfile, setSelectedProfile] = useState<any>(null);
const parsedSelectedProfile = {
...businessProfileParser(selectedProfile),
pendingLegalEntityEmail: selectedProfile?.controllerOwnerEmail,
address: businessAddressParser(
selectedProfile?.address,
selectedProfile?.address?.id,
),
};
const resetToInitialData = () => {
reset({
...defaultValues,
...data,
contactPhone: getServicePhoneNumber(data?.contactPhone),
DBA: data?.DBA || data?.dba,
});
};
useEffect(() => {
resetToInitialData();
(async () => {
if (data?.isLinkBusinessProfile && data.linkedBusinessProfile) {
const selectedEntity = await getLegalEntityByID(
data.linkedBusinessProfile,
);
setSelectedProfile({
isOutOfMerchantTree: data?.isOutOfMerchantTree,
...selectedEntity,
});
} else {
setSelectedProfile(null);
}
})();
}, [data]);
const handleClose = (data?: TCreateBusinessProfile) => {
reset(data);
modal.remove();
};
const handleSave = async (data: TCreateBusinessProfile) => {
Iif (merchantId) {
//only include in the payload the values that have been changed + required fields for logic, when editing BP
const dirtyData = !!legalEntityID
? pick(data, Object.keys(dirtyFields))
: data;
const payload = {
...dirtyData,
isLinkBusinessProfile: data.isLinkBusinessProfile,
...(data.tinType && { tinType: data.tinType }),
...(data.taxID && { taxID: data.taxID }),
...(data.ssn && { ssn: data.ssn }),
...(dirtyData.businessOpenedAt && {
businessOpenedAt: dirtyData?.businessOpenedAt,
}),
...(dirtyData.isLinkBusinessProfile &&
dirtyData.linkedBusinessProfile && {
address: businessAddressParser(selectedProfile?.address),
}),
};
handleSubmit("business_profile", payload, () => handleClose(data));
return;
}
let customData = {
businessProfile: {
...data,
ssn: data?.tinType === "ssn" ? data?.ssn?.replace("-", "") : "",
taxID: data?.tinType === "ein" ? data?.taxID?.replace("-", "") : "",
...(data?.businessOpenedAt && {
businessOpenedAt: moment(data.businessOpenedAt)
.utc()
.format("MM/DD/YYYY"),
}),
isLinkBusinessProfile: false,
linkedBusinessProfile: undefined as number | undefined,
},
businessOwners: [] as TBusinessOwner[],
};
Iif (data.isLinkBusinessProfile && data.linkedBusinessProfile) {
let principals = [];
if (selectedProfile?.id) {
try {
const legalEntityData = await getLegalEntityPrincipals(
selectedProfile.id,
);
principals = legalEntityData || [];
} catch (error) {
principals = [];
}
}
const linkedBusinessOwners = merchantBusinessOwnersParser(
principals,
)?.map((principal) => ({
...principal,
linked: true,
}));
customData = {
businessProfile: {
...businessProfileParser(selectedProfile),
isController: true,
businessType: selectedProfile?.type?.name,
isLinkBusinessProfile: true,
linkedBusinessProfile: data.linkedBusinessProfile,
isOutOfMerchantTree: selectedProfile?.isOutOfMerchantTree,
address: businessAddressParser(selectedProfile?.address),
},
businessOwners: linkedBusinessOwners || [],
};
}
Eif (onClose) onClose(customData);
modal.remove();
};
const onSubmit: SubmitHandler<FormInputs> = async (data) => {
if (!isEmpty(dirtyFields)) handleSave(data);
else EhandleClose();
};
const isErrors = !isEmpty(errors);
const handleCancel = () => {
if (isDirty && isValid && !isErrors) {
return NiceModal.show(SAVE_CHANGES_MODAL, {
modalName: "Edit Business Profile",
handleCancel: () => {
resetToInitialData();
handleClose();
},
handleSave: () => handleSave(values),
});
}
handleClose();
};
return {
open,
isLoading,
canEdit,
canEditWithLegalEntity,
handleCancel,
handleClose,
onSubmit,
methods,
parsedSelectedProfile,
setSelectedProfile,
showAccessDeniedTooltip,
checkedTaxID,
isErrors,
};
};
export default useBusinessProfileModal;
|