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 | 1x 15x 15x 15x 15x 15x 15x 15x 15x 2x 2x 2x 15x 2x 2x 1x 1x 1x 1x 1x 1x 1x 15x 1x 1x 1x 1x 15x 15x 15x 5x 5x 15x 15x | import { useCalculatePercentage } from "@common/SignUp/useCalculatePercentage";
import { yupResolver } from "@hookform/resolvers/yup";
import { followLink } from "@hooks/common/documents/utils";
import { cloneDeep, isEqual } from "lodash";
import { useEffect, useRef } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import * as Yup from "yup";
import { isTestEnvironment } from "@constants/constants";
import { useFormData } from "@components/Signup/Forms/SignupFormProvider";
import { useBusinessClassification } from "@components/Signup/Forms/hooks/useBusinessClassification";
import { FormProps, IFormInputs, TInputName } from "../types";
import { generateRedirectHosts, getURL } from "../helpers";
const schema = Yup.object().shape({
name: Yup.mixed<TInputName>().required(
"Business classification selection required.",
),
});
export function useBusinessClassificationForm({
handleNext,
handleUpdateStatusValue,
setIsStart,
handleResetStatusbars,
}: FormProps) {
const { formData, setFormValues, resetHandler } = useFormData();
const { data, isLoading } = useBusinessClassification(
formData.organizationType,
);
const methods = useForm<IFormInputs>({
mode: "onChange",
resolver: yupResolver(schema),
defaultValues: formData.classification,
});
const saveOnUnmount = useRef<any>();
const { calculatePercentageNested } = useCalculatePercentage({
isEdit: false,
});
const values = methods.watch();
const isManageSubmerchants =
formData.organizationType === "manage_submerchants";
const handleSelect = ({ name }: { name?: string }) => {
Iif (!name) return;
const newValue = (values.name === name ? "" : name) as TInputName;
methods.setValue("name", newValue);
};
const onSubmit: SubmitHandler<IFormInputs> = async (data) => {
setFormValues("classification", data);
if (data.name && !isManageSubmerchants) {
const { URL, host } = getURL(data.name) ?? {};
const redirectHosts = generateRedirectHosts();
const windowHost = window.location.host;
Eif (
URL &&
host !== windowHost &&
(isTestEnvironment || redirectHosts.includes(windowHost))
) {
followLink(URL);
return;
}
}
handleNext();
};
const handleGoBack = () => {
setIsStart();
resetHandler();
methods.reset();
handleResetStatusbars();
};
useEffect(() => {
(async () => {
if (!isEqual(values, saveOnUnmount.current)) {
const value = await calculatePercentageNested(schema, values);
handleUpdateStatusValue(value);
}
saveOnUnmount.current = cloneDeep(values);
})();
}, [values]);
return {
methods,
values,
data,
isLoading,
handleSelect,
handleGoBack,
onSubmit,
};
}
|