Skip to content

Commit c1e3d42

Browse files
committed
Start migration to react-final-form
1 parent 0c28d98 commit c1e3d42

File tree

5 files changed

+581
-253
lines changed

5 files changed

+581
-253
lines changed

client/modules/User/actions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ export function loginUserFailure(error) {
4848
};
4949
}
5050

51+
export function setPreferences(preferences) {
52+
return {
53+
type: ActionTypes.SET_PREFERENCES,
54+
preferences
55+
};
56+
}
57+
5158
export function validateAndLoginUser(previousPath, formProps, dispatch) {
5259
return new Promise((resolve, reject) => {
5360
loginUser(formProps)

client/modules/User/components/LoginForm.jsx

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,91 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
33
import { withTranslation } from 'react-i18next';
4+
import { Form, Field } from 'react-final-form';
5+
import { useDispatch, useSelector } from 'react-redux';
46
import Button from '../../../common/Button';
57

6-
import { domOnlyProps } from '../../../utils/reduxFormUtils';
8+
import { validateLogin } from '../../../utils/reduxFormUtils';
9+
import { loginUser, loginUserSuccess, setPreferences } from '../actions';
10+
import { setLanguage } from '../../IDE/actions/preferences';
11+
import { justOpenedProject } from '../../IDE/actions/ide';
12+
13+
function validateAndLoginUser() {
14+
return new Promise((resolve, reject) => {
15+
loginUser(formProps)
16+
.then((response) => {
17+
dispatch(loginUserSuccess(response.data));
18+
dispatch(setPreferences(response.data.preferences));
19+
dispatch(setLanguage(response.data.preferences.language, { persistPreference: false }));
20+
dispatch(justOpenedProject());
21+
browserHistory.push(props.previousPath);
22+
resolve();
23+
})
24+
.catch(error =>
25+
reject({ password: error.response.data.message, _error: 'Login failed!' })); // eslint-disable-line
26+
});
27+
}
728

829
function LoginForm(props) {
9-
const {
10-
fields: { email, password },
11-
handleSubmit,
12-
submitting,
13-
pristine,
14-
} = props;
1530
return (
16-
<form
17-
className="form"
18-
onSubmit={handleSubmit(props.validateAndLoginUser.bind(this, props.previousPath))}
31+
<Form
32+
fields={['email', 'password']}
33+
validate={validateLogin}
34+
onSubmit={props.validateAndLoginUser.bind(this, props.previousPath)}
1935
>
20-
<p className="form__field">
21-
<label htmlFor="email" className="form__label">{props.t('LoginForm.UsernameOrEmail')}</label>
22-
<input
23-
className="form__input"
24-
aria-label={props.t('LoginForm.UsernameOrEmailARIA')}
25-
type="text"
26-
id="email"
27-
{...domOnlyProps(email)}
28-
/>
29-
{email.touched && email.error && (
30-
<span className="form-error">{email.error}</span>
31-
)}
32-
</p>
33-
<p className="form__field">
34-
<label htmlFor="password" className="form__label">{props.t('LoginForm.Password')}</label>
35-
<input
36-
className="form__input"
37-
aria-label={props.t('LoginForm.PasswordARIA')}
38-
type="password"
39-
id="password"
40-
{...domOnlyProps(password)}
41-
/>
42-
{password.touched && password.error && (
43-
<span className="form-error">{password.error}</span>
44-
)}
45-
</p>
46-
<Button
47-
type="submit"
48-
disabled={submitting || pristine}
49-
>{props.t('LoginForm.Submit')}
50-
</Button>
51-
</form>
36+
{({
37+
handleSubmit, pristine, submitting
38+
}) => (
39+
<form
40+
className="form"
41+
onSubmit={handleSubmit}
42+
>
43+
<Field name="email">
44+
{field => (
45+
<p className="form__field">
46+
<label htmlFor="email" className="form__label">{props.t('LoginForm.UsernameOrEmail')}</label>
47+
<input
48+
className="form__input"
49+
aria-label={props.t('LoginForm.UsernameOrEmailARIA')}
50+
type="text"
51+
id="email"
52+
{...field.input}
53+
/>
54+
{field.meta.touched && field.meta.error && (
55+
<span className="form-error">{field.meta.error}</span>
56+
)}
57+
</p>
58+
)}
59+
</Field>
60+
<Field name="password">
61+
{field => (
62+
<p className="form__field">
63+
<label htmlFor="password" className="form__label">{props.t('LoginForm.Password')}</label>
64+
<input
65+
className="form__input"
66+
aria-label={props.t('LoginForm.PasswordARIA')}
67+
type="password"
68+
id="password"
69+
{...field.input}
70+
/>
71+
{field.meta.touched && field.meta.error && (
72+
<span className="form-error">{field.meta.error}</span>
73+
)}
74+
</p>
75+
)}
76+
</Field>
77+
<Button
78+
type="submit"
79+
disabled={submitting || pristine}
80+
>{props.t('LoginForm.Submit')}
81+
</Button>
82+
</form>
83+
)}
84+
</Form>
5285
);
5386
}
5487

5588
LoginForm.propTypes = {
56-
fields: PropTypes.shape({
57-
email: PropTypes.object.isRequired, // eslint-disable-line
58-
password: PropTypes.object.isRequired, // eslint-disable-line
59-
}).isRequired,
60-
handleSubmit: PropTypes.func.isRequired,
6189
validateAndLoginUser: PropTypes.func.isRequired,
6290
submitting: PropTypes.bool,
6391
pristine: PropTypes.bool,

client/modules/User/pages/LoginView.jsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
3-
import { reduxForm } from 'redux-form';
3+
import { connect } from 'react-redux';
44
import { Link, browserHistory } from 'react-router';
55
import { Helmet } from 'react-helmet';
66
import { withTranslation } from 'react-i18next';
77
import { validateAndLoginUser } from '../actions';
88
import LoginForm from '../components/LoginForm';
9-
import { validateLogin } from '../../../utils/reduxFormUtils';
109
import SocialAuthButton from '../components/SocialAuthButton';
1110
import Nav from '../../../components/Nav';
12-
import ResponsiveForm from '../components/ResponsiveForm';
1311

1412
class LoginView extends React.Component {
1513
constructor(props) {
@@ -88,8 +86,4 @@ LoginView.defaultProps = {
8886
},
8987
};
9088

91-
export default withTranslation()(reduxForm({
92-
form: 'login',
93-
fields: ['email', 'password'],
94-
validate: validateLogin
95-
}, mapStateToProps, mapDispatchToProps)(LoginView));
89+
export default withTranslation()(connect(mapStateToProps, mapDispatchToProps)(LoginView));

0 commit comments

Comments
 (0)