element-call/src/room/RoomAuthView.jsx

126 lines
4 KiB
React
Raw Normal View History

2022-05-04 16:09:48 +00: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.
*/
2022-01-06 00:09:51 +00:00
import React, { useCallback, useState } from "react";
import styles from "./RoomAuthView.module.css";
import { useClient } from "../ClientContext";
2022-01-06 00:09:51 +00:00
import { Button } from "../button";
import { Body, Caption, Link, Headline } from "../typography/Typography";
import { Header, HeaderLogo, LeftNav, RightNav } from "../Header";
import { useLocation } from "react-router-dom";
2022-01-06 00:34:01 +00:00
import { useRecaptcha } from "../auth/useRecaptcha";
2022-01-06 01:27:01 +00:00
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
2022-01-06 00:09:51 +00:00
import { randomString } from "matrix-js-sdk/src/randomstring";
2022-01-06 00:47:53 +00:00
import { useInteractiveRegistration } from "../auth/useInteractiveRegistration";
2022-01-06 00:09:51 +00:00
import { Form } from "../form/Form";
2022-01-06 22:10:33 +00:00
import { UserMenuContainer } from "../UserMenuContainer";
2022-02-15 20:46:58 +00:00
import { generateRandomName } from "../auth/generateRandomName";
2022-01-05 23:35:12 +00:00
export function RoomAuthView() {
const { setClient } = useClient();
2022-01-06 00:09:51 +00:00
const [loading, setLoading] = useState(false);
const [error, setError] = useState();
const [{ privacyPolicyUrl, recaptchaKey }, register] =
useInteractiveRegistration();
const { execute, reset, recaptchaId } = useRecaptcha(recaptchaKey);
2022-01-06 00:09:51 +00:00
const onSubmit = useCallback(
(e) => {
e.preventDefault();
const data = new FormData(e.target);
2022-02-15 20:46:58 +00:00
const displayName = data.get("displayName");
2022-01-06 00:09:51 +00:00
async function submit() {
setError(undefined);
setLoading(true);
const recaptchaResponse = await execute();
2022-02-15 20:46:58 +00:00
const userName = generateRandomName();
const [client, session] = await register(
2022-02-15 20:46:58 +00:00
userName,
randomString(16),
displayName,
recaptchaResponse,
true
);
setClient(client, session);
2022-01-06 00:09:51 +00:00
}
submit().catch((error) => {
console.error(error);
setLoading(false);
setError(error);
reset();
});
},
[register, reset, execute]
);
const location = useLocation();
return (
<>
<Header>
<LeftNav>
<HeaderLogo />
</LeftNav>
2022-01-06 22:10:33 +00:00
<RightNav>
<UserMenuContainer preventNavigation />
2022-01-06 22:10:33 +00:00
</RightNav>
2022-01-06 00:09:51 +00:00
</Header>
<div className={styles.container}>
<main className={styles.main}>
<Headline className={styles.headline}>Join Call</Headline>
<Form className={styles.form} onSubmit={onSubmit}>
<FieldRow>
<InputField
2022-02-15 20:46:58 +00:00
id="displayName"
name="displayName"
label="Display Name"
placeholder="Display Name"
2022-01-06 00:09:51 +00:00
type="text"
required
autoComplete="off"
/>
</FieldRow>
<Caption>
2022-02-02 22:31:11 +00:00
By clicking "Join call now", you agree to our{" "}
2022-01-06 00:09:51 +00:00
<Link href={privacyPolicyUrl}>Terms and conditions</Link>
</Caption>
{error && (
<FieldRow>
<ErrorMessage>{error.message}</ErrorMessage>
</FieldRow>
)}
<Button type="submit" size="lg" disabled={loading}>
{loading ? "Loading..." : "Join call now"}
</Button>
<div id={recaptchaId} />
</Form>
</main>
<Body className={styles.footer}>
{"Not registered yet? "}
<Link
color="primary"
to={{ pathname: "/login", state: { from: location } }}
>
Create an account
</Link>
</Body>
</div>
</>
);
2022-01-05 23:35:12 +00:00
}