Skip to content

Commit f188564

Browse files
committed
Add VerifyEmailForm
1 parent 8939a9e commit f188564

File tree

4 files changed

+165
-0
lines changed

4 files changed

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

src/common/constants/FormNames.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export default {
33
USER_LOGIN: 'USER_LOGIN',
44
USER_EDIT: 'USER_EDIT',
55
USER_AVATAR: 'USER_AVATAR',
6+
USER_VERIFY_EMAIL: 'USER_VERIFY_EMAIL',
67
USER_CHANGE_PASSWORD: 'USER_CHANGE_PASSWORD',
78
USER_FORGET_PASSWORD: 'USER_FORGET_PASSWORD',
89
USER_RESET_PASSWORD: 'USER_RESET_PASSWORD',

src/server/controllers/formValidation.js

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ export default {
2222
},
2323
},
2424

25+
[FormNames.USER_VERIFY_EMAIL]: {
26+
email(req, res) {
27+
User.findOne({
28+
'email.value': req.body.value,
29+
}, handleDbError(res)((user) => {
30+
if (!user) {
31+
res.json({
32+
isPassed: false,
33+
message: 'This is an invalid account',
34+
});
35+
} else {
36+
res.json({
37+
isPassed: true,
38+
});
39+
}
40+
}));
41+
},
42+
},
43+
2544
[FormNames.USER_FORGET_PASSWORD]: {
2645
email(req, res) {
2746
User.findOne({

src/server/routes/api.js

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export default ({ app }) => {
9090
bodyParser.json,
9191
formValidationController[FormNames.USER_REGISTER].email
9292
);
93+
app.post(
94+
`/api/forms/${FormNames.USER_VERIFY_EMAIL}/fields/email/validation`,
95+
bodyParser.json,
96+
formValidationController[FormNames.USER_VERIFY_EMAIL].email
97+
);
9398
app.post(
9499
`/api/forms/${FormNames.USER_FORGET_PASSWORD}/fields/email/validation`,
95100
bodyParser.json,

0 commit comments

Comments
 (0)