Skip to content

Commit 76a629c

Browse files
committed
Migrate loginform to react-final-form
1 parent c1e3d42 commit 76a629c

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

client/modules/User/components/LoginForm.jsx

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,38 @@ import PropTypes from 'prop-types';
22
import React from 'react';
33
import { withTranslation } from 'react-i18next';
44
import { Form, Field } from 'react-final-form';
5+
import { browserHistory } from 'react-router';
56
import { useDispatch, useSelector } from 'react-redux';
67
import Button from '../../../common/Button';
7-
88
import { validateLogin } from '../../../utils/reduxFormUtils';
99
import { loginUser, loginUserSuccess, setPreferences } from '../actions';
1010
import { setLanguage } from '../../IDE/actions/preferences';
1111
import { justOpenedProject } from '../../IDE/actions/ide';
1212

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-
}
28-
2913
function LoginForm(props) {
14+
const dispatch = useDispatch();
15+
const previousPath = useSelector(state => state.ide.previousPath);
16+
function validateAndLoginUser(formProps) {
17+
return new Promise((resolve, reject) => {
18+
loginUser(formProps)
19+
.then((response) => {
20+
dispatch(loginUserSuccess(response.data));
21+
dispatch(setPreferences(response.data.preferences));
22+
dispatch(setLanguage(response.data.preferences.language, { persistPreference: false }));
23+
dispatch(justOpenedProject());
24+
browserHistory.push(previousPath);
25+
resolve();
26+
})
27+
.catch(error =>
28+
reject({ password: error.response.data.message, _error: 'Login failed!' })); // eslint-disable-line
29+
});
30+
}
31+
3032
return (
3133
<Form
3234
fields={['email', 'password']}
3335
validate={validateLogin}
34-
onSubmit={props.validateAndLoginUser.bind(this, props.previousPath)}
36+
onSubmit={validateAndLoginUser}
3537
>
3638
{({
3739
handleSubmit, pristine, submitting
@@ -86,18 +88,7 @@ function LoginForm(props) {
8688
}
8789

8890
LoginForm.propTypes = {
89-
validateAndLoginUser: PropTypes.func.isRequired,
90-
submitting: PropTypes.bool,
91-
pristine: PropTypes.bool,
92-
invalid: PropTypes.bool,
93-
previousPath: PropTypes.string.isRequired,
9491
t: PropTypes.func.isRequired
9592
};
9693

97-
LoginForm.defaultProps = {
98-
submitting: false,
99-
pristine: true,
100-
invalid: false,
101-
};
102-
10394
export default withTranslation()(LoginForm);

client/modules/User/pages/LoginView.jsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class LoginView extends React.Component {
3838
</Helmet>
3939
<div className="form-container__content">
4040
<h2 className="form-container__title">{this.props.t('LoginView.Login')}</h2>
41-
<LoginForm {...this.props} />
41+
<LoginForm />
4242
<h2 className="form-container__divider">{this.props.t('LoginView.LoginOr')}</h2>
4343
<div className="form-container__stack">
4444
<SocialAuthButton service={SocialAuthButton.services.github} />
@@ -66,12 +66,6 @@ function mapStateToProps(state) {
6666
};
6767
}
6868

69-
function mapDispatchToProps() {
70-
return {
71-
validateAndLoginUser
72-
};
73-
}
74-
7569
LoginView.propTypes = {
7670
previousPath: PropTypes.string.isRequired,
7771
user: PropTypes.shape({
@@ -86,4 +80,4 @@ LoginView.defaultProps = {
8680
},
8781
};
8882

89-
export default withTranslation()(connect(mapStateToProps, mapDispatchToProps)(LoginView));
83+
export default withTranslation()(connect(mapStateToProps)(LoginView));

0 commit comments

Comments
 (0)