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

112 lines
3.6 KiB
React
Raw Normal View History

2016-05-02 18:43:18 +05:30
import React, { Component, PropTypes } from 'react';
import accounts from 'services/api/accounts';
import { FormModel } from 'components/ui/form';
import ChangeUsername from 'components/profile/changeUsername/ChangeUsername';
import PasswordRequestForm from 'components/profile/passwordRequestForm/PasswordRequestForm';
class ChangeUsernamePage extends Component {
static displayName = 'ChangeUsernamePage';
static propTypes = {
username: PropTypes.string.isRequired,
updateUsername: PropTypes.func.isRequired, // updates username in state
changeUsername: PropTypes.func.isRequired // saves username to backend
};
form = new FormModel();
componentWillMount() {
this.setState({
actualUsername: this.props.username
});
}
componentWillUnmount() {
this.props.updateUsername(this.state.actualUsername);
}
render() {
return (
<ChangeUsername form={this.form}
onSubmit={this.onSubmit}
onChange={this.onUsernameChange}
username={this.props.username}
/>
);
}
onUsernameChange = (username) => {
this.props.updateUsername(username);
};
onSubmit = () => {
this.props.changeUsername(this.form).then(() => {
this.setState({
actualUsername: this.props.username
});
});
};
}
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-02 18:43:18 +05:30
import { updateUser } from 'components/user/actions';
function goToProfile() {
return routeActions.push('/');
}
export default connect((state) => ({
username: state.user.username
}), {
updateUsername: (username) => {
return updateUser({username});
},
changeUsername: (form) => {
return (dispatch) => accounts.changeUsername(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(() => {
return new Promise((resolve) => {
2016-05-11 10:56:44 +05:30
dispatch(createPopup(PasswordRequestForm, (props) => ({
2016-05-02 18:43:18 +05:30
form,
onSubmit: () => {
// TODO: hide this logic in action
accounts.changeUsername(form.serialize())
.catch((resp) => {
if (resp.errors) {
form.setErrors(resp.errors);
}
return Promise.reject(resp);
})
.then(() => {
dispatch(updateUser({
username: form.value('username')
}));
})
.then(resolve)
.then(props.onClose)
.then(() => dispatch(goToProfile()));
}
})));
});
})
;
}
})(ChangeUsernamePage);