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

122 lines
4.0 KiB
React
Raw Normal View History

import React, { Component, PropTypes } from 'react';
import logger from 'services/logger';
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 { browserHistory } from 'react-router';
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 browserHistory.push('/');
},
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({
Popup(props) {
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);