|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import { Field, reduxForm, SubmissionError } from 'redux-form'; |
| 4 | +import Alert from 'react-bootstrap/lib/Alert'; |
| 5 | +import Button from 'react-bootstrap/lib/Button'; |
| 6 | +import userAPI from '../../../api/user'; |
| 7 | +import { pushErrors } from '../../../actions/errorActions'; |
| 8 | +import { Form, FormField, FormFooter } from '../../utils/BsForm'; |
| 9 | + |
| 10 | +export const validate = (values) => { |
| 11 | + const errors = {}; |
| 12 | + |
| 13 | + if ( |
| 14 | + values.newPasswordConfirm && |
| 15 | + values.newPassword !== values.newPasswordConfirm |
| 16 | + ) { |
| 17 | + errors.newPassword = errors.newPasswordConfirm = 'Password Not Matched'; |
| 18 | + } |
| 19 | + |
| 20 | + if (values.oldPassword === values.newPassword) { |
| 21 | + errors.newPassword = 'Cannot be same as old password'; |
| 22 | + } |
| 23 | + |
| 24 | + if (!values.oldPassword) { |
| 25 | + errors.oldPassword = 'Required'; |
| 26 | + } |
| 27 | + |
| 28 | + if (!values.newPassword) { |
| 29 | + errors.newPassword = 'Required'; |
| 30 | + } |
| 31 | + |
| 32 | + if (!values.newPasswordConfirm) { |
| 33 | + errors.newPasswordConfirm = 'Required'; |
| 34 | + } |
| 35 | + |
| 36 | + return errors; |
| 37 | +}; |
| 38 | + |
| 39 | +class ChangePasswordForm extends Component { |
| 40 | + constructor(props) { |
| 41 | + super(props); |
| 42 | + this.handleSubmit = this._handleSubmit.bind(this); |
| 43 | + } |
| 44 | + |
| 45 | + _handleSubmit(formData) { |
| 46 | + let { dispatch, apiEngine, initialize } = this.props; |
| 47 | + |
| 48 | + return userAPI(apiEngine) |
| 49 | + .updatePassword(formData) |
| 50 | + .catch((err) => { |
| 51 | + dispatch(pushErrors(err)); |
| 52 | + throw err; |
| 53 | + }) |
| 54 | + .then((json) => { |
| 55 | + if (json.isAuth) { |
| 56 | + initialize({ |
| 57 | + oldPassword: '', |
| 58 | + newPassword: '', |
| 59 | + newPasswordConfirm: '', |
| 60 | + }); |
| 61 | + } else { |
| 62 | + throw new SubmissionError({ |
| 63 | + oldPassword: 'Wrong old password', |
| 64 | + _error: 'Change password failed', |
| 65 | + }); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + render() { |
| 71 | + const { |
| 72 | + handleSubmit, |
| 73 | + submitSucceeded, |
| 74 | + submitFailed, |
| 75 | + error, |
| 76 | + pristine, |
| 77 | + submitting, |
| 78 | + invalid, |
| 79 | + } = this.props; |
| 80 | + |
| 81 | + return ( |
| 82 | + <Form horizontal onSubmit={handleSubmit(this.handleSubmit)}> |
| 83 | + {submitSucceeded && (<Alert bsStyle="success">Password Changed</Alert>)} |
| 84 | + {submitFailed && error && (<Alert bsStyle="danger">{error}</Alert>)} |
| 85 | + <Field |
| 86 | + label="Old Password" |
| 87 | + name="oldPassword" |
| 88 | + component={FormField} |
| 89 | + type="password" |
| 90 | + placeholder="Old Password" |
| 91 | + /> |
| 92 | + <Field |
| 93 | + label="New Password" |
| 94 | + name="newPassword" |
| 95 | + component={FormField} |
| 96 | + type="password" |
| 97 | + placeholder="New Password" |
| 98 | + /> |
| 99 | + <Field |
| 100 | + label="New Password Confirm" |
| 101 | + name="newPasswordConfirm" |
| 102 | + component={FormField} |
| 103 | + type="password" |
| 104 | + placeholder="New Password Confirm" |
| 105 | + /> |
| 106 | + <FormFooter> |
| 107 | + <Button type="submit" disabled={pristine || submitting || invalid}> |
| 108 | + Change |
| 109 | + {submitting && ( |
| 110 | + <i className="fa fa-spinner fa-spin" aria-hidden="true" /> |
| 111 | + )} |
| 112 | + </Button> |
| 113 | + </FormFooter> |
| 114 | + </Form> |
| 115 | + ); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +export default reduxForm({ |
| 120 | + form: 'userChangePassword', |
| 121 | + validate, |
| 122 | +})(connect(state => ({ |
| 123 | + apiEngine: state.apiEngine, |
| 124 | +}))(ChangePasswordForm)); |
0 commit comments