2022-05-04 17:09:48 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2022 Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-13 14:54:44 -08:00
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
2022-06-06 22:33:13 +02:00
|
|
|
import { MatrixClient } from "matrix-js-sdk";
|
|
|
|
|
|
2022-01-05 17:27:01 -08:00
|
|
|
import { Button } from "../button";
|
2022-01-05 17:19:03 -08:00
|
|
|
import { useProfile } from "./useProfile";
|
2022-01-05 17:27:01 -08:00
|
|
|
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
|
|
|
|
|
import { Modal, ModalContent } from "../Modal";
|
2022-02-18 16:02:27 -08:00
|
|
|
import { AvatarInputField } from "../input/AvatarInputField";
|
|
|
|
|
import styles from "./ProfileModal.module.css";
|
2021-12-13 14:54:44 -08:00
|
|
|
|
2022-06-06 22:33:13 +02:00
|
|
|
interface Props {
|
|
|
|
|
client: MatrixClient;
|
|
|
|
|
onClose: () => {};
|
|
|
|
|
[rest: string]: unknown;
|
|
|
|
|
}
|
|
|
|
|
export function ProfileModal({ client, ...rest }: Props) {
|
2021-12-13 14:54:44 -08:00
|
|
|
const { onClose } = rest;
|
|
|
|
|
const {
|
|
|
|
|
success,
|
|
|
|
|
error,
|
|
|
|
|
loading,
|
|
|
|
|
displayName: initialDisplayName,
|
2022-02-18 16:02:27 -08:00
|
|
|
avatarUrl,
|
2021-12-15 14:31:07 -08:00
|
|
|
saveProfile,
|
|
|
|
|
} = useProfile(client);
|
2021-12-13 14:54:44 -08:00
|
|
|
const [displayName, setDisplayName] = useState(initialDisplayName || "");
|
2022-02-18 16:02:27 -08:00
|
|
|
const [removeAvatar, setRemoveAvatar] = useState(false);
|
|
|
|
|
|
|
|
|
|
const onRemoveAvatar = useCallback(() => {
|
|
|
|
|
setRemoveAvatar(true);
|
|
|
|
|
}, []);
|
2021-12-13 14:54:44 -08:00
|
|
|
|
|
|
|
|
const onChangeDisplayName = useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
setDisplayName(e.target.value);
|
|
|
|
|
},
|
|
|
|
|
[setDisplayName]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onSubmit = useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const data = new FormData(e.target);
|
2022-06-11 15:14:00 +02:00
|
|
|
const displayNameDataEntry = data.get("displayName");
|
2022-06-06 22:33:13 +02:00
|
|
|
const avatar: File | string = data.get("avatar");
|
2022-06-11 15:14:00 +02:00
|
|
|
|
2022-06-06 22:33:13 +02:00
|
|
|
const avatarSize =
|
|
|
|
|
typeof avatar == "string" ? avatar.length : avatar.size;
|
2022-06-11 15:14:00 +02:00
|
|
|
const displayName =
|
|
|
|
|
typeof displayNameDataEntry == "string"
|
|
|
|
|
? displayNameDataEntry
|
|
|
|
|
: displayNameDataEntry.name;
|
|
|
|
|
|
2021-12-15 14:31:07 -08:00
|
|
|
saveProfile({
|
|
|
|
|
displayName,
|
2022-06-06 22:33:13 +02:00
|
|
|
avatar: avatar && avatarSize > 0 ? avatar : undefined,
|
|
|
|
|
removeAvatar: removeAvatar && (!avatar || avatarSize === 0),
|
2021-12-15 14:31:07 -08:00
|
|
|
});
|
2021-12-13 14:54:44 -08:00
|
|
|
},
|
2022-02-18 16:02:27 -08:00
|
|
|
[saveProfile, removeAvatar]
|
2021-12-13 14:54:44 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (success) {
|
|
|
|
|
onClose();
|
|
|
|
|
}
|
|
|
|
|
}, [success, onClose]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Modal title="Profile" isDismissable {...rest}>
|
|
|
|
|
<ModalContent>
|
|
|
|
|
<form onSubmit={onSubmit}>
|
2022-02-18 16:02:27 -08:00
|
|
|
<FieldRow className={styles.avatarFieldRow}>
|
|
|
|
|
<AvatarInputField
|
|
|
|
|
id="avatar"
|
|
|
|
|
name="avatar"
|
|
|
|
|
label="Avatar"
|
|
|
|
|
avatarUrl={avatarUrl}
|
|
|
|
|
displayName={displayName}
|
|
|
|
|
onRemoveAvatar={onRemoveAvatar}
|
|
|
|
|
/>
|
|
|
|
|
</FieldRow>
|
2022-01-21 16:36:21 -08:00
|
|
|
<FieldRow>
|
|
|
|
|
<InputField
|
|
|
|
|
id="userId"
|
|
|
|
|
name="userId"
|
|
|
|
|
label="User Id"
|
|
|
|
|
type="text"
|
|
|
|
|
disabled
|
|
|
|
|
value={client.getUserId()}
|
|
|
|
|
/>
|
|
|
|
|
</FieldRow>
|
2021-12-13 14:54:44 -08:00
|
|
|
<FieldRow>
|
|
|
|
|
<InputField
|
|
|
|
|
id="displayName"
|
|
|
|
|
name="displayName"
|
|
|
|
|
label="Display Name"
|
|
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
placeholder="Display Name"
|
|
|
|
|
value={displayName}
|
|
|
|
|
onChange={onChangeDisplayName}
|
|
|
|
|
/>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
{error && (
|
|
|
|
|
<FieldRow>
|
|
|
|
|
<ErrorMessage>{error.message}</ErrorMessage>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
)}
|
|
|
|
|
<FieldRow rightAlign>
|
|
|
|
|
<Button type="button" variant="secondary" onPress={onClose}>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button type="submit" disabled={loading}>
|
|
|
|
|
{loading ? "Saving..." : "Save"}
|
|
|
|
|
</Button>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
</form>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|