2015-10-07 18:19:29 +01:00
|
|
|
/*
|
2021-09-01 10:19:25 +01:00
|
|
|
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
2015-10-07 18:19:29 +01: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 07:57:28 +01:00
|
|
|
import React from "react";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
2017-06-08 12:33:29 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2021-08-17 18:05:10 +01:00
|
|
|
import EditableTextContainer from "../elements/EditableTextContainer";
|
2015-10-07 18:19:29 +01:00
|
|
|
|
2020-08-29 12:14:16 +01:00
|
|
|
export default class ChangeDisplayName extends React.Component {
|
2021-08-14 11:06:34 +02:00
|
|
|
private getDisplayName = async (): Promise<string> => {
|
2023-06-15 08:46:19 +01:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2018-06-19 07:56:04 +01:00
|
|
|
try {
|
2023-02-24 15:28:40 +00:00
|
|
|
const res = await cli.getProfileInfo(cli.getUserId()!);
|
|
|
|
|
return res.displayname ?? "";
|
2018-06-19 07:56:04 +01:00
|
|
|
} catch (e) {
|
2016-07-29 17:35:48 +01:00
|
|
|
throw new Error("Failed to fetch display name");
|
2018-06-19 07:56:04 +01:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2015-10-07 18:19:29 +01:00
|
|
|
|
2021-08-14 11:06:34 +02:00
|
|
|
private changeDisplayName = (newDisplayname: string): Promise<{}> => {
|
2023-06-15 08:46:19 +01:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2021-08-14 11:06:34 +02:00
|
|
|
return cli.setDisplayName(newDisplayname).catch(function () {
|
|
|
|
|
throw new Error("Failed to set display name");
|
2015-10-07 18:19:29 +01:00
|
|
|
});
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2015-11-26 17:10:36 +00:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2016-07-29 17:35:48 +01:00
|
|
|
return (
|
|
|
|
|
<EditableTextContainer
|
2021-08-14 11:06:34 +02:00
|
|
|
getInitialValue={this.getDisplayName}
|
2023-09-26 13:04:17 +01:00
|
|
|
placeholder={_t("settings|general|name_placeholder")}
|
2017-03-02 17:29:06 +00:00
|
|
|
blurToSubmit={true}
|
2021-08-14 11:06:34 +02:00
|
|
|
onSubmit={this.changeDisplayName}
|
|
|
|
|
/>
|
2016-07-29 17:35:48 +01:00
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|