2016-05-12 10:00:10 +05:30
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-04-17 15:05:04 +05:30
|
|
|
|
2017-01-02 19:09:34 +05:30
|
|
|
import logger from 'services/logger';
|
|
|
|
|
2016-05-22 22:55:38 +05:30
|
|
|
import { FooterMenu } from 'components/footerMenu';
|
|
|
|
|
2016-04-17 15:05:04 +05:30
|
|
|
import styles from './profile.scss';
|
|
|
|
|
2016-05-12 10:00:10 +05:30
|
|
|
class ProfilePage extends Component {
|
2016-04-17 15:05:04 +05:30
|
|
|
displayName = 'ProfilePage';
|
|
|
|
|
2016-05-12 10:00:10 +05:30
|
|
|
static propTypes = {
|
2016-05-14 16:56:17 +05:30
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2016-05-22 23:32:46 +05:30
|
|
|
fetchUserData: PropTypes.func.isRequired,
|
2016-05-22 13:23:40 +05:30
|
|
|
goToProfile: PropTypes.func.isRequired,
|
|
|
|
children: PropTypes.element
|
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,
|
2016-05-22 23:32:46 +05:30
|
|
|
goToProfile: () => this.props.fetchUserData().then(this.props.goToProfile)
|
2016-05-12 10:00:10 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-17 15:05:04 +05:30
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
|
|
|
{this.props.children}
|
2016-05-22 22:55:38 +05:30
|
|
|
|
|
|
|
<div className={styles.footer}>
|
|
|
|
<FooterMenu />
|
|
|
|
</div>
|
2016-04-17 15:05:04 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 10:00:10 +05:30
|
|
|
|
|
|
|
import { connect } from 'react-redux';
|
2017-05-09 01:04:50 +05:30
|
|
|
import { browserHistory } from 'react-router';
|
2016-05-22 23:32:46 +05:30
|
|
|
import { fetchUserData } from 'components/user/actions';
|
2016-05-12 10:00:10 +05:30
|
|
|
import { create as createPopup } from 'components/ui/popup/actions';
|
|
|
|
import PasswordRequestForm from 'components/profile/passwordRequestForm/PasswordRequestForm';
|
|
|
|
|
|
|
|
export default connect(null, {
|
2016-05-22 13:23:40 +05:30
|
|
|
goToProfile() {
|
2017-05-09 01:04:50 +05:30
|
|
|
return browserHistory.push('/');
|
2016-05-22 13:23:40 +05:30
|
|
|
},
|
2016-05-22 23:32:46 +05:30
|
|
|
fetchUserData,
|
2016-05-28 01:34:17 +05:30
|
|
|
onSubmit: ({form, sendData}) => (dispatch) => {
|
|
|
|
form.beginLoading();
|
|
|
|
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) {
|
|
|
|
Reflect.deleteProperty(resp.errors, 'password');
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-22 13:23:40 +05:30
|
|
|
return Promise.resolve({requirePassword});
|
|
|
|
}
|
2016-05-12 10:00:10 +05:30
|
|
|
})
|
2016-05-28 03:06:22 +05:30
|
|
|
.then((resp) => !resp.requirePassword || requestPassword(form))
|
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) {
|
|
|
|
return new Promise((resolve) => {
|
2016-07-26 10:10:45 +05:30
|
|
|
dispatch(createPopup({
|
|
|
|
Popup(props) {
|
|
|
|
const onSubmit = () => {
|
|
|
|
form.beginLoading();
|
|
|
|
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-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);
|