2016-05-01 23:20:55 +05:30
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
|
|
|
|
import accounts from 'services/api/accounts';
|
2016-05-02 15:08:45 +05:30
|
|
|
import { FormModel } from 'components/ui/form';
|
2016-05-01 23:20:55 +05:30
|
|
|
import ChangePassword from 'components/profile/changePassword/ChangePassword';
|
|
|
|
import PasswordRequestForm from 'components/profile/passwordRequestForm/PasswordRequestForm';
|
|
|
|
|
|
|
|
class ChangePasswordPage extends Component {
|
|
|
|
static displayName = 'ChangePasswordPage';
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
changePassword: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
2016-05-02 14:50:50 +05:30
|
|
|
form = new FormModel();
|
|
|
|
|
2016-05-01 23:20:55 +05:30
|
|
|
render() {
|
|
|
|
return (
|
2016-05-02 14:50:50 +05:30
|
|
|
<ChangePassword onSubmit={this.onSubmit} form={this.form} />
|
2016-05-01 23:20:55 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:50:50 +05:30
|
|
|
onSubmit = () => {
|
|
|
|
this.props.changePassword(this.form);
|
2016-05-01 23:20:55 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { routeActions } from 'react-router-redux';
|
2016-05-11 10:56:44 +05:30
|
|
|
import { create as createPopup } from 'components/ui/popup/actions';
|
2016-05-01 23:40:45 +05:30
|
|
|
import { updateUser } from 'components/user/actions';
|
2016-05-01 23:20:55 +05:30
|
|
|
|
|
|
|
function goToProfile() {
|
|
|
|
return routeActions.push('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(null, {
|
2016-05-02 14:50:50 +05:30
|
|
|
changePassword: (form) => {
|
2016-05-01 23:20:55 +05:30
|
|
|
return (dispatch) => {
|
2016-05-02 15:41:10 +05:30
|
|
|
accounts.changePassword(form.serialize())
|
|
|
|
.catch((resp) => {
|
|
|
|
// 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();
|
|
|
|
})
|
|
|
|
.then(() => {
|
2016-05-11 10:56:44 +05:30
|
|
|
dispatch(createPopup(PasswordRequestForm, (props) => ({
|
2016-05-02 14:50:50 +05:30
|
|
|
form,
|
|
|
|
onSubmit: () => {
|
2016-05-01 23:40:45 +05:30
|
|
|
// TODO: hide this logic in action
|
2016-05-02 14:50:50 +05:30
|
|
|
accounts.changePassword(form.serialize())
|
|
|
|
.catch((resp) => {
|
|
|
|
if (resp.errors) {
|
|
|
|
form.setErrors(resp.errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
2016-05-01 23:20:55 +05:30
|
|
|
})
|
2016-05-01 23:40:45 +05:30
|
|
|
.then(() => {
|
|
|
|
dispatch(updateUser({
|
|
|
|
passwordChangedAt: Date.now() / 1000,
|
|
|
|
shouldChangePassword: false
|
|
|
|
}));
|
|
|
|
})
|
2016-05-01 23:20:55 +05:30
|
|
|
.then(props.onClose)
|
|
|
|
.then(() => dispatch(goToProfile()));
|
|
|
|
}
|
2016-05-02 15:41:10 +05:30
|
|
|
})));
|
|
|
|
});
|
2016-05-01 23:20:55 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
})(ChangePasswordPage);
|