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

76 lines
1.9 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';
class ChangeUsernamePage extends Component {
static displayName = 'ChangeUsernamePage';
static propTypes = {
username: PropTypes.string.isRequired,
updateUsername: PropTypes.func.isRequired
};
static contextTypes = {
onSubmit: PropTypes.func.isRequired,
goToProfile: PropTypes.func.isRequired
2016-05-02 18:43:18 +05:30
};
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 = () => {
const {form} = this;
if (this.state.actualUsername === this.props.username) {
this.context.goToProfile();
return;
}
this.context.onSubmit({
form,
sendData: () => accounts.changeUsername(form.serialize())
}).then(() => {
this.props.updateUsername(form.value('username'));
2016-05-02 18:43:18 +05:30
this.setState({
actualUsername: this.props.username
});
});
};
}
import { connect } from 'react-redux';
import { updateUser } from 'components/user/actions';
export default connect((state) => ({
username: state.user.username
}), {
updateUsername: (username) => {
return updateUser({username});
}
})(ChangeUsernamePage);