2019-01-27 22:12:58 +03:00
|
|
|
// @flow
|
2017-08-22 21:49:50 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2016-05-02 16:13:18 +03:00
|
|
|
|
2019-01-27 22:12:58 +03:00
|
|
|
import { changeUsername } from 'services/api/accounts';
|
2016-05-02 16:13:18 +03:00
|
|
|
import { FormModel } from 'components/ui/form';
|
|
|
|
import ChangeUsername from 'components/profile/changeUsername/ChangeUsername';
|
|
|
|
|
2019-06-30 16:32:50 +03:00
|
|
|
type OwnProps = {|
|
|
|
|
|};
|
|
|
|
|
|
|
|
type Props = {
|
2019-01-27 22:12:58 +03:00
|
|
|
username: string;
|
|
|
|
updateUsername: (username: string) => void;
|
2019-06-30 16:32:50 +03:00
|
|
|
};
|
2016-05-02 16:13:18 +03:00
|
|
|
|
2019-01-27 22:12:58 +03:00
|
|
|
class ChangeUsernamePage extends Component<Props> {
|
2016-05-12 07:30:10 +03:00
|
|
|
static contextTypes = {
|
2019-01-27 22:12:58 +03:00
|
|
|
userId: PropTypes.number.isRequired,
|
2016-05-14 10:54:26 +03:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2019-01-27 22:12:58 +03:00
|
|
|
goToProfile: PropTypes.func.isRequired,
|
2016-05-02 16:13:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
form = new FormModel();
|
|
|
|
|
2019-01-27 22:12:58 +03:00
|
|
|
actualUsername: string;
|
|
|
|
|
2016-05-02 16:13:18 +03:00
|
|
|
componentWillMount() {
|
2016-05-22 21:02:46 +03:00
|
|
|
this.actualUsername = this.props.username;
|
2016-05-02 16:13:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2016-05-22 21:02:46 +03:00
|
|
|
this.props.updateUsername(this.actualUsername);
|
2016-05-02 16:13:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ChangeUsername form={this.form}
|
|
|
|
onSubmit={this.onSubmit}
|
|
|
|
onChange={this.onUsernameChange}
|
|
|
|
username={this.props.username}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-30 16:32:50 +03:00
|
|
|
onUsernameChange = (username: string) => {
|
2016-05-02 16:13:18 +03:00
|
|
|
this.props.updateUsername(username);
|
|
|
|
};
|
|
|
|
|
|
|
|
onSubmit = () => {
|
2019-01-27 22:12:58 +03:00
|
|
|
const { form } = this;
|
2016-05-22 21:02:46 +03:00
|
|
|
if (this.actualUsername === this.props.username) {
|
2016-05-14 10:54:26 +03:00
|
|
|
this.context.goToProfile();
|
2017-02-27 07:47:31 +02:00
|
|
|
return Promise.resolve();
|
2016-05-14 10:54:26 +03:00
|
|
|
}
|
|
|
|
|
2017-02-27 07:47:31 +02:00
|
|
|
return this.context.onSubmit({
|
2016-05-12 07:30:10 +03:00
|
|
|
form,
|
2019-01-27 22:12:58 +03:00
|
|
|
sendData: () => {
|
|
|
|
const { username, password } = form.serialize();
|
|
|
|
return changeUsername(this.context.userId, username, password);
|
|
|
|
},
|
2016-05-12 07:30:10 +03:00
|
|
|
}).then(() => {
|
2016-05-22 21:02:46 +03:00
|
|
|
this.actualUsername = form.value('username');
|
2017-01-02 15:37:02 +02:00
|
|
|
|
2016-05-22 10:53:40 +03:00
|
|
|
this.context.goToProfile();
|
2016-05-02 16:13:18 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { updateUser } from 'components/user/actions';
|
|
|
|
|
2019-06-30 16:32:50 +03:00
|
|
|
export default connect<Props, OwnProps, _, _, _, _>((state) => ({
|
2019-01-27 22:12:58 +03:00
|
|
|
username: state.user.username,
|
2016-05-02 16:13:18 +03:00
|
|
|
}), {
|
2019-01-27 22:12:58 +03:00
|
|
|
updateUsername: (username) => updateUser({username}),
|
2016-05-02 16:13:18 +03:00
|
|
|
})(ChangeUsernamePage);
|