fix(frontend): add error prop to password input

Signed-off-by: Avinash <avinash.kumar.cs92@gmail.com>
This commit is contained in:
Avinash 2023-01-19 11:35:42 +05:30 committed by Philip Molares
parent 58819f6018
commit dae3b9d8dc
4 changed files with 24 additions and 8 deletions

View file

@ -8,4 +8,5 @@ import type { ChangeEvent } from 'react'
export interface CommonFieldProps { export interface CommonFieldProps {
onChange: (event: ChangeEvent<HTMLInputElement>) => void onChange: (event: ChangeEvent<HTMLInputElement>) => void
value: string value: string
hasError?: boolean
} }

View file

@ -14,7 +14,7 @@ import { Trans, useTranslation } from 'react-i18next'
* @param onChange Hook that is called when the entered password changes. * @param onChange Hook that is called when the entered password changes.
* @param value The currently entered password. * @param value The currently entered password.
*/ */
export const NewPasswordField: React.FC<CommonFieldProps> = ({ onChange, value }) => { export const NewPasswordField: React.FC<CommonFieldProps> = ({ onChange, value, hasError = false }) => {
const { t } = useTranslation() const { t } = useTranslation()
const isValid = useMemo(() => { const isValid = useMemo(() => {
@ -30,6 +30,7 @@ export const NewPasswordField: React.FC<CommonFieldProps> = ({ onChange, value }
type='password' type='password'
size='sm' size='sm'
isValid={isValid} isValid={isValid}
isInvalid={hasError}
onChange={onChange} onChange={onChange}
placeholder={t('login.auth.password') ?? undefined} placeholder={t('login.auth.password') ?? undefined}
className='bg-dark text-light' className='bg-dark text-light'

View file

@ -19,16 +19,21 @@ interface PasswordAgainFieldProps extends CommonFieldProps {
* @param value The currently entered retype of the password. * @param value The currently entered retype of the password.
* @param password The password entered into the password input field. * @param password The password entered into the password input field.
*/ */
export const PasswordAgainField: React.FC<PasswordAgainFieldProps> = ({ onChange, value, password }) => { export const PasswordAgainField: React.FC<PasswordAgainFieldProps> = ({
onChange,
value,
password,
hasError = false
}) => {
const { t } = useTranslation() const { t } = useTranslation()
const isInvalid = useMemo(() => { const isInvalid = useMemo(() => {
return value !== '' && password !== value return value !== '' && password !== value && hasError
}, [password, value]) }, [password, value, hasError])
const isValid = useMemo(() => { const isValid = useMemo(() => {
return password !== '' && password === value return password !== '' && password === value && !hasError
}, [password, value]) }, [password, value, hasError])
return ( return (
<Form.Group> <Form.Group>

View file

@ -69,6 +69,10 @@ export const RegisterPage: NextPage = () => {
) )
}, [username, password, displayName, passwordAgain]) }, [username, password, displayName, passwordAgain])
const isWeakPassword = useMemo(() => {
return error === RegisterErrorType.PASSWORD_TOO_WEAK
}, [error])
const onUsernameChange = useOnInputChange(setUsername) const onUsernameChange = useOnInputChange(setUsername)
const onDisplayNameChange = useOnInputChange(setDisplayName) const onDisplayNameChange = useOnInputChange(setDisplayName)
const onPasswordChange = useOnInputChange(setPassword) const onPasswordChange = useOnInputChange(setPassword)
@ -95,8 +99,13 @@ export const RegisterPage: NextPage = () => {
<Form onSubmit={doRegisterSubmit}> <Form onSubmit={doRegisterSubmit}>
<UsernameField onChange={onUsernameChange} value={username} /> <UsernameField onChange={onUsernameChange} value={username} />
<DisplayNameField onChange={onDisplayNameChange} value={displayName} /> <DisplayNameField onChange={onDisplayNameChange} value={displayName} />
<NewPasswordField onChange={onPasswordChange} value={password} /> <NewPasswordField onChange={onPasswordChange} value={password} hasError={isWeakPassword} />
<PasswordAgainField password={password} onChange={onPasswordAgainChange} value={passwordAgain} /> <PasswordAgainField
password={password}
onChange={onPasswordAgainChange}
value={passwordAgain}
hasError={isWeakPassword}
/>
<RegisterInfos /> <RegisterInfos />