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

87 lines
1.9 KiB
JavaScript
Raw Normal View History

// @flow
import PropTypes from 'prop-types';
import React, { Component } from 'react';
2016-05-02 18:43:18 +05:30
import { changeUsername } from 'services/api/accounts';
2016-05-02 18:43:18 +05:30
import { FormModel } from 'components/ui/form';
import ChangeUsername from 'components/profile/changeUsername/ChangeUsername';
type OwnProps = {||};
type Props = {
username: string,
updateUsername: (username: string) => void,
};
2016-05-02 18:43:18 +05:30
class ChangeUsernamePage extends Component<Props> {
static contextTypes = {
userId: PropTypes.number.isRequired,
onSubmit: PropTypes.func.isRequired,
goToProfile: PropTypes.func.isRequired,
};
2016-05-02 18:43:18 +05:30
form = new FormModel();
2016-05-02 18:43:18 +05:30
actualUsername: string;
componentWillMount() {
this.actualUsername = this.props.username;
}
2016-05-02 18:43:18 +05:30
componentWillUnmount() {
this.props.updateUsername(this.actualUsername);
}
render() {
return (
<ChangeUsername
form={this.form}
onSubmit={this.onSubmit}
onChange={this.onUsernameChange}
username={this.props.username}
/>
);
}
onUsernameChange = (username: string) => {
this.props.updateUsername(username);
};
2016-05-02 18:43:18 +05:30
onSubmit = () => {
const { form } = this;
if (this.actualUsername === this.props.username) {
this.context.goToProfile();
return Promise.resolve();
2016-05-02 18:43:18 +05:30
}
return this.context
.onSubmit({
form,
sendData: () => {
const { username, password } = form.serialize();
return changeUsername(this.context.userId, username, password);
},
})
.then(() => {
this.actualUsername = form.value('username');
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<Props, OwnProps, _, _, _, _>(
state => ({
username: state.user.username,
}),
{
updateUsername: username => updateUser({ username }),
},
)(ChangeUsernamePage);