diff --git a/src/common/api/user.js b/src/common/api/user.js index 1fecfbf..51da725 100644 --- a/src/common/api/user.js +++ b/src/common/api/user.js @@ -4,6 +4,9 @@ export default (apiEngine) => ({ verifyEmail: ({ token }) => apiEngine.post('/api/users/email/verify', { data: { verifyEmailToken: token }, }), + requestVerifyEmail: (form) => ( + apiEngine.post('/api/users/email/request-verify', { data: form }) + ), login: (user) => apiEngine.post('/api/users/login', { data: user }), requestResetPassword: (form) => ( apiEngine.post('/api/users/password/request-reset', { data: form }) diff --git a/src/common/components/forms/user/VerifyEmailForm.js b/src/common/components/forms/user/VerifyEmailForm.js new file mode 100644 index 0000000..b2bb8ad --- /dev/null +++ b/src/common/components/forms/user/VerifyEmailForm.js @@ -0,0 +1,154 @@ +import React, { Component, PropTypes } from 'react'; +import { connect } from 'react-redux'; +import { push } from 'react-router-redux'; +import { Field, reduxForm } from 'redux-form'; +import Alert from 'react-bootstrap/lib/Alert'; +import Button from 'react-bootstrap/lib/Button'; +// import validator from 'validator'; +import FormNames from '../../../constants/FormNames'; +import userAPI from '../../../api/user'; +import { validateForm } from '../../../actions/formActions'; +import { pushErrors } from '../../../actions/errorActions'; +import { Form, FormField, FormFooter } from '../../utils/BsForm'; +import configs from '../../../../../configs/project/client'; + +export let validate = (values) => { + let errors = {}; + + // if (values.email && !validator.isEmail(values.email)) { + // errors.email = 'Not an email'; + // } + + if (!values.email) { + errors.email = 'Required'; + } + + if (configs.recaptcha && !values.recaptcha) { + errors.recaptcha = 'Required'; + } + + return errors; +}; + +let asyncValidate = (values, dispatch) => { + return dispatch(validateForm( + FormNames.USER_VERIFY_EMAIL, + 'email', + values.email + )).then((json) => { + let validationError = {}; + if (!json.isPassed) { + validationError.email = json.message; + throw validationError; + } + }); +}; + +class VerifyEmailForm extends Component { + constructor() { + super(); + this.handleSubmit = this._handleSubmit.bind(this); + this.handleCancleClick = this._handleCancleClick.bind(this); + } + + componentDidMount() { + let { email, initialize } = this.props; + + if (email) { + initialize({ email }); + } + } + + _handleSubmit(formData) { + let { dispatch, apiEngine, initialize } = this.props; + + return userAPI(apiEngine) + .requestVerifyEmail(formData) + .catch((err) => { + dispatch(pushErrors(err)); + throw err; + }) + .then((json) => { + initialize({ + email: '', + }); + }); + } + + _handleCancleClick() { + let { onCancel, dispatch } = this.props; + + if (onCancel) { + onCancel(); + } else { + dispatch(push('/')); + } + } + + render() { + const { + email, + handleSubmit, + submitSucceeded, + submitFailed, + error, + pristine, + submitting, + invalid, + } = this.props; + + return ( +
+ {submitSucceeded && ( + A reset link is sent + )} + {submitFailed && error && ({error})} + + + +