element-call/src/RegisterPage.jsx

118 lines
3.4 KiB
React
Raw Normal View History

2021-08-20 16:23:12 -07:00
/*
Copyright 2021 New Vector Ltd
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-09-10 12:20:17 -07:00
import React, { useCallback, useRef, useState } from "react";
2021-08-20 16:23:12 -07:00
import { useHistory, useLocation, Link } from "react-router-dom";
2021-12-02 17:21:37 -08:00
import { Header, LeftNav, HeaderLogo } from "./Header";
2021-12-07 11:59:57 -08:00
import { FieldRow, InputField, ErrorMessage } from "./Input";
2021-08-20 16:23:12 -07:00
import { Center, Content, Info, Modal } from "./Layout";
2021-12-07 11:59:57 -08:00
import { Button } from "./button";
2021-12-09 12:58:30 -08:00
import { useClient } from "./ConferenceCallManagerHooks";
2021-08-20 16:23:12 -07:00
2021-12-09 12:58:30 -08:00
export function RegisterPage() {
// TODO: Handle hitting login page with authenticated client
const { register } = useClient();
2021-08-20 16:23:12 -07:00
const registerUsernameRef = useRef();
const registerPasswordRef = useRef();
const history = useHistory();
const location = useLocation();
const [loading, setLoading] = useState(false);
2021-09-10 12:20:17 -07:00
const [error, setError] = useState();
2021-08-20 16:23:12 -07:00
const onSubmitRegisterForm = useCallback(
(e) => {
e.preventDefault();
setLoading(true);
2021-12-09 12:58:30 -08:00
register(
2021-08-20 16:23:12 -07:00
registerUsernameRef.current.value,
2021-09-10 12:20:17 -07:00
registerPasswordRef.current.value
)
.then(() => {
2021-08-20 16:23:12 -07:00
if (location.state && location.state.from) {
2021-12-09 12:58:30 -08:00
history.push(location.state.from);
2021-08-20 16:23:12 -07:00
} else {
2021-12-09 12:58:30 -08:00
history.push("/");
2021-08-20 16:23:12 -07:00
}
2021-09-10 12:20:17 -07:00
})
.catch((error) => {
setError(error);
setLoading(false);
});
2021-08-20 16:23:12 -07:00
},
2021-12-09 12:58:30 -08:00
[register, location, history]
2021-08-20 16:23:12 -07:00
);
return (
<>
<Header>
2021-12-02 17:21:37 -08:00
<LeftNav>
<HeaderLogo />
</LeftNav>
2021-08-20 16:23:12 -07:00
</Header>
<Content>
<Center>
{loading ? (
<div>Loading...</div>
) : (
<Modal>
<h2>Register</h2>
<form onSubmit={onSubmitRegisterForm}>
2021-08-20 16:23:12 -07:00
<FieldRow>
<InputField
type="text"
ref={registerUsernameRef}
placeholder="Username"
label="Username"
autoCorrect="off"
autoCapitalize="none"
/>
2021-08-20 16:23:12 -07:00
</FieldRow>
<FieldRow>
<InputField
type="password"
ref={registerPasswordRef}
placeholder="Password"
label="Password"
/>
</FieldRow>
{error && (
<FieldRow>
<ErrorMessage>{error.message}</ErrorMessage>
</FieldRow>
)}
<FieldRow rightAlign>
<Button type="submit">Register</Button>
</FieldRow>
</form>
<Info>
Already have an account?{" "}
<Link
to={{
pathname: "/login",
state: location.state,
}}
>
Sign in here
</Link>
</Info>
</Modal>
)}
2021-08-20 16:23:12 -07:00
</Center>
</Content>
</>
);
}