accounts-frontend/src/pages/profile/ProfilePage.js

139 lines
5.1 KiB
JavaScript
Raw Normal View History

// @flow
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Route, Switch, Redirect } from 'react-router-dom';
import logger from 'services/logger';
import { browserHistory } from 'services/history';
import Profile from 'components/profile/Profile';
import ChangePasswordPage from 'pages/profile/ChangePasswordPage';
import ChangeUsernamePage from 'pages/profile/ChangeUsernamePage';
import ChangeEmailPage from 'pages/profile/ChangeEmailPage';
2017-07-22 21:27:38 +05:30
import MultiFactorAuthPage from 'pages/profile/MultiFactorAuthPage';
2016-05-22 22:55:38 +05:30
import { FooterMenu } from 'components/footerMenu';
import styles from './profile.scss';
import type { FormModel } from 'components/ui/form';
class ProfilePage extends Component {
props: {
onSubmit: ({form: FormModel, sendData: () => Promise<*>}) => void,
fetchUserData: () => Promise<*>
};
static childContextTypes = {
onSubmit: PropTypes.func,
goToProfile: PropTypes.func
};
getChildContext() {
return {
onSubmit: this.props.onSubmit,
goToProfile: () => this.props.fetchUserData().then(this.goToProfile)
};
}
render() {
return (
<div className={styles.container}>
<Switch>
<Route path="/profile/mfa/step:step([1-3])" component={MultiFactorAuthPage} />
<Route path="/profile/mfa" exact component={MultiFactorAuthPage} />
<Route path="/profile/change-password" exact component={ChangePasswordPage} />
<Route path="/profile/change-username" exact component={ChangeUsernamePage} />
<Route path="/profile/change-email/:step?/:code?" component={ChangeEmailPage} />
<Route path="/profile" exact component={Profile} />
<Route path="/" exact component={Profile} />
<Redirect to="/404" />
</Switch>
2016-05-22 22:55:38 +05:30
<div className={styles.footer}>
<FooterMenu />
</div>
</div>
);
}
goToProfile = () => browserHistory.push('/');
}
import { connect } from 'react-redux';
import { fetchUserData } from 'components/user/actions';
import { create as createPopup } from 'components/ui/popup/actions';
import PasswordRequestForm from 'components/profile/passwordRequestForm/PasswordRequestForm';
export default connect(null, {
fetchUserData,
2016-05-28 01:34:17 +05:30
onSubmit: ({form, sendData}) => (dispatch) => {
form.beginLoading();
return sendData()
.catch((resp) => {
const requirePassword = resp.errors && !!resp.errors.password;
// prevalidate user input, because requestPassword popup will block the
// entire form from input, so it must be valid
if (resp.errors) {
Reflect.deleteProperty(resp.errors, 'password');
if (resp.errors.email && resp.data && resp.data.canRepeatIn) {
resp.errors.email = {
type: resp.errors.email,
payload: {
msLeft: resp.data.canRepeatIn * 1000
}
};
}
if (Object.keys(resp.errors).length) {
form.setErrors(resp.errors);
return Promise.reject(resp);
}
return Promise.resolve({requirePassword});
}
})
2016-05-28 03:06:22 +05:30
.then((resp) => !resp.requirePassword || requestPassword(form))
.catch((resp) => {
if (!resp || !resp.errors) {
logger.warn('Unexpected profile editing error', {
resp
});
2017-02-27 11:17:31 +05:30
} else {
return Promise.reject(resp);
}
})
2016-05-28 01:34:17 +05:30
.finally(() => form.endLoading());
2016-05-28 03:06:22 +05:30
function requestPassword(form) {
return new Promise((resolve) => {
2016-07-26 10:10:45 +05:30
dispatch(createPopup({
2017-06-13 01:02:59 +05:30
Popup(props: {
onClose: Function
}) {
2016-07-26 10:10:45 +05:30
const onSubmit = () => {
form.beginLoading();
sendData()
.then(resolve)
.then(props.onClose)
2016-07-26 10:10:45 +05:30
.catch((resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
} else {
return Promise.reject(resp);
2016-07-26 10:10:45 +05:30
}
})
.finally(() => form.endLoading());
};
return <PasswordRequestForm form={form} onSubmit={onSubmit} />;
},
disableOverlayClose: true
}));
2016-05-28 03:06:22 +05:30
});
}
2016-05-28 01:34:17 +05:30
}
})(ProfilePage);