|
| 1 | +import React, { Component, PropTypes } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import { push } from 'react-router-redux'; |
| 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 validator from 'validator'; |
| 8 | +import FormNames from '../../../constants/FormNames'; |
| 9 | +import userAPI from '../../../api/user'; |
| 10 | +import { validateForm } from '../../../actions/formActions'; |
| 11 | +import { pushErrors } from '../../../actions/errorActions'; |
| 12 | +import { Form, FormField, FormFooter } from '../../utils/BsForm'; |
| 13 | +import configs from '../../../../../configs/project/client'; |
| 14 | + |
| 15 | +export let validate = (values) => { |
| 16 | + let errors = {}; |
| 17 | + |
| 18 | + // if (values.email && !validator.isEmail(values.email)) { |
| 19 | + // errors.email = 'Not an email'; |
| 20 | + // } |
| 21 | + |
| 22 | + if (!values.email) { |
| 23 | + errors.email = 'Required'; |
| 24 | + } |
| 25 | + |
| 26 | + if (configs.recaptcha && !values.recaptcha) { |
| 27 | + errors.recaptcha = 'Required'; |
| 28 | + } |
| 29 | + |
| 30 | + return errors; |
| 31 | +}; |
| 32 | + |
| 33 | +let asyncValidate = (values, dispatch) => { |
| 34 | + return dispatch(validateForm( |
| 35 | + FormNames.USER_VERIFY_EMAIL, |
| 36 | + 'email', |
| 37 | + values.email |
| 38 | + )).then((json) => { |
| 39 | + let validationError = {}; |
| 40 | + if (!json.isPassed) { |
| 41 | + validationError.email = json.message; |
| 42 | + throw validationError; |
| 43 | + } |
| 44 | + }); |
| 45 | +}; |
| 46 | + |
| 47 | +class VerifyEmailForm extends Component { |
| 48 | + constructor() { |
| 49 | + super(); |
| 50 | + this.handleSubmit = this._handleSubmit.bind(this); |
| 51 | + this.handleCancleClick = this._handleCancleClick.bind(this); |
| 52 | + } |
| 53 | + |
| 54 | + componentDidMount() { |
| 55 | + let { email, initialize } = this.props; |
| 56 | + |
| 57 | + if (email) { |
| 58 | + initialize({ email }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + _handleSubmit(formData) { |
| 63 | + let { dispatch, apiEngine, initialize } = this.props; |
| 64 | + |
| 65 | + return userAPI(apiEngine) |
| 66 | + .requestVerifyEmail(formData) |
| 67 | + .catch((err) => { |
| 68 | + dispatch(pushErrors(err)); |
| 69 | + throw err; |
| 70 | + }) |
| 71 | + .then((json) => { |
| 72 | + initialize({ |
| 73 | + email: '', |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + _handleCancleClick() { |
| 79 | + let { onCancel, dispatch } = this.props; |
| 80 | + |
| 81 | + if (onCancel) { |
| 82 | + onCancel(); |
| 83 | + } else { |
| 84 | + dispatch(push('/')); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + render() { |
| 89 | + const { |
| 90 | + email, |
| 91 | + handleSubmit, |
| 92 | + submitSucceeded, |
| 93 | + submitFailed, |
| 94 | + error, |
| 95 | + pristine, |
| 96 | + submitting, |
| 97 | + invalid, |
| 98 | + } = this.props; |
| 99 | + |
| 100 | + return ( |
| 101 | + <Form horizontal onSubmit={handleSubmit(this.handleSubmit)}> |
| 102 | + {submitSucceeded && ( |
| 103 | + <Alert bsStyle="success">A reset link is sent</Alert> |
| 104 | + )} |
| 105 | + {submitFailed && error && (<Alert bsStyle="danger">{error}</Alert>)} |
| 106 | + <Field |
| 107 | + label="Email" |
| 108 | + name="email" |
| 109 | + component={FormField} |
| 110 | + type="text" |
| 111 | + disabled={Boolean(email)} |
| 112 | + placeholder="Email" |
| 113 | + /> |
| 114 | + <Field |
| 115 | + label=" " |
| 116 | + name="recaptcha" |
| 117 | + component={FormField} |
| 118 | + type="recaptcha" |
| 119 | + /> |
| 120 | + <FormFooter> |
| 121 | + <Button |
| 122 | + type="submit" |
| 123 | + disabled={(!email && pristine) || submitting || invalid} |
| 124 | + > |
| 125 | + Send An Email to Verify My Email Address |
| 126 | + {submitting && ( |
| 127 | + <i className="fa fa-spinner fa-spin" aria-hidden="true" /> |
| 128 | + )} |
| 129 | + </Button> |
| 130 | + <Button |
| 131 | + bsStyle="link" |
| 132 | + onClick={this.handleCancleClick} |
| 133 | + > |
| 134 | + Cancel |
| 135 | + </Button> |
| 136 | + </FormFooter> |
| 137 | + </Form> |
| 138 | + ); |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +VerifyEmailForm.propTypes = { |
| 143 | + email: PropTypes.string, |
| 144 | + onCancel: PropTypes.func, |
| 145 | +}; |
| 146 | + |
| 147 | +export default reduxForm({ |
| 148 | + form: FormNames.USER_VERIFY_EMAIL, |
| 149 | + validate, |
| 150 | + asyncValidate, |
| 151 | + asyncBlurFields: ['email'], |
| 152 | +})(connect(state => ({ |
| 153 | + apiEngine: state.apiEngine, |
| 154 | +}))(VerifyEmailForm)); |
0 commit comments