|
1 | 1 | import PropTypes from 'prop-types'; |
2 | 2 | import React from 'react'; |
3 | | -import { withTranslation } from 'react-i18next'; |
4 | | -import { domOnlyProps } from '../../../utils/reduxFormUtils'; |
| 3 | +import { Form, Field } from 'react-final-form'; |
| 4 | +import { useDispatch } from 'react-redux'; |
| 5 | +import { useTranslation } from 'react-i18next'; |
| 6 | +import { validateNewPassword } from '../../../utils/reduxFormUtils'; |
| 7 | +import { updatePassword } from '../actions'; |
5 | 8 | import Button from '../../../common/Button'; |
6 | 9 |
|
7 | 10 | function NewPasswordForm(props) { |
8 | | - const { |
9 | | - fields: { password, confirmPassword }, handleSubmit, submitting, invalid, pristine, |
10 | | - t |
11 | | - } = props; |
| 11 | + const { resetPasswordToken } = props; |
| 12 | + const { t } = useTranslation(); |
| 13 | + const dispatch = useDispatch(); |
| 14 | + |
| 15 | + function onSubmit(formProps) { |
| 16 | + return dispatch(updatePassword(formProps, resetPasswordToken)); |
| 17 | + } |
| 18 | + |
12 | 19 | return ( |
13 | | - <form |
14 | | - className="form" |
15 | | - onSubmit={handleSubmit(props.updatePassword.bind(this, props.params.reset_password_token))} |
| 20 | + <Form |
| 21 | + fields={['password', 'confirmPassword']} |
| 22 | + validate={validateNewPassword} |
| 23 | + onSubmit={onSubmit} |
16 | 24 | > |
17 | | - <p className="form__field"> |
18 | | - <label htmlFor="password" className="form__label">{t('NewPasswordForm.Title')}</label> |
19 | | - <input |
20 | | - className="form__input" |
21 | | - aria-label={t('NewPasswordForm.TitleARIA')} |
22 | | - type="password" |
23 | | - id="Password" |
24 | | - {...domOnlyProps(password)} |
25 | | - /> |
26 | | - {password.touched && password.error && ( |
27 | | - <span className="form-error">{password.error}</span> |
28 | | - )} |
29 | | - </p> |
30 | | - <p className="form__field"> |
31 | | - <label htmlFor="confirm password" className="form__label">{t('NewPasswordForm.ConfirmPassword')}</label> |
32 | | - <input |
33 | | - className="form__input" |
34 | | - type="password" |
35 | | - aria-label={t('NewPasswordForm.ConfirmPasswordARIA')} |
36 | | - id="confirm password" |
37 | | - {...domOnlyProps(confirmPassword)} |
38 | | - /> |
39 | | - {confirmPassword.touched && confirmPassword.error && ( |
40 | | - <span className="form-error">{confirmPassword.error}</span> |
41 | | - )} |
42 | | - </p> |
43 | | - <Button type="submit" disabled={submitting || invalid || pristine}>{t('NewPasswordForm.SubmitSetNewPassword')}</Button> |
44 | | - </form> |
| 25 | + {({ |
| 26 | + handleSubmit, submitting, invalid, pristine |
| 27 | + }) => ( |
| 28 | + <form |
| 29 | + className="form" |
| 30 | + onSubmit={handleSubmit} |
| 31 | + > |
| 32 | + <Field name="password"> |
| 33 | + {field => ( |
| 34 | + <p className="form__field"> |
| 35 | + <label htmlFor="password" className="form__label">{t('NewPasswordForm.Title')}</label> |
| 36 | + <input |
| 37 | + className="form__input" |
| 38 | + aria-label={t('NewPasswordForm.TitleARIA')} |
| 39 | + type="password" |
| 40 | + id="Password" |
| 41 | + {...field.input} |
| 42 | + /> |
| 43 | + {field.meta.touched && field.meta.error && ( |
| 44 | + <span className="form-error">{field.meta.error}</span> |
| 45 | + )} |
| 46 | + </p> |
| 47 | + )} |
| 48 | + </Field> |
| 49 | + <Field name="confirmPassword"> |
| 50 | + {field => ( |
| 51 | + <p className="form__field"> |
| 52 | + <label htmlFor="confirm password" className="form__label">{t('NewPasswordForm.ConfirmPassword')}</label> |
| 53 | + <input |
| 54 | + className="form__input" |
| 55 | + type="password" |
| 56 | + aria-label={t('NewPasswordForm.ConfirmPasswordARIA')} |
| 57 | + id="confirm password" |
| 58 | + {...field.input} |
| 59 | + /> |
| 60 | + {field.meta.touched && field.meta.error && ( |
| 61 | + <span className="form-error">{field.meta.error}</span> |
| 62 | + )} |
| 63 | + </p> |
| 64 | + )} |
| 65 | + </Field> |
| 66 | + <Button type="submit" disabled={submitting || invalid || pristine}>{t('NewPasswordForm.SubmitSetNewPassword')}</Button> |
| 67 | + </form> |
| 68 | + )} |
| 69 | + </Form> |
45 | 70 | ); |
46 | 71 | } |
47 | 72 |
|
48 | 73 | NewPasswordForm.propTypes = { |
49 | | - fields: PropTypes.shape({ |
50 | | - password: PropTypes.objectOf(PropTypes.shape()).isRequired, |
51 | | - confirmPassword: PropTypes.objectOf(PropTypes.shape()).isRequired, |
52 | | - }).isRequired, |
53 | | - handleSubmit: PropTypes.func.isRequired, |
54 | | - updatePassword: PropTypes.func.isRequired, |
55 | | - submitting: PropTypes.bool, |
56 | | - invalid: PropTypes.bool, |
57 | | - pristine: PropTypes.bool, |
58 | | - params: PropTypes.shape({ |
59 | | - reset_password_token: PropTypes.string, |
60 | | - }).isRequired, |
61 | | - t: PropTypes.func.isRequired |
62 | | -}; |
63 | | - |
64 | | -NewPasswordForm.defaultProps = { |
65 | | - invalid: false, |
66 | | - pristine: true, |
67 | | - submitting: false, |
| 74 | + resetPasswordToken: PropTypes.string.isRequired, |
68 | 75 | }; |
69 | 76 |
|
70 | | -export default withTranslation()(NewPasswordForm); |
| 77 | +export default NewPasswordForm; |
0 commit comments