element-call/src/ProfileModal.jsx

95 lines
2.3 KiB
React
Raw Normal View History

2021-12-13 14:54:44 -08:00
import React, { useCallback, useEffect, useState } from "react";
import { Button } from "./button";
2021-12-15 14:31:07 -08:00
import { useProfile } from "./ConferenceCallManagerHooks";
2021-12-13 14:54:44 -08:00
import { FieldRow, InputField, ErrorMessage } from "./Input";
import { Modal, ModalContent } from "./Modal";
export function ProfileModal({
client,
isAuthenticated,
isPasswordlessUser,
...rest
}) {
2021-12-13 14:54:44 -08:00
const { onClose } = rest;
const {
success,
error,
loading,
displayName: initialDisplayName,
2021-12-15 14:31:07 -08:00
saveProfile,
} = useProfile(client);
2021-12-13 14:54:44 -08:00
const [displayName, setDisplayName] = useState(initialDisplayName || "");
const onChangeDisplayName = useCallback(
(e) => {
setDisplayName(e.target.value);
},
[setDisplayName]
);
const onSubmit = useCallback(
(e) => {
e.preventDefault();
const data = new FormData(e.target);
const displayName = data.get("displayName");
2021-12-15 14:31:07 -08:00
const avatar = data.get("avatar");
saveProfile({
displayName,
avatar,
});
2021-12-13 14:54:44 -08:00
},
2021-12-15 14:31:07 -08:00
[saveProfile]
2021-12-13 14:54:44 -08:00
);
useEffect(() => {
if (success) {
onClose();
}
}, [success, onClose]);
return (
<Modal title="Profile" isDismissable {...rest}>
<ModalContent>
<form onSubmit={onSubmit}>
<FieldRow>
<InputField
id="displayName"
name="displayName"
label="Display Name"
type="text"
required
autoComplete="off"
placeholder="Display Name"
value={displayName}
onChange={onChangeDisplayName}
/>
</FieldRow>
2022-01-04 16:00:13 -08:00
{isAuthenticated && !isPasswordlessUser && (
<FieldRow>
<InputField
type="file"
id="avatar"
name="avatar"
label="Avatar"
/>
</FieldRow>
)}
2021-12-13 14:54:44 -08:00
{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>
);
}