2024-03-13 17:56:04 +01:00
|
|
|
import { useState } from "react";
|
2024-03-18 00:44:11 +01:00
|
|
|
import { NewUser } from "../Types/goTypes";
|
2024-03-14 11:23:57 +01:00
|
|
|
import { api } from "../API/API";
|
2024-03-15 14:16:11 +01:00
|
|
|
import Logo from "../assets/Logo.svg";
|
2024-03-15 14:52:05 +01:00
|
|
|
import Button from "./Button";
|
2024-03-18 00:44:11 +01:00
|
|
|
import InputField from "./InputField";
|
2024-03-16 15:14:03 +01:00
|
|
|
|
2024-03-20 13:56:13 +01:00
|
|
|
/**
|
|
|
|
* Renders a registration form for the admin to add new users in.
|
|
|
|
* @returns The JSX element representing the registration form.
|
|
|
|
*/
|
2024-03-14 11:23:57 +01:00
|
|
|
export default function Register(): JSX.Element {
|
2024-03-17 22:48:38 +01:00
|
|
|
const [username, setUsername] = useState<string>();
|
|
|
|
const [password, setPassword] = useState<string>();
|
|
|
|
const [errMessage, setErrMessage] = useState<string>();
|
|
|
|
|
2024-03-14 11:23:57 +01:00
|
|
|
const handleRegister = async (): Promise<void> => {
|
2024-04-04 11:54:34 +02:00
|
|
|
if (username === "" || password === "") {
|
|
|
|
alert("Must provide username and password");
|
|
|
|
return;
|
|
|
|
}
|
2024-03-17 22:48:38 +01:00
|
|
|
const newUser: NewUser = {
|
2024-04-04 11:54:34 +02:00
|
|
|
username: username?.replace(/ /g, "") ?? "",
|
2024-03-17 22:48:38 +01:00
|
|
|
password: password ?? "",
|
|
|
|
};
|
|
|
|
const response = await api.registerUser(newUser);
|
|
|
|
if (response.success) {
|
2024-04-04 11:54:34 +02:00
|
|
|
alert(`${newUser.username} added!`);
|
2024-04-01 02:17:57 +02:00
|
|
|
setPassword("");
|
|
|
|
setUsername("");
|
2024-03-17 22:48:38 +01:00
|
|
|
} else {
|
2024-04-04 11:54:34 +02:00
|
|
|
alert("User not added, name could be taken");
|
2024-03-17 22:48:38 +01:00
|
|
|
setErrMessage(response.message ?? "Unknown error");
|
2024-03-18 14:43:28 +01:00
|
|
|
console.error(errMessage);
|
2024-03-17 22:48:38 +01:00
|
|
|
}
|
2024-03-13 17:56:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-03-16 02:47:17 +01:00
|
|
|
<div className="flex flex-col h-fit w-screen items-center justify-center">
|
2024-03-15 14:16:11 +01:00
|
|
|
<div className="border-4 border-black bg-white flex flex-col items-center justify-center h-fit w-fit rounded-3xl content-center pl-20 pr-20">
|
2024-03-13 17:56:04 +01:00
|
|
|
<form
|
2024-03-15 14:16:11 +01:00
|
|
|
className="bg-white rounded px-8 pt-6 pb-8 mb-4 items-center justify-center flex flex-col w-fit h-fit"
|
2024-03-13 17:56:04 +01:00
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault();
|
2024-03-14 11:23:57 +01:00
|
|
|
void handleRegister();
|
2024-03-13 17:56:04 +01:00
|
|
|
}}
|
|
|
|
>
|
2024-03-15 14:16:11 +01:00
|
|
|
<img
|
|
|
|
src={Logo}
|
|
|
|
className="logo w-[7vw] mb-10 mt-10"
|
|
|
|
alt="TTIME Logo"
|
|
|
|
/>
|
2024-03-15 14:52:05 +01:00
|
|
|
<h3 className="pb-4 mb-2 text-center font-bold text-[18px]">
|
|
|
|
Register New User
|
|
|
|
</h3>
|
2024-04-02 13:14:08 +02:00
|
|
|
<div className="space-y-3">
|
|
|
|
<InputField
|
|
|
|
label="Username"
|
|
|
|
type="text"
|
|
|
|
value={username ?? ""}
|
|
|
|
onChange={(e) => {
|
|
|
|
setUsername(e.target.value);
|
|
|
|
}}
|
2024-04-11 00:32:18 +02:00
|
|
|
placeholder={"Username"}
|
2024-04-02 13:14:08 +02:00
|
|
|
/>
|
|
|
|
<InputField
|
|
|
|
label="Password"
|
|
|
|
type="password"
|
|
|
|
value={password ?? ""}
|
|
|
|
onChange={(e) => {
|
|
|
|
setPassword(e.target.value);
|
|
|
|
}}
|
2024-04-11 00:32:18 +02:00
|
|
|
placeholder={"Password"}
|
2024-04-02 13:14:08 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2024-03-13 17:56:04 +01:00
|
|
|
<div className="flex items-center justify-between">
|
2024-03-15 14:52:05 +01:00
|
|
|
<Button
|
|
|
|
text="Register"
|
|
|
|
onClick={(): void => {
|
|
|
|
return;
|
|
|
|
}}
|
2024-03-13 17:56:04 +01:00
|
|
|
type="submit"
|
2024-03-15 14:52:05 +01:00
|
|
|
/>
|
2024-03-13 17:56:04 +01:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<p className="text-center text-gray-500 text-xs"></p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|