2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-06-23 22:29:55 +00:00
|
|
|
import { bindActionCreators } from 'redux';
|
2016-12-15 23:43:58 +00:00
|
|
|
import { Link, browserHistory } from 'react-router';
|
2018-02-23 16:31:41 +00:00
|
|
|
import { Helmet } from 'react-helmet';
|
2017-02-22 19:29:35 +00:00
|
|
|
import { reduxForm } from 'redux-form';
|
2020-08-17 09:51:59 +00:00
|
|
|
import { withTranslation } from 'react-i18next';
|
2017-02-22 19:29:35 +00:00
|
|
|
import * as UserActions from '../actions';
|
|
|
|
import SignupForm from '../components/SignupForm';
|
2020-06-08 10:29:24 +00:00
|
|
|
import apiClient from '../../../utils/apiClient';
|
2017-03-16 22:25:12 +00:00
|
|
|
import { validateSignup } from '../../../utils/reduxFormUtils';
|
2020-05-28 18:46:44 +00:00
|
|
|
import SocialAuthButton from '../components/SocialAuthButton';
|
2019-09-19 17:38:27 +00:00
|
|
|
import Nav from '../../../components/Nav';
|
2020-08-21 18:09:50 +00:00
|
|
|
import ResponsiveForm from '../components/ResponsiveForm';
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2016-10-08 22:03:39 +00:00
|
|
|
class SignupView extends React.Component {
|
2019-09-19 17:38:27 +00:00
|
|
|
gotoHomePage = () => {
|
2016-12-15 23:43:58 +00:00
|
|
|
browserHistory.push('/');
|
2016-10-08 22:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-09-29 18:52:20 +00:00
|
|
|
if (this.props.user.authenticated) {
|
|
|
|
this.gotoHomePage();
|
|
|
|
return null;
|
|
|
|
}
|
2016-10-08 22:03:39 +00:00
|
|
|
return (
|
2020-08-21 20:03:04 +00:00
|
|
|
<div className="signup">
|
|
|
|
<Nav layout="dashboard" />
|
|
|
|
<main className="form-container">
|
|
|
|
<Helmet>
|
|
|
|
<title>{this.props.t('SignupView.Title')}</title>
|
|
|
|
</Helmet>
|
|
|
|
<div className="form-container__content">
|
|
|
|
<h2 className="form-container__title">{this.props.t('SignupView.Description')}</h2>
|
|
|
|
<SignupForm {...this.props} />
|
|
|
|
<h2 className="form-container__divider">{this.props.t('SignupView.Or')}</h2>
|
|
|
|
<div className="form-container__stack">
|
|
|
|
<SocialAuthButton service={SocialAuthButton.services.github} />
|
|
|
|
<SocialAuthButton service={SocialAuthButton.services.google} />
|
2020-05-28 18:46:44 +00:00
|
|
|
</div>
|
2020-08-21 20:03:04 +00:00
|
|
|
<p className="form__navigation-options">
|
|
|
|
{this.props.t('SignupView.AlreadyHave')}
|
|
|
|
<Link className="form__login-button" to="/login">{this.props.t('SignupView.Login')}</Link>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</main>
|
|
|
|
</div>
|
2016-10-08 22:03:39 +00:00
|
|
|
);
|
|
|
|
}
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 23:33:37 +00: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-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,
|
2018-10-29 23:33:37 +00:00
|
|
|
previousPath: state.ide.previousPath,
|
|
|
|
asyncErrors: asyncErrorsSelector('signup', state)
|
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) {
|
2018-10-29 23:33:37 +00: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;
|
2020-06-08 10:29:24 +00:00
|
|
|
return apiClient.get('/signup/duplicate_check', { params: queryParams })
|
2018-10-29 23:33:37 +00: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 18:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSubmitFail(errors) {
|
|
|
|
console.log(errors);
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:43:58 +00:00
|
|
|
SignupView.propTypes = {
|
2017-09-29 18:52:20 +00:00
|
|
|
previousPath: PropTypes.string.isRequired,
|
2019-09-19 17:38:27 +00:00
|
|
|
user: PropTypes.shape({
|
2017-09-29 18:52:20 +00:00
|
|
|
authenticated: PropTypes.bool
|
2020-08-17 09:51:59 +00:00
|
|
|
}),
|
2020-08-21 18:09:50 +00:00
|
|
|
t: PropTypes.func.isRequired,
|
2017-09-29 18:52:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SignupView.defaultProps = {
|
|
|
|
user: {
|
|
|
|
authenticated: false
|
2020-08-21 18:09:50 +00:00
|
|
|
},
|
2016-12-15 23:43:58 +00:00
|
|
|
};
|
|
|
|
|
2020-08-17 09:51:59 +00:00
|
|
|
export default withTranslation()(reduxForm({
|
2016-06-23 22:29:55 +00:00
|
|
|
form: 'signup',
|
2016-06-24 22:08:52 +00:00
|
|
|
fields: ['username', 'email', 'password', 'confirmPassword'],
|
2016-09-02 18:51:30 +00:00
|
|
|
onSubmitFail,
|
2017-03-16 22:25:12 +00:00
|
|
|
validate: validateSignup,
|
2016-09-02 18:51:30 +00:00
|
|
|
asyncValidate,
|
|
|
|
asyncBlurFields: ['username', 'email']
|
2020-08-17 09:51:59 +00:00
|
|
|
}, mapStateToProps, mapDispatchToProps)(SignupView));
|