Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 92 additions & 65 deletions client/modules/User/components/LoginForm.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Form, Field } from 'react-final-form';
import { useDispatch } from 'react-redux';
Expand All @@ -8,92 +8,119 @@ import { validateLogin } from '../../../utils/reduxFormUtils';
import { validateAndLoginUser } from '../actions';

function LoginForm() {
const { t } = useTranslation();
const { t, i18n } = useTranslation();

const dispatch = useDispatch();
function onSubmit(formProps) {
return dispatch(validateAndLoginUser(formProps));
}
const [showPassword, setShowPassword] = useState(false);
const formRef = useRef(null);

const handleVisibility = () => {
setShowPassword(!showPassword);
};
useEffect(() => {
const form = formRef.current;
if (!form) return;

const { values } = form.getState(); // store current form touched values
form.reset();

// Restore prev form values and trigger validation
Object.keys(values).forEach((field) => {
if (values[field]) {
// Only reapply touched values
form.change(field, values[field]);
}
});
}, [i18n.language]);

return (
<Form
fields={['email', 'password']}
validate={validateLogin}
onSubmit={onSubmit}
>
{({ handleSubmit, submitError, submitting, modifiedSinceLastSubmit }) => (
<form className="form" onSubmit={handleSubmit}>
<Field name="email">
{(field) => (
<div className="form__field">
<label htmlFor="email" className="form__label">
{t('LoginForm.UsernameOrEmail')}
</label>
<input
className="form__input"
aria-label={t('LoginForm.UsernameOrEmailARIA')}
type="text"
id="email"
autoComplete="username"
autoCapitalize="none"
{...field.input}
/>
{field.meta.touched && field.meta.error && (
<span className="form-error" aria-live="polite">
{field.meta.error}
</span>
)}
</div>
)}
</Field>
<Field name="password">
{(field) => (
<div className="form__field">
<label htmlFor="password" className="form__label">
{t('LoginForm.Password')}
</label>
<div className="form__field__password">
{({
handleSubmit,
submitError,
submitting,
modifiedSinceLastSubmit,
form
}) => {
formRef.current = form;

return (
<form className="form" onSubmit={handleSubmit}>
<Field name="email">
{(field) => (
<div className="form__field">
<label htmlFor="email" className="form__label">
{t('LoginForm.UsernameOrEmail')}
</label>
<input
className="form__input"
aria-label={t('LoginForm.PasswordARIA')}
type={showPassword ? 'text' : 'password'}
id="password"
autoComplete="current-password"
aria-label={t('LoginForm.UsernameOrEmailARIA')}
type="text"
id="email"
autoComplete="username"
autoCapitalize="none"
{...field.input}
/>
<button
className="form__eye__icon"
type="button"
onClick={handleVisibility}
aria-hidden="true"
>
{showPassword ? (
<AiOutlineEyeInvisible />
) : (
<AiOutlineEye />
)}
</button>
{field.meta.touched && field.meta.error && (
<span className="form-error" aria-live="polite">
{field.meta.error}
</span>
)}
</div>
)}
</Field>
<Field name="password">
{(field) => (
<div className="form__field">
<label htmlFor="password" className="form__label">
{t('LoginForm.Password')}
</label>
<div className="form__field__password">
<input
className="form__input"
aria-label={t('LoginForm.PasswordARIA')}
type={showPassword ? 'text' : 'password'}
id="password"
autoComplete="current-password"
{...field.input}
/>
<button
className="form__eye__icon"
type="button"
onClick={handleVisibility}
aria-hidden="true"
>
{showPassword ? (
<AiOutlineEyeInvisible />
) : (
<AiOutlineEye />
)}
</button>
</div>
{field.meta.touched && field.meta.error && (
<span className="form-error" aria-live="polite">
{field.meta.error}
</span>
)}
</div>
{field.meta.touched && field.meta.error && (
<span className="form-error" aria-live="polite">
{field.meta.error}
</span>
)}
</div>
)}
</Field>
{submitError && !modifiedSinceLastSubmit && (
<span className="form-error">{submitError}</span>
)}
</Field>
{submitError && !modifiedSinceLastSubmit && (
<span className="form-error">{submitError}</span>
)}
<Button type="submit" disabled={submitting}>
{t('LoginForm.Submit')}
</Button>
</form>
)}
<Button type="submit" disabled={submitting}>
{t('LoginForm.Submit')}
</Button>
</form>
);
}}
</Form>
);
}
Expand Down
Loading
Loading