element-call/src/home/UnauthenticatedView.jsx

160 lines
5.1 KiB
React
Raw Normal View History

2022-01-04 16:00:13 -08:00
import React, { useCallback, useState } from "react";
import { Header, HeaderLogo, LeftNav, RightNav } from "../Header";
import { UserMenuContainer } from "../UserMenuContainer";
import { useHistory } from "react-router-dom";
import { FieldRow, InputField, ErrorMessage } from "../Input";
import { Button } from "../button";
import { randomString } from "matrix-js-sdk/src/randomstring";
import {
createRoom,
2022-01-04 17:09:27 -08:00
roomAliasFromRoomName,
2022-01-04 16:00:13 -08:00
} from "../ConferenceCallManagerHooks";
2022-01-05 16:47:53 -08:00
import { useInteractiveRegistration } from "../auth/useInteractiveRegistration";
2022-01-04 16:00:13 -08:00
import { useModalTriggerState } from "../Modal";
2022-01-05 17:00:02 -08:00
import { JoinExistingCallModal } from "./JoinExistingCallModal";
2022-01-05 16:34:01 -08:00
import { useRecaptcha } from "../auth/useRecaptcha";
2022-01-04 17:09:27 -08:00
import { Body, Caption, Link, Headline } from "../typography/Typography";
2022-01-04 16:00:13 -08:00
import { Form } from "../form/Form";
import styles from "./UnauthenticatedView.module.css";
2022-01-04 17:09:27 -08:00
import commonStyles from "./common.module.css";
2022-01-04 16:00:13 -08:00
export function UnauthenticatedView() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState();
const [{ privacyPolicyUrl, recaptchaKey }, register] =
useInteractiveRegistration();
const { execute, reset, recaptchaId } = useRecaptcha(recaptchaKey);
const onSubmit = useCallback(
(e) => {
e.preventDefault();
const data = new FormData(e.target);
2022-01-04 17:57:23 -08:00
const roomName = data.get("callName");
2022-01-04 16:00:13 -08:00
const userName = data.get("userName");
async function submit() {
setError(undefined);
setLoading(true);
const recaptchaResponse = await execute();
const client = await register(
userName,
randomString(16),
recaptchaResponse,
true
);
const roomIdOrAlias = await createRoom(client, roomName);
if (roomIdOrAlias) {
history.push(`/room/${roomIdOrAlias}`);
}
}
submit().catch((error) => {
if (error.errcode === "M_ROOM_IN_USE") {
setExistingRoomId(roomAliasFromRoomName(roomName));
2022-01-05 11:52:50 -08:00
setLoading(false);
2022-01-04 16:00:13 -08:00
setError(undefined);
modalState.open();
} else {
console.error(error);
setLoading(false);
setError(error);
reset();
}
});
},
2022-01-05 16:09:51 -08:00
[register, reset, execute]
2022-01-04 16:00:13 -08:00
);
const { modalState, modalProps } = useModalTriggerState();
const [existingRoomId, setExistingRoomId] = useState();
const history = useHistory();
const onJoinExistingRoom = useCallback(() => {
history.push(`/${existingRoomId}`);
}, [history, existingRoomId]);
return (
<>
<Header>
<LeftNav>
<HeaderLogo />
</LeftNav>
<RightNav hideMobile>
<UserMenuContainer />
</RightNav>
</Header>
2022-01-04 17:09:27 -08:00
<div className={commonStyles.container}>
<main className={commonStyles.main}>
<HeaderLogo className={commonStyles.logo} />
<Headline className={commonStyles.headline}>
Enter a call name
</Headline>
2022-01-04 16:00:13 -08:00
<Form className={styles.form} onSubmit={onSubmit}>
<FieldRow>
<InputField
id="callName"
name="callName"
label="Call name"
placeholder="Call name"
type="text"
required
autoComplete="off"
/>
</FieldRow>
<FieldRow>
<InputField
id="userName"
name="userName"
label="Your name"
placeholder="Your name"
type="text"
required
autoComplete="off"
/>
</FieldRow>
<Caption>
This site is protected by ReCAPTCHA and the Google{" "}
<Link href="https://www.google.com/policies/privacy/">
Privacy Policy
</Link>{" "}
and{" "}
<Link href="https://policies.google.com/terms">
Terms of Service
</Link>{" "}
apply.
<br />
By clicking "Go", you agree to our{" "}
<Link href={privacyPolicyUrl}>Terms and conditions</Link>
</Caption>
{error && (
<FieldRow>
<ErrorMessage>{error.message}</ErrorMessage>
</FieldRow>
)}
<Button type="submit" size="lg" disabled={loading}>
{loading ? "Loading..." : "Go"}
</Button>
<div id={recaptchaId} />
</Form>
</main>
<footer className={styles.footer}>
<Body className={styles.mobileLoginLink}>
<Link color="primary" to="/l;ogin">
Login to your account
</Link>
</Body>
<Body>
Not registered yet?{" "}
<Link color="primary" to="/register">
Create an account
</Link>
</Body>
</footer>
</div>
{modalState.isOpen && (
<JoinExistingCallModal onJoin={onJoinExistingRoom} {...modalProps} />
)}
</>
);
}