p5.js-web-editor/client/modules/User/pages/AccountView.jsx

121 lines
4 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import React from 'react';
2019-07-22 21:52:19 +00:00
import { reduxForm } from 'redux-form';
import { bindActionCreators } from 'redux';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
2019-07-22 21:52:19 +00:00
import axios from 'axios';
import { Helmet } from 'react-helmet';
2019-05-15 14:25:58 +00:00
import { updateSettings, initiateVerification, createApiKey, removeApiKey } from '../actions';
import AccountForm from '../components/AccountForm';
import { validateSettings } from '../../../utils/reduxFormUtils';
import GithubButton from '../components/GithubButton';
import APIKeyForm from '../components/APIKeyForm';
import Nav from '../../../components/Nav';
const __process = (typeof global !== 'undefined' ? global : window).process;
const ROOT_URL = __process.env.API_URL;
class AccountView extends React.Component {
2018-10-13 20:14:46 +00:00
componentDidMount() {
2019-05-24 09:59:56 +00:00
document.body.className = this.props.theme;
2018-10-13 20:14:46 +00:00
}
render() {
const accessTokensUIEnabled = window.process.env.UI_ACCESS_TOKEN_ENABLED;
return (
<div className="account-settings__container">
<Helmet>
<title>p5.js Web Editor | Account Settings</title>
</Helmet>
2019-05-24 09:59:56 +00:00
<Nav layout="dashboard" />
2019-05-24 09:59:56 +00:00
<section className="account-settings">
<header className="account-settings__header">
<h1 className="account-settings__title">Account Settings</h1>
</header>
{accessTokensUIEnabled &&
2019-05-24 09:59:56 +00:00
<Tabs className="account__tabs">
<TabList>
<div className="tabs__titles">
<Tab><h4 className="tabs__title">Account</h4></Tab>
<Tab><h4 className="tabs__title">Access Tokens</h4></Tab>
2019-05-24 09:59:56 +00:00
</div>
</TabList>
<TabPanel>
<AccountForm {...this.props} />
<h2 className="form-container__divider">Social Login</h2>
2019-05-24 09:59:56 +00:00
<p className="account__social-text">
Link this account with your GitHub account to allow login from both.
</p>
<GithubButton buttonText="Login with GitHub" />
2019-05-24 09:59:56 +00:00
</TabPanel>
<TabPanel>
<APIKeyForm {...this.props} />
</TabPanel>
</Tabs>
}
{!accessTokensUIEnabled &&
<div>
<AccountForm {...this.props} />
<h2 className="form-container__divider">Social Login</h2>
<p className="account__social-text">
Link this account with your GitHub account to allow login from both.
</p>
<GithubButton buttonText="Login with GitHub" />
</div>
}
2019-05-24 09:59:56 +00:00
</section>
</div>
);
}
}
function mapStateToProps(state) {
return {
initialValues: state.user, // <- initialValues for reduxForm
2019-05-15 14:40:09 +00:00
previousPath: state.ide.previousPath,
user: state.user,
apiKeys: state.user.apiKeys,
2018-10-13 20:14:46 +00:00
theme: state.preferences.theme
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({
updateSettings, initiateVerification, createApiKey, removeApiKey
}, dispatch);
}
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(`${ROOT_URL}/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 = {
2019-05-15 14:40:09 +00:00
previousPath: PropTypes.string.isRequired,
2018-10-13 20:14:46 +00:00
theme: PropTypes.string.isRequired
};
2019-07-22 21:52:19 +00:00
export default reduxForm({
form: 'updateAllSettings',
fields: ['username', 'email', 'currentPassword', 'newPassword'],
validate: validateSettings,
asyncValidate,
asyncBlurFields: ['username', 'email', 'currentPassword']
}, mapStateToProps, mapDispatchToProps)(AccountView);