2016-12-15 23:43:58 +00:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-06-23 22:29:55 +00:00
|
|
|
import { bindActionCreators } from 'redux';
|
2016-09-02 18:51:30 +00:00
|
|
|
import axios from 'axios';
|
2016-12-15 23:43:58 +00:00
|
|
|
import { Link, browserHistory } from 'react-router';
|
|
|
|
import InlineSVG from 'react-inlinesvg';
|
2017-02-22 19:29:35 +00:00
|
|
|
import { reduxForm } from 'redux-form';
|
|
|
|
import * as UserActions from '../actions';
|
|
|
|
import SignupForm from '../components/SignupForm';
|
|
|
|
|
2016-12-15 23:43:58 +00:00
|
|
|
const exitUrl = require('../../../images/exit.svg');
|
|
|
|
const logoUrl = require('../../../images/p5js-logo.svg');
|
2016-06-23 22:29:55 +00:00
|
|
|
|
2016-10-08 22:03:39 +00:00
|
|
|
class SignupView extends React.Component {
|
2016-12-15 23:43:58 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.closeSignupPage = this.closeSignupPage.bind(this);
|
|
|
|
this.gotoHomePage = this.gotoHomePage.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
closeSignupPage() {
|
|
|
|
browserHistory.push(this.props.previousPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
gotoHomePage() {
|
|
|
|
browserHistory.push('/');
|
2016-10-08 22:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2016-12-15 23:43:58 +00:00
|
|
|
<div className="form-container">
|
|
|
|
<div className="form-container__header">
|
|
|
|
<button className="form-container__logo-button" onClick={this.gotoHomePage}>
|
|
|
|
<InlineSVG src={logoUrl} alt="p5js Logo" />
|
|
|
|
</button>
|
|
|
|
<button className="form-container__exit-button" onClick={this.closeSignupPage}>
|
|
|
|
<InlineSVG src={exitUrl} alt="Close Signup Page" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div className="form-container__content">
|
|
|
|
<h2 className="form-container__title">Sign Up</h2>
|
|
|
|
<SignupForm {...this.props} />
|
|
|
|
<p className="form__navigation-options">
|
|
|
|
Already have an account?
|
|
|
|
<Link className="form__login-button" to="/login">Log In</Link>
|
|
|
|
</p>
|
|
|
|
</div>
|
2016-10-08 22:03:39 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 23:43:58 +00:00
|
|
|
function mapStateToProps(state) {
|
2016-06-23 22:29:55 +00:00
|
|
|
return {
|
2016-11-10 21:13:00 +00:00
|
|
|
user: state.user,
|
2016-12-15 23:43:58 +00:00
|
|
|
previousPath: state.ide.previousPath
|
2016-06-23 22:29:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(UserActions, dispatch);
|
|
|
|
}
|
|
|
|
|
2016-09-02 18:51:30 +00: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 })
|
2017-02-22 19:29:35 +00:00
|
|
|
.then((response) => {
|
2016-09-02 18:51:30 +00:00
|
|
|
if (response.data.exists) {
|
|
|
|
const error = {};
|
|
|
|
error[fieldToValidate] = response.data.message;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.resolve(true).then(() => {});
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
function validate(formProps) {
|
|
|
|
const errors = {};
|
2016-06-24 18:22:32 +00:00
|
|
|
|
|
|
|
if (!formProps.username) {
|
2016-09-02 17:05:42 +00: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 18:22:32 +00:00
|
|
|
}
|
2016-09-02 17:05:42 +00:00
|
|
|
|
2016-06-24 18:22:32 +00:00
|
|
|
if (!formProps.email) {
|
2016-09-02 17:05:42 +00:00
|
|
|
errors.email = 'Please enter an email.';
|
2017-02-22 19:29:35 +00:00
|
|
|
} else if (!formProps.email.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i)) {
|
2016-09-02 17:05:42 +00:00
|
|
|
errors.email = 'Please enter a valid email address.';
|
2016-06-24 18:22:32 +00:00
|
|
|
}
|
2016-09-02 17:05:42 +00:00
|
|
|
|
2016-06-24 18:22:32 +00:00
|
|
|
if (!formProps.password) {
|
|
|
|
errors.password = 'Please enter a password';
|
|
|
|
}
|
|
|
|
if (!formProps.confirmPassword) {
|
|
|
|
errors.confirmPassword = 'Please enter a password confirmation';
|
|
|
|
}
|
|
|
|
|
2016-06-24 22:08:52 +00:00
|
|
|
if (formProps.password !== formProps.confirmPassword) {
|
2016-06-24 18:22:32 +00:00
|
|
|
errors.password = 'Passwords must match';
|
|
|
|
}
|
2016-06-24 22:08:52 +00:00
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2016-09-02 18:51:30 +00:00
|
|
|
function onSubmitFail(errors) {
|
|
|
|
console.log(errors);
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:43:58 +00:00
|
|
|
SignupView.propTypes = {
|
|
|
|
previousPath: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export default reduxForm({
|
|
|
|
form: 'signup',
|
2016-06-24 22:08:52 +00:00
|
|
|
fields: ['username', 'email', 'password', 'confirmPassword'],
|
2016-09-02 18:51:30 +00:00
|
|
|
onSubmitFail,
|
|
|
|
validate,
|
|
|
|
asyncValidate,
|
|
|
|
asyncBlurFields: ['username', 'email']
|
2016-06-23 22:29:55 +00:00
|
|
|
}, mapStateToProps, mapDispatchToProps)(SignupView);
|