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

71 lines
1.8 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
import React, { Component } from 'react';
2016-05-02 18:43:18 +05:30
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.actualUsername = this.props.username;
2016-05-02 18:43:18 +05:30
}
componentWillUnmount() {
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 = () => {
const {form} = this;
if (this.actualUsername === this.props.username) {
this.context.goToProfile();
2017-02-27 11:17:31 +05:30
return Promise.resolve();
}
2017-02-27 11:17:31 +05:30
return this.context.onSubmit({
form,
sendData: () => accounts.changeUsername(form.serialize())
}).then(() => {
this.actualUsername = form.value('username');
2017-01-02 19:07:02 +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);