2015-10-07 20:19:29 +03:00
|
|
|
/*
|
2021-09-01 12:19:25 +03:00
|
|
|
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
2015-10-07 20:19:29 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-06-19 09:57:28 +03:00
|
|
|
import React from 'react';
|
2021-06-29 15:11:58 +03:00
|
|
|
import { MatrixClientPeg } from '../../../MatrixClientPeg';
|
2017-06-08 14:33:29 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2021-06-29 15:11:58 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-08-17 20:05:10 +03:00
|
|
|
import EditableTextContainer from "../elements/EditableTextContainer";
|
2015-10-07 20:19:29 +03:00
|
|
|
|
2021-03-09 06:20:07 +03:00
|
|
|
@replaceableComponent("views.settings.ChangeDisplayName")
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class ChangeDisplayName extends React.Component {
|
2021-08-14 12:06:34 +03:00
|
|
|
private getDisplayName = async (): Promise<string> => {
|
2017-10-11 19:56:17 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
2018-06-19 09:56:04 +03:00
|
|
|
try {
|
|
|
|
const res = await cli.getProfileInfo(cli.getUserId());
|
|
|
|
return res.displayname;
|
|
|
|
} catch (e) {
|
2016-07-29 19:35:48 +03:00
|
|
|
throw new Error("Failed to fetch display name");
|
2018-06-19 09:56:04 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2015-10-07 20:19:29 +03:00
|
|
|
|
2021-08-14 12:06:34 +03:00
|
|
|
private changeDisplayName = (newDisplayname: string): Promise<{}> => {
|
2017-10-11 19:56:17 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
2021-08-14 12:06:34 +03:00
|
|
|
return cli.setDisplayName(newDisplayname).catch(function() {
|
|
|
|
throw new Error("Failed to set display name");
|
2015-10-07 20:19:29 +03:00
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2015-11-26 20:10:36 +03:00
|
|
|
|
2021-08-14 12:06:34 +03:00
|
|
|
public render(): JSX.Element {
|
2016-07-29 19:35:48 +03:00
|
|
|
return (
|
|
|
|
<EditableTextContainer
|
2021-08-14 12:06:34 +03:00
|
|
|
getInitialValue={this.getDisplayName}
|
2017-06-08 14:33:29 +03:00
|
|
|
placeholder={_t("No display name")}
|
2017-03-02 20:29:06 +03:00
|
|
|
blurToSubmit={true}
|
2021-08-14 12:06:34 +03:00
|
|
|
onSubmit={this.changeDisplayName} />
|
2016-07-29 19:35:48 +03:00
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|