@@ -14,7 +14,6 @@ import styles from './login.module.scss' import ClosedEyeSvg from '#/assets/svg/closed-eye.svg?react' import OpenedEyeSvg from '#/assets/svg/opened-eye.svg?react' import { PasswordOptions } from '#/features/register-by-email/lib/constants' -import { IEmailForms } from '#/features/register-by-email/model/types' import { YandexAuthButton } from '#/features/yandex-auth-button' import { NextPageWithLayout } from '#/pages/_app' import { ERROR_MAPPING, ERRROR_YANDEX_TRANSLATE_MAPPING } from '#/pages/api/auth/constants' @@ -23,6 +22,11 @@ import { getDeviceType } from '#/shared/lib/helpers' import { useShowDataStore } from '#/shared/lib/hooks/use-show-data' import { InputStyleDark, InputStyleLight } from '#/shared/ui/input' +type LoginFormValues = { + email: string + password: string +} + const Login: NextPageWithLayout = () => { const device = getDeviceType() @@ -42,7 +46,47 @@ const Login: NextPageWithLayout = () => { const desktop = device === 'desktop' - const { register, handleSubmit } = useForm() + const { register, handleSubmit, setValue, trigger } = useForm() + + const emailInputRef = React.useRef(null) + const passwordInputRef = React.useRef(null) + + const { + ref: emailRef, + onChange: emailOnChange, + ...emailField + } = register('email', emailOptions as any) + + const { + ref: passwordRef, + onChange: passwordOnChange, + ...passwordField + } = register('password', { + ...PasswordOptions, + } as any) + + const mergeRefs = + (...refs: Array | undefined>) => + (value: T) => { + refs.forEach((ref) => { + if (!ref) return + if (typeof ref === 'function') ref(value) + else (ref as React.MutableRefObject).current = value + }) + } + + useEffect(() => { + const t = window.setTimeout(() => { + const email = emailInputRef.current?.value ?? '' + const password = passwordInputRef.current?.value ?? '' + + if (email) setValue('email', email, { shouldDirty: true, shouldValidate: true }) + if (password) setValue('password', password, { shouldDirty: true, shouldValidate: true }) + if (email || password) trigger() + }, 200) + + return () => window.clearTimeout(t) + }, [setValue, trigger]) useEffect(() => { const params = new URL(window.location.href).searchParams @@ -75,7 +119,7 @@ const Login: NextPageWithLayout = () => { } } - const checkError: SubmitErrorHandler = (data) => { + const checkError: SubmitErrorHandler = (data) => { showMessage(Object.values(data)[0].message || 'Неверные данные') } @@ -85,10 +129,6 @@ const Login: NextPageWithLayout = () => { } } - const handleInputChangeTrim = (event: any) => { - event.target.value = event.target.value.trim() - } - return ( <> { { + event.target.value = event.target.value.trim() + emailOnChange(event) + }} /> { ), }} sx={{ ...InputStyleDark }} - {...register('password', { - ...PasswordOptions, - })} + {...passwordField} + inputRef={mergeRefs(passwordRef, passwordInputRef)} + onChange={(event) => { + passwordOnChange(event) + }} /> {!loading ? (