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

124 lines
4.1 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import React from 'react';
2019-07-22 23:52:19 +02:00
import { reduxForm } from 'redux-form';
import { bindActionCreators } from 'redux';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import { Helmet } from 'react-helmet';
import { withTranslation } from 'react-i18next';
2019-05-15 16:25:58 +02:00
import { updateSettings, initiateVerification, createApiKey, removeApiKey } from '../actions';
import AccountForm from '../components/AccountForm';
import apiClient from '../../../utils/apiClient';
import { validateSettings } from '../../../utils/reduxFormUtils';
import SocialAuthButton from '../components/SocialAuthButton';
import APIKeyForm from '../components/APIKeyForm';
import Nav from '../../../components/Nav';
function SocialLoginPanel(props) {
return (
<React.Fragment>
<AccountForm {...props} />
{/* eslint-disable-next-line react/prop-types */}
<h2 className="form-container__divider">{props.t('AccountView.SocialLogin')}</h2>
<p className="account__social-text">
{/* eslint-disable-next-line react/prop-types */}
{props.t('AccountView.SocialLoginDescription')}
</p>
<div className="account__social-stack">
<SocialAuthButton service={SocialAuthButton.services.github} />
<SocialAuthButton service={SocialAuthButton.services.google} />
</div>
</React.Fragment>
);
}
class AccountView extends React.Component {
2018-10-13 22:14:46 +02:00
componentDidMount() {
2019-05-24 11:59:56 +02:00
document.body.className = this.props.theme;
2018-10-13 22:14:46 +02:00
}
render() {
const accessTokensUIEnabled = window.process.env.UI_ACCESS_TOKEN_ENABLED;
return (
<div className="account-settings__container">
<Helmet>
<title>{this.props.t('AccountView.Title')}</title>
</Helmet>
2019-05-24 11:59:56 +02:00
<Nav layout="dashboard" />
2019-05-24 11:59:56 +02:00
<main className="account-settings">
<header className="account-settings__header">
<h1 className="account-settings__title">{this.props.t('AccountView.Settings')}</h1>
</header>
{accessTokensUIEnabled &&
2019-05-24 11:59:56 +02:00
<Tabs className="account__tabs">
<TabList>
<div className="tabs__titles">
<Tab><h4 className="tabs__title">{this.props.t('AccountView.AccountTab')}</h4></Tab>
{accessTokensUIEnabled && <Tab><h4 className="tabs__title">{this.props.t('AccountView.AccessTokensTab')}</h4></Tab>}
2019-05-24 11:59:56 +02:00
</div>
</TabList>
<TabPanel>
{/* <SocialLoginPanel {...this.props} /> */}
2019-05-24 11:59:56 +02:00
</TabPanel>
<TabPanel>
<APIKeyForm {...this.props} />
</TabPanel>
</Tabs>
}
{ !accessTokensUIEnabled && <SocialLoginPanel {...this.props} /> }
</main>
</div>
);
}
}
function mapStateToProps(state) {
return {
initialValues: state.user, // <- initialValues for reduxForm
2019-05-15 16:40:09 +02:00
previousPath: state.ide.previousPath,
user: state.user,
apiKeys: state.user.apiKeys,
2018-10-13 22:14:46 +02: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 apiClient.get('/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 16:40:09 +02:00
previousPath: PropTypes.string.isRequired,
theme: PropTypes.string.isRequired,
t: PropTypes.func.isRequired
};
export default withTranslation()(reduxForm({
2019-07-22 23:52:19 +02:00
form: 'updateAllSettings',
fields: ['username', 'email', 'currentPassword', 'newPassword'],
validate: validateSettings,
asyncValidate,
asyncBlurFields: ['username', 'email', 'currentPassword']
}, mapStateToProps, mapDispatchToProps)(AccountView));