2018-02-07 19:06:07 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-06-24 00:29:55 +02:00
|
|
|
import { bindActionCreators } from 'redux';
|
2016-09-02 20:51:30 +02:00
|
|
|
import axios from 'axios';
|
2016-12-16 00:43:58 +01:00
|
|
|
import { Link, browserHistory } from 'react-router';
|
2018-02-23 17:31:41 +01:00
|
|
|
import { Helmet } from 'react-helmet';
|
2017-02-22 20:29:35 +01:00
|
|
|
import { reduxForm } from 'redux-form';
|
|
|
|
import * as UserActions from '../actions';
|
|
|
|
import SignupForm from '../components/SignupForm';
|
2017-03-16 23:25:12 +01:00
|
|
|
import { validateSignup } from '../../../utils/reduxFormUtils';
|
2020-05-28 20:46:44 +02:00
|
|
|
import SocialAuthButton from '../components/SocialAuthButton';
|
2019-09-19 19:38:27 +02:00
|
|
|
import Nav from '../../../components/Nav';
|
2017-02-22 20:29:35 +01:00
|
|
|
|
2019-09-19 19:38:27 +02:00
|
|
|
const __process = (typeof global !== 'undefined' ? global : window).process;
|
|
|
|
const ROOT_URL = __process.env.API_URL;
|
2016-06-24 00:29:55 +02:00
|
|
|
|
2016-10-09 00:03:39 +02:00
|
|
|
class SignupView extends React.Component {
|
2019-09-19 19:38:27 +02:00
|
|
|
gotoHomePage = () => {
|
2016-12-16 00:43:58 +01:00
|
|
|
browserHistory.push('/');
|
2016-10-09 00:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-09-29 20:52:20 +02:00
|
|
|
if (this.props.user.authenticated) {
|
|
|
|
this.gotoHomePage();
|
|
|
|
return null;
|
|
|
|
}
|
2016-10-09 00:03:39 +02:00
|
|
|
return (
|
2019-09-19 19:38:27 +02:00
|
|
|
<div className="signup">
|
|
|
|
<Nav layout="dashboard" />
|
2020-05-19 21:34:00 +02:00
|
|
|
<main className="form-container">
|
2019-06-04 14:57:48 +02:00
|
|
|
<Helmet>
|
|
|
|
<title>p5.js Web Editor | Signup</title>
|
|
|
|
</Helmet>
|
|
|
|
<div className="form-container__content">
|
|
|
|
<h2 className="form-container__title">Sign Up</h2>
|
|
|
|
<SignupForm {...this.props} />
|
2020-05-28 20:46:44 +02:00
|
|
|
<h2 className="form-container__divider">Or</h2>
|
|
|
|
<div className="form-container__stack">
|
|
|
|
<SocialAuthButton service={SocialAuthButton.services.github} />
|
|
|
|
<SocialAuthButton service={SocialAuthButton.services.google} />
|
|
|
|
</div>
|
2019-06-04 14:57:48 +02:00
|
|
|
<p className="form__navigation-options">
|
|
|
|
Already have an account?
|
|
|
|
<Link className="form__login-button" to="/login">Log In</Link>
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-05-19 21:34:00 +02:00
|
|
|
</main>
|
2016-10-09 00:03:39 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:33:37 +01:00
|
|
|
function asyncErrorsSelector(formName, state) {
|
|
|
|
const form = state.form[formName];
|
|
|
|
if (!form) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const fieldNames = Object.keys(form).filter(key => !key.startsWith('_'));
|
|
|
|
return fieldNames.reduce((asyncErrors, fieldName) => {
|
|
|
|
if (form[fieldName].asyncError) {
|
|
|
|
return { ...asyncErrors, [fieldName]: form[fieldName].asyncError };
|
|
|
|
}
|
|
|
|
return asyncErrors;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:43:58 +01:00
|
|
|
function mapStateToProps(state) {
|
2016-06-24 00:29:55 +02:00
|
|
|
return {
|
2016-11-10 22:13:00 +01:00
|
|
|
user: state.user,
|
2018-10-30 00:33:37 +01:00
|
|
|
previousPath: state.ide.previousPath,
|
|
|
|
asyncErrors: asyncErrorsSelector('signup', state)
|
2016-06-24 00:29:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(UserActions, dispatch);
|
|
|
|
}
|
|
|
|
|
2016-09-02 20:51:30 +02:00
|
|
|
function asyncValidate(formProps, dispatch, props) {
|
2018-10-30 00:33:37 +01:00
|
|
|
const errors = {};
|
|
|
|
return Promise.resolve(true)
|
|
|
|
.then(() => {
|
|
|
|
const fieldToValidate = props.form._active;
|
|
|
|
if (fieldToValidate) {
|
|
|
|
const queryParams = {};
|
|
|
|
queryParams[fieldToValidate] = formProps[fieldToValidate];
|
|
|
|
queryParams.check_type = fieldToValidate;
|
2019-09-19 19:38:27 +02:00
|
|
|
return axios.get(`${ROOT_URL}/signup/duplicate_check`, { params: queryParams })
|
2018-10-30 00:33:37 +01:00
|
|
|
.then((response) => {
|
|
|
|
if (response.data.exists) {
|
|
|
|
errors[fieldToValidate] = response.data.message;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
const err = { ...errors, ...props.asyncErrors };
|
|
|
|
if (Object.keys(err).length > 0) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
2016-09-02 20:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSubmitFail(errors) {
|
|
|
|
console.log(errors);
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:43:58 +01:00
|
|
|
SignupView.propTypes = {
|
2017-09-29 20:52:20 +02:00
|
|
|
previousPath: PropTypes.string.isRequired,
|
2019-09-19 19:38:27 +02:00
|
|
|
user: PropTypes.shape({
|
2017-09-29 20:52:20 +02:00
|
|
|
authenticated: PropTypes.bool
|
2019-09-19 19:38:27 +02:00
|
|
|
})
|
2017-09-29 20:52:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
SignupView.defaultProps = {
|
|
|
|
user: {
|
|
|
|
authenticated: false
|
|
|
|
}
|
2016-12-16 00:43:58 +01:00
|
|
|
};
|
|
|
|
|
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,
|
2017-03-16 23:25:12 +01:00
|
|
|
validate: validateSignup,
|
2016-09-02 20:51:30 +02:00
|
|
|
asyncValidate,
|
|
|
|
asyncBlurFields: ['username', 'email']
|
2016-06-24 00:29:55 +02:00
|
|
|
}, mapStateToProps, mapDispatchToProps)(SignupView);
|