import { ChangeEvent, useMemo, useEffect, useRef, useState } from 'react'; import { useMutation } from 'react-query'; import { useRegisterStore } from '../stores/useRegisterStore'; import { RegisterProps } from '~/types/auth'; import { registerUser } from '~/services/auth'; import TermCondition from './TermCondition'; import FormCaptcha from './FormCaptcha'; import { useRouter } from 'next/router'; import { UseToastOptions, useToast } from '@chakra-ui/react'; import Link from 'next/link'; interface FormProps { type: string; required: boolean; isBisnisRegist: boolean; chekValid: boolean; buttonSubmitClick: boolean; } const Form: React.FC = ({ type, required, isBisnisRegist = false, chekValid, buttonSubmitClick, }) => { const { form, isCheckedTNC, isValidCaptcha, errors, updateForm, validate } = useRegisterStore(); const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]); const router = useRouter(); const toast = useToast(); const emailRef = useRef(null); const nameRef = useRef(null); const passwordRef = useRef(null); const phoneRef = useRef(null); const handleInputChange = (event: ChangeEvent) => { const { name, value } = event.target; updateForm(name, value); validate(); }; useEffect(() => { const loadIndustries = async () => { if (!isFormValid) { const options: ScrollIntoViewOptions = { behavior: 'smooth', block: 'center', }; if (errors.email_partner && emailRef.current) { emailRef.current.scrollIntoView(options); return; } if (errors.name && nameRef.current) { nameRef.current.scrollIntoView(options); return; } if (errors.password && passwordRef.current) { passwordRef.current.scrollIntoView(options); return; } if (errors.phone && phoneRef.current) { phoneRef.current.scrollIntoView(options); return; } } }; loadIndustries(); }, [buttonSubmitClick, chekValid]); return (
{chekValid && !!errors.name && ( {errors.name} )}
{chekValid && !!errors.email && ( {errors.email} )}
{chekValid && !!errors.password && ( {errors.password} )}
{chekValid && !!errors.phone && ( {errors.phone} )}
); }; export default Form;