From 4fea17a2ce13b177b400adf4d727d141aff4cda0 Mon Sep 17 00:00:00 2001 From: Arkadzi Rakouski Date: Sun, 13 Aug 2023 16:36:18 +0300 Subject: [PATCH] add focus & fix styles for auth forms errors (#156) * add focus & fix styles for auth forms errors * refactor by review comments * minor changes: let ref -> const ref: { current }, additional typings --------- Co-authored-by: bniwredyc --- .../Nav/AuthModal/ForgotPasswordForm.tsx | 18 ++++- src/components/Nav/AuthModal/LoginForm.tsx | 30 ++++++- src/components/Nav/AuthModal/RegisterForm.tsx | 44 +++++++---- src/styles/app.scss | 78 ++++++++++--------- 4 files changed, 115 insertions(+), 55 deletions(-) diff --git a/src/components/Nav/AuthModal/ForgotPasswordForm.tsx b/src/components/Nav/AuthModal/ForgotPasswordForm.tsx index fb9268d0..c18e2092 100644 --- a/src/components/Nav/AuthModal/ForgotPasswordForm.tsx +++ b/src/components/Nav/AuthModal/ForgotPasswordForm.tsx @@ -28,6 +28,8 @@ export const ForgotPasswordForm = () => { const [validationErrors, setValidationErrors] = createSignal({}) const [isUserNotFount, setIsUserNotFound] = createSignal(false) + const authFormRef: { current: HTMLFormElement } = { current: null } + const handleSubmit = async (event: Event) => { event.preventDefault() @@ -47,6 +49,10 @@ export const ForgotPasswordForm = () => { const isValid = Object.keys(newValidationErrors).length === 0 if (!isValid) { + authFormRef.current + .querySelector(`input[name="${Object.keys(newValidationErrors)[0]}"]`) + .focus() + return } @@ -66,7 +72,11 @@ export const ForgotPasswordForm = () => { } return ( -
+ (authFormRef.current = el)} + >

{t('Forgot password?')}

{t('Everything is ok, please give us your email address')}
@@ -95,7 +105,11 @@ export const ForgotPasswordForm = () => {
{validationErrors().email}
-
+
{ const [isLinkSent, setIsLinkSent] = createSignal(false) const [showPassword, setShowPassword] = createSignal(false) + const authFormRef: { current: HTMLFormElement } = { current: null } + const { actions: { showSnackbar } } = useSnackbar() @@ -57,9 +59,11 @@ export const LoginForm = () => { const handleSendLinkAgainClick = async (event: Event) => { event.preventDefault() + + setIsLinkSent(true) setIsEmailNotConfirmed(false) setSubmitError('') - setIsLinkSent(true) + const result = await signSendLink({ email: email(), lang: lang(), template: 'email_confirmation' }) if (result.error) setSubmitError(result.error) } @@ -85,6 +89,11 @@ export const LoginForm = () => { if (Object.keys(newValidationErrors).length > 0) { setValidationErrors(newValidationErrors) + + authFormRef.current + .querySelector(`input[name="${Object.keys(newValidationErrors)[0]}"]`) + .focus() + return } @@ -92,18 +101,23 @@ export const LoginForm = () => { try { await signIn({ email: email(), password: password() }) + hideModal() + showSnackbar({ body: t('Welcome!') }) } catch (error) { if (error instanceof ApiError) { if (error.code === 'email_not_confirmed') { setSubmitError(t('Please, confirm email')) + setIsEmailNotConfirmed(true) + return } if (error.code === 'user_not_found') { setSubmitError(t('Something went wrong, check email and password')) + return } } @@ -115,7 +129,7 @@ export const LoginForm = () => { } return ( - + (authFormRef.current = el)}>
@@ -131,7 +145,11 @@ export const LoginForm = () => {
{t('Link sent, check your email')}
-
+
{
{validationErrors().email}
-
+
{ const { emailChecks } = useEmailChecks() const [submitError, setSubmitError] = createSignal('') - const [name, setName] = createSignal('') + const [fullName, setFullName] = createSignal('') const [password, setPassword] = createSignal('') const [isSubmitting, setIsSubmitting] = createSignal(false) const [isSuccess, setIsSuccess] = createSignal(false) const [validationErrors, setValidationErrors] = createSignal({}) + const authFormRef: { current: HTMLFormElement } = { current: null } + const handleEmailInput = (newEmail: string) => { setValidationErrors(({ email: _notNeeded, ...rest }) => rest) setEmail(newEmail) @@ -73,8 +75,8 @@ export const RegisterForm = () => { } const handleNameInput = (newPasswordCopy: string) => { - setValidationErrors(({ name: _notNeeded, ...rest }) => rest) - setName(newPasswordCopy) + setValidationErrors(({ fullName: _notNeeded, ...rest }) => rest) + setFullName(newPasswordCopy) } const handleSubmit = async (event: Event) => { @@ -84,11 +86,11 @@ export const RegisterForm = () => { const newValidationErrors: ValidationErrors = {} - const cleanName = name().trim() + const cleanName = fullName().trim() const cleanEmail = email().trim() if (!cleanName) { - newValidationErrors.name = t('Please enter a name to sign your comments and publication') + newValidationErrors.fullName = t('Please enter a name to sign your comments and publication') } if (!cleanEmail) { @@ -108,6 +110,10 @@ export const RegisterForm = () => { const isValid = Object.keys(newValidationErrors).length === 0 && !emailCheckResult if (!isValid) { + authFormRef.current + .querySelector(`input[name="${Object.keys(newValidationErrors)[0]}"]`) + .focus() + return } @@ -135,7 +141,7 @@ export const RegisterForm = () => { return ( <> - + (authFormRef.current = el)}>
@@ -145,7 +151,11 @@ export const RegisterForm = () => {
-
+
{ autocomplete="" onInput={(event) => handleNameInput(event.currentTarget.value)} /> - +
- -
{validationErrors().name}
+ +
{validationErrors().fullName}
-
+
{
-
+