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

93 lines
2.9 KiB
React
Raw Normal View History

import React, { Component, PropTypes } from 'react';
2016-05-22 22:55:38 +05:30
import { FooterMenu } from 'components/footerMenu';
import styles from './profile.scss';
class ProfilePage extends Component {
displayName = 'ProfilePage';
static propTypes = {
2016-05-14 16:56:17 +05:30
onSubmit: PropTypes.func.isRequired,
fetchUserData: PropTypes.func.isRequired,
goToProfile: PropTypes.func.isRequired,
children: PropTypes.element
};
static childContextTypes = {
onSubmit: PropTypes.func,
goToProfile: PropTypes.func
};
getChildContext() {
return {
onSubmit: this.props.onSubmit,
goToProfile: () => this.props.fetchUserData().then(this.props.goToProfile)
};
}
render() {
return (
<div className={styles.container}>
{this.props.children}
2016-05-22 22:55:38 +05:30
<div className={styles.footer}>
<FooterMenu />
</div>
</div>
);
}
}
import { connect } from 'react-redux';
import { routeActions } from 'react-router-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, {
goToProfile() {
return routeActions.push('/');
},
fetchUserData,
onSubmit: ({form, sendData}) => (dispatch) =>
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 (Object.keys(resp.errors).length) {
form.setErrors(resp.errors);
return Promise.reject(resp);
}
return Promise.resolve({requirePassword});
}
})
.then((resp) => new Promise((resolve) => {
if (resp.requirePassword) {
dispatch(createPopup(PasswordRequestForm, (props) => ({
form,
onSubmit: () => {
sendData()
.catch((resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
}
return Promise.reject(resp);
})
.then(resolve)
.then(props.onClose);
}
})));
} else {
resolve();
}
}))
})(ProfilePage);