2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2017-03-16 22:25:12 +00:00
|
|
|
import { reduxForm } from 'redux-form';
|
|
|
|
import { bindActionCreators } from 'redux';
|
2018-10-13 20:14:46 +00:00
|
|
|
import { Link, browserHistory } from 'react-router';
|
2017-03-16 22:25:12 +00:00
|
|
|
import InlineSVG from 'react-inlinesvg';
|
|
|
|
import axios from 'axios';
|
2018-02-23 16:31:41 +00:00
|
|
|
import { Helmet } from 'react-helmet';
|
2017-06-26 16:48:28 +00:00
|
|
|
import { updateSettings, initiateVerification } from '../actions';
|
2017-03-16 22:25:12 +00:00
|
|
|
import AccountForm from '../components/AccountForm';
|
|
|
|
import { validateSettings } from '../../../utils/reduxFormUtils';
|
2017-04-13 16:04:10 +00:00
|
|
|
import GithubButton from '../components/GithubButton';
|
2017-03-16 22:25:12 +00:00
|
|
|
|
|
|
|
const exitUrl = require('../../../images/exit.svg');
|
|
|
|
const logoUrl = require('../../../images/p5js-logo.svg');
|
|
|
|
|
|
|
|
|
|
|
|
class AccountView extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.closeAccountPage = this.closeAccountPage.bind(this);
|
|
|
|
this.gotoHomePage = this.gotoHomePage.bind(this);
|
|
|
|
}
|
|
|
|
|
2018-10-13 20:14:46 +00:00
|
|
|
componentDidMount() {
|
|
|
|
document.body.className = this.props.theme;
|
|
|
|
}
|
|
|
|
|
2017-03-16 22:25:12 +00:00
|
|
|
closeAccountPage() {
|
2018-10-13 20:14:46 +00:00
|
|
|
browserHistory.goBack();
|
2017-03-16 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gotoHomePage() {
|
|
|
|
browserHistory.push('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="form-container">
|
2018-02-23 16:31:41 +00:00
|
|
|
<Helmet>
|
|
|
|
<title>p5.js Web Editor | Account</title>
|
|
|
|
</Helmet>
|
2017-03-16 22:25:12 +00:00
|
|
|
<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.closeAccountPage}>
|
|
|
|
<InlineSVG src={exitUrl} alt="Close Account Page" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div className="form-container__content">
|
|
|
|
<h2 className="form-container__title">My Account</h2>
|
|
|
|
<AccountForm {...this.props} />
|
2018-10-13 20:14:46 +00:00
|
|
|
<Link to="/account/advanced">Advanced Settings</Link>
|
2017-04-13 16:04:10 +00:00
|
|
|
<h2 className="form-container__divider">Or</h2>
|
|
|
|
<GithubButton buttonText="Login with Github" />
|
2017-03-16 22:25:12 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
initialValues: state.user, // <- initialValues for reduxForm
|
|
|
|
user: state.user,
|
2018-10-13 20:14:46 +00:00
|
|
|
theme: state.preferences.theme
|
2017-03-16 22:25:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
2017-06-26 16:48:28 +00:00
|
|
|
return bindActionCreators({ updateSettings, initiateVerification }, dispatch);
|
2017-03-16 22:25:12 +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 })
|
|
|
|
.then((response) => {
|
|
|
|
if (response.data.exists) {
|
|
|
|
const error = {};
|
|
|
|
error[fieldToValidate] = response.data.message;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.resolve(true).then(() => {});
|
|
|
|
}
|
|
|
|
|
|
|
|
AccountView.propTypes = {
|
2018-10-13 20:14:46 +00:00
|
|
|
theme: PropTypes.string.isRequired
|
2017-03-16 22:25:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default reduxForm({
|
|
|
|
form: 'updateAllSettings',
|
|
|
|
fields: ['username', 'email', 'currentPassword', 'newPassword'],
|
|
|
|
validate: validateSettings,
|
|
|
|
asyncValidate,
|
|
|
|
asyncBlurFields: ['username', 'email', 'currentPassword']
|
|
|
|
}, mapStateToProps, mapDispatchToProps)(AccountView);
|