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