2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2020-07-22 15:31:12 +05:30
|
|
|
|
|
|
|
import { connect } from 'app/functions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { updateUser } from 'app/components/user/actions';
|
|
|
|
import { changeUsername } from 'app/services/api/accounts';
|
|
|
|
import { FormModel } from 'app/components/ui/form';
|
|
|
|
import ChangeUsername from 'app/components/profile/changeUsername/ChangeUsername';
|
2019-12-12 12:56:23 +05:30
|
|
|
import Context from 'app/components/profile/Context';
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
type Props = {
|
2020-05-24 04:38:24 +05:30
|
|
|
username: string;
|
|
|
|
updateUsername: (username: string) => void;
|
2019-06-30 19:02:50 +05:30
|
|
|
};
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
class ChangeUsernamePage extends React.Component<Props> {
|
2020-05-24 04:38:24 +05:30
|
|
|
static contextType = Context;
|
2020-10-11 22:03:55 +05:30
|
|
|
declare context: React.ContextType<typeof Context>;
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
form = new FormModel();
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
actualUsername: string = this.props.username;
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
componentWillUnmount() {
|
|
|
|
this.props.updateUsername(this.actualUsername);
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ChangeUsername
|
|
|
|
form={this.form}
|
|
|
|
onSubmit={this.onSubmit}
|
|
|
|
onChange={this.onUsernameChange}
|
|
|
|
username={this.props.username}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onUsernameChange = (username: string) => {
|
|
|
|
this.props.updateUsername(username);
|
|
|
|
};
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onSubmit = () => {
|
|
|
|
const { form } = this;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (this.actualUsername === this.props.username) {
|
|
|
|
this.context.goToProfile();
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2016-05-02 18:43:18 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return this.context
|
|
|
|
.onSubmit({
|
|
|
|
form,
|
|
|
|
sendData: () => {
|
|
|
|
const { username, password } = form.serialize();
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return changeUsername(this.context.userId, username, password);
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.actualUsername = form.value('username');
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
this.context.goToProfile();
|
|
|
|
});
|
|
|
|
};
|
2016-05-02 18:43:18 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export default connect(
|
2020-07-22 15:31:12 +05:30
|
|
|
(state) => ({
|
2020-05-24 04:38:24 +05:30
|
|
|
username: state.user.username,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
updateUsername: (username: string) => updateUser({ username }),
|
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
)(ChangeUsernamePage);
|