Skip to content

Commit

Permalink
Fixes form reinitialization on language change (#3229)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhigyanSrivastav committed Oct 30, 2024
1 parent 55ac03c commit a7b7d7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 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, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Form, Field } from 'react-final-form';
import { useDispatch } from 'react-redux';
Expand All @@ -8,22 +8,28 @@ 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 [formUpdateKey, setFormUpdateKey] = useState(false);

const handleVisibility = () => {
setShowPassword(!showPassword);
};
useEffect(() => {
setFormUpdateKey(!formUpdateKey);
}, [i18n.language]);

return (
<Form
fields={['email', 'password']}
validate={validateLogin}
onSubmit={onSubmit}
key={formUpdateKey}
>
{({ handleSubmit, submitError, submitting, modifiedSinceLastSubmit }) => (
<form className="form" onSubmit={handleSubmit}>
Expand Down
9 changes: 7 additions & 2 deletions client/modules/User/components/SignupForm.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Form, Field } from 'react-final-form';
import { useDispatch } from 'react-redux';
Expand Down Expand Up @@ -34,26 +34,31 @@ function validateEmail(email) {
}

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

const dispatch = useDispatch();
function onSubmit(formProps) {
return dispatch(validateAndSignUpUser(formProps));
}
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const [formUpdateKey, setFormUpdateKey] = useState(false);
const handleVisibility = () => {
setShowPassword(!showPassword);
};
const handleConfirmVisibility = () => {
setShowConfirmPassword(!showConfirmPassword);
};
useEffect(() => {
setFormUpdateKey(!formUpdateKey);
}, [i18n.language]);

return (
<Form
fields={['username', 'email', 'password', 'confirmPassword']}
validate={validateSignup}
onSubmit={onSubmit}
key={formUpdateKey}
>
{({ handleSubmit, pristine, submitting, invalid }) => (
<form className="form" onSubmit={handleSubmit}>
Expand Down

0 comments on commit a7b7d7f

Please sign in to comment.