From 6311c687127cb0e19935bbf76e69e18a6e3c2a11 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Wed, 2 Mar 2022 19:43:52 +0530 Subject: [PATCH] feat: show sign-in/sign-up errors under input instead of alert (#904) --- .../AccountMenu/ConfirmPassword.tsx | 28 +++++++++---------- .../components/AccountMenu/CreateAccount.tsx | 6 ++-- .../components/AccountMenu/SignIn.tsx | 26 ++++++----------- .../components/AccountMenu/index.tsx | 1 - 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/app/assets/javascripts/components/AccountMenu/ConfirmPassword.tsx b/app/assets/javascripts/components/AccountMenu/ConfirmPassword.tsx index b4d780545..25dd2c317 100644 --- a/app/assets/javascripts/components/AccountMenu/ConfirmPassword.tsx +++ b/app/assets/javascripts/components/AccountMenu/ConfirmPassword.tsx @@ -3,7 +3,7 @@ import { WebApplication } from '@/ui_models/application'; import { AppState } from '@/ui_models/app_state'; import { observer } from 'mobx-react-lite'; import { FunctionComponent } from 'preact'; -import { StateUpdater, useEffect, useRef, useState } from 'preact/hooks'; +import { useEffect, useRef, useState } from 'preact/hooks'; import { AccountMenuPane } from '.'; import { Button } from '../Button'; import { Checkbox } from '../Checkbox'; @@ -17,22 +17,22 @@ type Props = { setMenuPane: (pane: AccountMenuPane) => void; email: string; password: string; - setPassword: StateUpdater; }; export const ConfirmPassword: FunctionComponent = observer( - ({ application, appState, setMenuPane, email, password, setPassword }) => { + ({ application, appState, setMenuPane, email, password }) => { const { notesAndTagsCount } = appState.accountMenu; const [confirmPassword, setConfirmPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [isRegistering, setIsRegistering] = useState(false); const [isEphemeral, setIsEphemeral] = useState(false); const [shouldMergeLocal, setShouldMergeLocal] = useState(true); + const [error, setError] = useState(''); const passwordInputRef = useRef(null); useEffect(() => { - passwordInputRef?.current?.focus(); + passwordInputRef.current?.focus(); }, []); const handlePasswordChange = (e: Event) => { @@ -50,6 +50,9 @@ export const ConfirmPassword: FunctionComponent = observer( }; const handleKeyDown = (e: KeyboardEvent) => { + if (error.length) { + setError(''); + } if (e.key === 'Enter') { handleConfirmFormSubmit(e); } @@ -59,7 +62,7 @@ export const ConfirmPassword: FunctionComponent = observer( e.preventDefault(); if (!password) { - passwordInputRef?.current!.focus(); + passwordInputRef.current?.focus(); return; } @@ -76,21 +79,15 @@ export const ConfirmPassword: FunctionComponent = observer( }) .catch((err) => { console.error(err); - application.alertService.alert(err).finally(() => { - setPassword(''); - handleGoBack(); - }); + setError(err.message); }) .finally(() => { setIsRegistering(false); }); } else { - application.alertService - .alert(STRING_NON_MATCHING_PASSWORDS) - .finally(() => { - setConfirmPassword(''); - passwordInputRef?.current!.focus(); - }); + setError(STRING_NON_MATCHING_PASSWORDS); + setConfirmPassword(''); + passwordInputRef.current?.focus(); } }; @@ -138,6 +135,7 @@ export const ConfirmPassword: FunctionComponent = observer( ref={passwordInputRef} disabled={isRegistering} /> + {error ?
{error}
: null}