|
1 | 1 | import PropTypes from 'prop-types';
|
2 | 2 | import React from 'react';
|
3 | 3 | import { withTranslation } from 'react-i18next';
|
| 4 | +import { Form, Field } from 'react-final-form'; |
| 5 | +import { useDispatch, useSelector } from 'react-redux'; |
4 | 6 | import Button from '../../../common/Button';
|
5 | 7 |
|
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 | +} |
7 | 28 |
|
8 | 29 | function LoginForm(props) {
|
9 |
| - const { |
10 |
| - fields: { email, password }, |
11 |
| - handleSubmit, |
12 |
| - submitting, |
13 |
| - pristine, |
14 |
| - } = props; |
15 | 30 | 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)} |
19 | 35 | >
|
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> |
52 | 85 | );
|
53 | 86 | }
|
54 | 87 |
|
55 | 88 | 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, |
61 | 89 | validateAndLoginUser: PropTypes.func.isRequired,
|
62 | 90 | submitting: PropTypes.bool,
|
63 | 91 | pristine: PropTypes.bool,
|
|
0 commit comments