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';
|
|
|
|
|
|
|
|
class ChangeUsernamePage extends Component {
|
|
|
|
static displayName = 'ChangeUsernamePage';
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
username: PropTypes.string.isRequired,
|
2016-05-12 10:00:10 +05:30
|
|
|
updateUsername: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
static contextTypes = {
|
2016-05-14 13:24:26 +05:30
|
|
|
onSubmit: PropTypes.func.isRequired,
|
|
|
|
goToProfile: PropTypes.func.isRequired
|
2016-05-02 18:43:18 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
form = new FormModel();
|
|
|
|
|
|
|
|
componentWillMount() {
|
2016-05-22 23:32:46 +05:30
|
|
|
this.actualUsername = this.props.username;
|
2016-05-02 18:43:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2016-05-22 23:32:46 +05:30
|
|
|
this.props.updateUsername(this.actualUsername);
|
2016-05-02 18:43:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ChangeUsername form={this.form}
|
|
|
|
onSubmit={this.onSubmit}
|
|
|
|
onChange={this.onUsernameChange}
|
|
|
|
username={this.props.username}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onUsernameChange = (username) => {
|
|
|
|
this.props.updateUsername(username);
|
|
|
|
};
|
|
|
|
|
|
|
|
onSubmit = () => {
|
2016-05-12 10:00:10 +05:30
|
|
|
const {form} = this;
|
2016-05-22 23:32:46 +05:30
|
|
|
if (this.actualUsername === this.props.username) {
|
2016-05-14 13:24:26 +05:30
|
|
|
this.context.goToProfile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:00:10 +05:30
|
|
|
this.context.onSubmit({
|
|
|
|
form,
|
|
|
|
sendData: () => accounts.changeUsername(form.serialize())
|
|
|
|
}).then(() => {
|
2016-05-22 23:32:46 +05:30
|
|
|
this.actualUsername = form.value('username');
|
2017-01-02 19:07:02 +05:30
|
|
|
|
2016-05-22 13:23:40 +05:30
|
|
|
this.context.goToProfile();
|
2016-05-02 18:43:18 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { updateUser } from 'components/user/actions';
|
|
|
|
|
|
|
|
export default connect((state) => ({
|
|
|
|
username: state.user.username
|
|
|
|
}), {
|
2017-01-02 19:07:02 +05:30
|
|
|
updateUsername: (username) => updateUser({username})
|
2016-05-02 18:43:18 +05:30
|
|
|
})(ChangeUsernamePage);
|