2019-01-28 00:42:58 +05:30
|
|
|
// @flow
|
2017-08-23 00:19:50 +05:30
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2019-01-28 00:42:58 +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';
|
|
|
|
|
2019-01-28 00:42:58 +05:30
|
|
|
interface Props {
|
|
|
|
username: string;
|
|
|
|
updateUsername: (username: string) => void;
|
|
|
|
}
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2019-01-28 00:42:58 +05:30
|
|
|
class ChangeUsernamePage extends Component<Props> {
|
|
|
|
static displayName = 'ChangeUsernamePage';
|
2016-05-12 10:00:10 +05:30
|
|
|
|
|
|
|
static contextTypes = {
|
2019-01-28 00:42:58 +05:30
|
|
|
userId: PropTypes.number.isRequired,
|
2016-05-14 13:24:26 +05:30
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2019-01-28 00:42:58 +05:30
|
|
|
goToProfile: PropTypes.func.isRequired,
|
2016-05-02 18:43:18 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
form = new FormModel();
|
|
|
|
|
2019-01-28 00:42:58 +05:30
|
|
|
actualUsername: string;
|
|
|
|
|
2016-05-02 18:43:18 +05:30
|
|
|
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 = () => {
|
2019-01-28 00:42:58 +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();
|
2017-02-27 11:17:31 +05:30
|
|
|
return Promise.resolve();
|
2016-05-14 13:24:26 +05:30
|
|
|
}
|
|
|
|
|
2017-02-27 11:17:31 +05:30
|
|
|
return this.context.onSubmit({
|
2016-05-12 10:00:10 +05:30
|
|
|
form,
|
2019-01-28 00:42:58 +05:30
|
|
|
sendData: () => {
|
|
|
|
const { username, password } = form.serialize();
|
|
|
|
return changeUsername(this.context.userId, username, password);
|
|
|
|
},
|
2016-05-12 10:00:10 +05:30
|
|
|
}).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) => ({
|
2019-01-28 00:42:58 +05:30
|
|
|
username: state.user.username,
|
2016-05-02 18:43:18 +05:30
|
|
|
}), {
|
2019-01-28 00:42:58 +05:30
|
|
|
updateUsername: (username) => updateUser({username}),
|
2016-05-02 18:43:18 +05:30
|
|
|
})(ChangeUsernamePage);
|