2017-08-09 23:31:32 +05:30
|
|
|
// @flow
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-05-26 00:41:57 +05:30
|
|
|
import { Route, Switch, Redirect } from 'react-router-dom';
|
2017-09-09 19:52:19 +05:30
|
|
|
import { connect } from 'react-redux';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2017-09-09 19:52:19 +05:30
|
|
|
import { fetchUserData } from 'components/user/actions';
|
|
|
|
import { create as createPopup } from 'components/ui/popup/actions';
|
|
|
|
import PasswordRequestForm from 'components/profile/passwordRequestForm/PasswordRequestForm';
|
2017-01-02 19:09:34 +05:30
|
|
|
import logger from 'services/logger';
|
2017-08-20 21:41:01 +05:30
|
|
|
import { browserHistory } from 'services/history';
|
2017-08-20 21:15:21 +05:30
|
|
|
import { FooterMenu } from 'components/footerMenu';
|
2017-05-26 00:41:57 +05:30
|
|
|
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';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2016-04-17 15:05:04 +05:30
|
|
|
import styles from './profile.scss';
|
|
|
|
|
2017-08-09 23:31:32 +05:30
|
|
|
import type { FormModel } from 'components/ui/form';
|
2016-04-17 15:05:04 +05:30
|
|
|
|
2017-08-23 02:01:41 +05:30
|
|
|
class ProfilePage extends Component<{
|
|
|
|
onSubmit: ({form: FormModel, sendData: () => Promise<*>}) => void,
|
|
|
|
fetchUserData: () => Promise<*>
|
|
|
|
}> {
|
2016-05-12 10:00:10 +05:30
|
|
|
static childContextTypes = {
|
2016-05-14 13:24:26 +05:30
|
|
|
onSubmit: PropTypes.func,
|
|
|
|
goToProfile: PropTypes.func
|
2016-05-12 10:00:10 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
2016-05-14 13:24:26 +05:30
|
|
|
onSubmit: this.props.onSubmit,
|
2017-08-09 23:31:32 +05:30
|
|
|
goToProfile: () => this.props.fetchUserData().then(this.goToProfile)
|
2016-05-12 10:00:10 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-17 15:05:04 +05:30
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
2017-05-26 00:41:57 +05:30
|
|
|
<Switch>
|
2017-08-04 10:21:41 +05:30
|
|
|
<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} />
|
2017-05-26 00:41:57 +05:30
|
|
|
<Route path="/profile/change-email/:step?/:code?" component={ChangeEmailPage} />
|
2017-08-04 10:21:41 +05:30
|
|
|
<Route path="/profile" exact component={Profile} />
|
2017-05-26 00:41:57 +05:30
|
|
|
<Route path="/" exact component={Profile} />
|
|
|
|
<Redirect to="/404" />
|
|
|
|
</Switch>
|
2016-05-22 22:55:38 +05:30
|
|
|
|
|
|
|
<div className={styles.footer}>
|
|
|
|
<FooterMenu />
|
|
|
|
</div>
|
2016-04-17 15:05:04 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-08-09 23:31:32 +05:30
|
|
|
|
|
|
|
goToProfile = () => browserHistory.push('/');
|
2016-04-17 15:05:04 +05:30
|
|
|
}
|
2016-05-12 10:00:10 +05:30
|
|
|
|
|
|
|
export default connect(null, {
|
2016-05-22 23:32:46 +05:30
|
|
|
fetchUserData,
|
2017-08-20 21:15:21 +05:30
|
|
|
onSubmit: ({form, sendData}: {
|
|
|
|
form: FormModel,
|
|
|
|
sendData: () => Promise<*>
|
|
|
|
}) => (dispatch) => {
|
2016-05-28 01:34:17 +05:30
|
|
|
form.beginLoading();
|
2017-09-09 19:52:19 +05:30
|
|
|
|
2016-05-28 01:34:17 +05:30
|
|
|
return sendData()
|
2016-05-12 10:00:10 +05:30
|
|
|
.catch((resp) => {
|
2016-05-22 13:23:40 +05:30
|
|
|
const requirePassword = resp.errors && !!resp.errors.password;
|
|
|
|
|
2016-05-12 10:00:10 +05:30
|
|
|
// prevalidate user input, because requestPassword popup will block the
|
|
|
|
// entire form from input, so it must be valid
|
|
|
|
if (resp.errors) {
|
2017-08-20 21:15:21 +05:30
|
|
|
delete resp.errors.password;
|
2016-05-12 10:00:10 +05:30
|
|
|
|
2016-07-28 09:55:02 +05:30
|
|
|
if (resp.errors.email && resp.data && resp.data.canRepeatIn) {
|
|
|
|
resp.errors.email = {
|
|
|
|
type: resp.errors.email,
|
|
|
|
payload: {
|
|
|
|
msLeft: resp.data.canRepeatIn * 1000
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:00:10 +05:30
|
|
|
if (Object.keys(resp.errors).length) {
|
|
|
|
form.setErrors(resp.errors);
|
|
|
|
return Promise.reject(resp);
|
|
|
|
}
|
|
|
|
|
2017-08-20 21:15:21 +05:30
|
|
|
if (requirePassword) {
|
|
|
|
return requestPassword(form);
|
|
|
|
}
|
2016-05-22 13:23:40 +05:30
|
|
|
}
|
2017-08-20 21:15:21 +05:30
|
|
|
|
|
|
|
return Promise.reject(resp);
|
2016-05-12 10:00:10 +05:30
|
|
|
})
|
2017-01-02 19:09:34 +05:30
|
|
|
.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);
|
2017-01-02 19:09:34 +05:30
|
|
|
}
|
|
|
|
})
|
2016-05-28 01:34:17 +05:30
|
|
|
.finally(() => form.endLoading());
|
2016-05-28 03:06:22 +05:30
|
|
|
|
|
|
|
function requestPassword(form) {
|
2017-10-28 01:15:16 +05:30
|
|
|
return new Promise((resolve, reject) => {
|
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();
|
2017-10-28 01:15:16 +05:30
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
sendData()
|
2017-01-02 19:09:34 +05:30
|
|
|
.then(resolve)
|
|
|
|
.then(props.onClose)
|
2016-07-26 10:10:45 +05:30
|
|
|
.catch((resp) => {
|
|
|
|
if (resp.errors) {
|
|
|
|
form.setErrors(resp.errors);
|
2017-10-28 01:15:16 +05:30
|
|
|
|
2017-11-20 02:03:03 +05:30
|
|
|
const parentFormHasErrors = Object.keys(resp.errors)
|
|
|
|
.filter((name) => name !== 'password')
|
|
|
|
.length > 0;
|
|
|
|
|
|
|
|
if (parentFormHasErrors) {
|
2017-10-28 01:15:16 +05:30
|
|
|
// something wrong with parent form, hidding popup and show that form
|
|
|
|
props.onClose();
|
|
|
|
reject(resp);
|
|
|
|
logger.warn('Profile: can not submit pasword popup due to errors in source form', { resp });
|
|
|
|
}
|
2017-01-02 19:09:34 +05:30
|
|
|
} 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
|
|
|
}
|
2016-05-12 10:00:10 +05:30
|
|
|
})(ProfilePage);
|