Migrate ProfileSettings to TypeScript

This commit is contained in:
Germain Souquet 2021-08-14 11:36:12 +02:00
parent 447beb8294
commit 2e1d5aa67b

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2019 New Vector Ltd Copyright 2019-2021 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -26,10 +26,25 @@ import ErrorDialog from "../dialogs/ErrorDialog";
import { replaceableComponent } from "../../../utils/replaceableComponent"; import { replaceableComponent } from "../../../utils/replaceableComponent";
import { mediaFromMxc } from "../../../customisations/Media"; import { mediaFromMxc } from "../../../customisations/Media";
interface IProps {
}
interface IState {
userId?: string;
originalDisplayName?: string;
displayName?: string;
originalAvatarUrl?: string;
avatarUrl?: string | ArrayBuffer;
avatarFile?: File;
enableProfileSave?: boolean;
}
@replaceableComponent("views.settings.ProfileSettings") @replaceableComponent("views.settings.ProfileSettings")
export default class ProfileSettings extends React.Component { export default class ProfileSettings extends React.Component<IProps, IState> {
constructor() { private avatarUpload: React.RefObject<HTMLInputElement> = createRef();
super(); constructor(props: IProps) {
super(props);
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
let avatarUrl = OwnProfileStore.instance.avatarMxc; let avatarUrl = OwnProfileStore.instance.avatarMxc;
@ -43,17 +58,15 @@ export default class ProfileSettings extends React.Component {
avatarFile: null, avatarFile: null,
enableProfileSave: false, enableProfileSave: false,
}; };
this._avatarUpload = createRef();
} }
_uploadAvatar = () => { private uploadAvatar = (): void => {
this._avatarUpload.current.click(); this.avatarUpload.current.click();
}; };
_removeAvatar = () => { private removeAvatar = (): void => {
// clear file upload field so same file can be selected // clear file upload field so same file can be selected
this._avatarUpload.current.value = ""; this.avatarUpload.current.value = "";
this.setState({ this.setState({
avatarUrl: null, avatarUrl: null,
avatarFile: null, avatarFile: null,
@ -61,7 +74,7 @@ export default class ProfileSettings extends React.Component {
}); });
}; };
_cancelProfileChanges = async (e) => { private cancelProfileChanges = async (e: React.MouseEvent): Promise<void> => {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
@ -74,7 +87,7 @@ export default class ProfileSettings extends React.Component {
}); });
}; };
_saveProfile = async (e) => { private saveProfile = async (e: React.FormEvent<HTMLFormElement>): Promise<void> => {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
@ -82,7 +95,7 @@ export default class ProfileSettings extends React.Component {
this.setState({ enableProfileSave: false }); this.setState({ enableProfileSave: false });
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
const newState = {}; const newState: IState = {};
const displayName = this.state.displayName.trim(); const displayName = this.state.displayName.trim();
try { try {
@ -115,14 +128,14 @@ export default class ProfileSettings extends React.Component {
this.setState(newState); this.setState(newState);
}; };
_onDisplayNameChanged = (e) => { private onDisplayNameChanged = (e: React.ChangeEvent<HTMLInputElement>): void => {
this.setState({ this.setState({
displayName: e.target.value, displayName: e.target.value,
enableProfileSave: true, enableProfileSave: true,
}); });
}; };
_onAvatarChanged = (e) => { private onAvatarChanged = (e: React.ChangeEvent<HTMLInputElement>): void => {
if (!e.target.files || !e.target.files.length) { if (!e.target.files || !e.target.files.length) {
this.setState({ this.setState({
avatarUrl: this.state.originalAvatarUrl, avatarUrl: this.state.originalAvatarUrl,
@ -144,7 +157,7 @@ export default class ProfileSettings extends React.Component {
reader.readAsDataURL(file); reader.readAsDataURL(file);
}; };
render() { public render(): JSX.Element {
const hostingSignupLink = getHostingLink('user-settings'); const hostingSignupLink = getHostingLink('user-settings');
let hostingSignup = null; let hostingSignup = null;
if (hostingSignupLink) { if (hostingSignupLink) {
@ -165,16 +178,16 @@ export default class ProfileSettings extends React.Component {
const AvatarSetting = sdk.getComponent('settings.AvatarSetting'); const AvatarSetting = sdk.getComponent('settings.AvatarSetting');
return ( return (
<form <form
onSubmit={this._saveProfile} onSubmit={this.saveProfile}
autoComplete="off" autoComplete="off"
noValidate={true} noValidate={true}
className="mx_ProfileSettings_profileForm" className="mx_ProfileSettings_profileForm"
> >
<input <input
type="file" type="file"
ref={this._avatarUpload} ref={this.avatarUpload}
className="mx_ProfileSettings_avatarUpload" className="mx_ProfileSettings_avatarUpload"
onChange={this._onAvatarChanged} onChange={this.onAvatarChanged}
accept="image/*" accept="image/*"
/> />
<div className="mx_ProfileSettings_profile"> <div className="mx_ProfileSettings_profile">
@ -185,7 +198,7 @@ export default class ProfileSettings extends React.Component {
type="text" type="text"
value={this.state.displayName} value={this.state.displayName}
autoComplete="off" autoComplete="off"
onChange={this._onDisplayNameChanged} onChange={this.onDisplayNameChanged}
/> />
<p> <p>
{ this.state.userId } { this.state.userId }
@ -196,19 +209,19 @@ export default class ProfileSettings extends React.Component {
avatarUrl={this.state.avatarUrl} avatarUrl={this.state.avatarUrl}
avatarName={this.state.displayName || this.state.userId} avatarName={this.state.displayName || this.state.userId}
avatarAltText={_t("Profile picture")} avatarAltText={_t("Profile picture")}
uploadAvatar={this._uploadAvatar} uploadAvatar={this.uploadAvatar}
removeAvatar={this._removeAvatar} /> removeAvatar={this.removeAvatar} />
</div> </div>
<div className="mx_ProfileSettings_buttons"> <div className="mx_ProfileSettings_buttons">
<AccessibleButton <AccessibleButton
onClick={this._cancelProfileChanges} onClick={this.cancelProfileChanges}
kind="link" kind="link"
disabled={!this.state.enableProfileSave} disabled={!this.state.enableProfileSave}
> >
{ _t("Cancel") } { _t("Cancel") }
</AccessibleButton> </AccessibleButton>
<AccessibleButton <AccessibleButton
onClick={this._saveProfile} onClick={this.saveProfile}
kind="primary" kind="primary"
disabled={!this.state.enableProfileSave} disabled={!this.state.enableProfileSave}
> >