2016-06-24 00:29:55 +02:00
|
|
|
import React from 'react';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import * as UserActions from '../actions';
|
|
|
|
import { reduxForm } from 'redux-form';
|
|
|
|
import SignupForm from '../components/SignupForm';
|
2016-09-02 20:51:30 +02:00
|
|
|
import axios from 'axios';
|
2016-09-02 23:37:34 +02:00
|
|
|
import { Link } from 'react-router';
|
2016-06-24 00:29:55 +02:00
|
|
|
|
2016-06-25 00:18:22 +02:00
|
|
|
function SignupView(props) {
|
|
|
|
return (
|
|
|
|
<div className="signup">
|
|
|
|
<h1>Sign Up</h1>
|
|
|
|
<SignupForm {...props} />
|
2016-09-02 23:37:34 +02:00
|
|
|
<Link to="/">Cancel</Link>
|
2016-06-25 00:18:22 +02:00
|
|
|
</div>
|
|
|
|
);
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
user: state.user
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(UserActions, dispatch);
|
|
|
|
}
|
|
|
|
|
2016-09-02 20:51:30 +02:00
|
|
|
function asyncValidate(formProps, dispatch, props) {
|
|
|
|
const fieldToValidate = props.form._active;
|
|
|
|
if (fieldToValidate) {
|
|
|
|
const queryParams = {};
|
|
|
|
queryParams[fieldToValidate] = formProps[fieldToValidate];
|
|
|
|
queryParams.check_type = fieldToValidate;
|
|
|
|
return axios.get('/api/signup/duplicate_check', { params: queryParams })
|
|
|
|
.then(response => {
|
|
|
|
if (response.data.exists) {
|
|
|
|
const error = {};
|
|
|
|
error[fieldToValidate] = response.data.message;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.resolve(true).then(() => {});
|
|
|
|
}
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
function validate(formProps) {
|
|
|
|
const errors = {};
|
2016-06-24 20:22:32 +02:00
|
|
|
|
|
|
|
if (!formProps.username) {
|
2016-09-02 19:05:42 +02:00
|
|
|
errors.username = 'Please enter a username.';
|
|
|
|
} else if (!formProps.username.match(/^.{1,20}$/)) {
|
|
|
|
errors.username = 'Username must be less than 20 characters.';
|
|
|
|
} else if (!formProps.username.match(/^[a-zA-Z0-9._-]{1,20}$/)) {
|
|
|
|
errors.username = 'Username must only consist of numbers, letters, periods, dashes, and underscores.';
|
2016-06-24 20:22:32 +02:00
|
|
|
}
|
2016-09-02 19:05:42 +02:00
|
|
|
|
2016-06-24 20:22:32 +02:00
|
|
|
if (!formProps.email) {
|
2016-09-02 19:05:42 +02:00
|
|
|
errors.email = 'Please enter an email.';
|
|
|
|
} else if (!formProps.email.match(/^[-a-z0-9~!$%^&*_=+}{'?]+(\.[-a-z0-9~!$%^&*_=+}{'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i)) {
|
|
|
|
errors.email = 'Please enter a valid email address.';
|
2016-06-24 20:22:32 +02:00
|
|
|
}
|
2016-09-02 19:05:42 +02:00
|
|
|
|
2016-06-24 20:22:32 +02:00
|
|
|
if (!formProps.password) {
|
|
|
|
errors.password = 'Please enter a password';
|
|
|
|
}
|
|
|
|
if (!formProps.confirmPassword) {
|
|
|
|
errors.confirmPassword = 'Please enter a password confirmation';
|
|
|
|
}
|
|
|
|
|
2016-06-25 00:08:52 +02:00
|
|
|
if (formProps.password !== formProps.confirmPassword) {
|
2016-06-24 20:22:32 +02:00
|
|
|
errors.password = 'Passwords must match';
|
|
|
|
}
|
2016-06-25 00:08:52 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2016-09-02 20:51:30 +02:00
|
|
|
function onSubmitFail(errors) {
|
|
|
|
console.log(errors);
|
|
|
|
}
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export default reduxForm({
|
|
|
|
form: 'signup',
|
2016-06-25 00:08:52 +02:00
|
|
|
fields: ['username', 'email', 'password', 'confirmPassword'],
|
2016-09-02 20:51:30 +02:00
|
|
|
onSubmitFail,
|
|
|
|
validate,
|
|
|
|
asyncValidate,
|
|
|
|
asyncBlurFields: ['username', 'email']
|
2016-06-24 00:29:55 +02:00
|
|
|
}, mapStateToProps, mapDispatchToProps)(SignupView);
|