-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
183 lines (170 loc) · 6.76 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, { useState } from "react";
import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
import Container from "@mui/material/Container";
import Avatar from "@mui/material/Avatar";
import Typography from "@mui/material/Typography";
import { Box, MenuItem, Select } from "@mui/material";
import HowToRegOutlinedIcon from "@mui/icons-material/HowToRegOutlined";
import RootPage from "../root";
import "./create.css"; // Import CSS file for additional styling
import { backend_post } from "../../utils";
const CreateUser = () => {
// const navigate = useNavigate();
const [regStatus, setRegStatus] = useState("");
const handleTokenResponse = (data: any) => {
console.log(data);
if (!("username" in data) || typeof data["username"] !== "string") {
setRegStatus("failed");
return;
} else {
setRegStatus("success");
}
// event.currentTarget.reset()
// navigate(0);
// navigate("/home", { replace: true });
};
// TODO: Add first and last name validation
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
setRegStatus("submitting");
event.preventDefault();
const data = new FormData(event.currentTarget);
const first_name = data.get("first_name");
const last_name = data.get("last_name");
let username = data.get("username");
let email = data.get("email");
if (username == "") username = first_name + "." + last_name;
if (email == "") email = first_name + "." + last_name + "@uni.org";
if (username !== null) username = username.toString().toLowerCase();
if (email !== null) email = email.toString().toLowerCase();
backend_post(
"user/register/",
JSON.stringify({
username: username,
password: data.get("password"),
first_name: first_name,
last_name: last_name,
email: email,
role: data.get("role")
})
)
.then((resp) => resp.json())
.then((data) => handleTokenResponse(data))
.catch((error) => console.log(error));
};
return (
<RootPage>
<Container component="main" maxWidth="xs">
<Box
sx={{
marginTop: 8,
display: "flex",
flexDirection: "column",
alignItems: "center"
}}
>
<Avatar sx={{ m: 1, bgcolor: "secondary.main" }}>
<HowToRegOutlinedIcon />
</Avatar>
<Typography component="h1" variant="h5">
Register User
</Typography>
<Box
component="form"
onSubmit={handleSubmit}
noValidate
sx={{ mt: 1 }}
color={"primary"}
>
<TextField
margin="normal"
required
fullWidth
id="first_name"
label="First Name"
name="first_name"
autoComplete="first_name"
autoFocus
color="primary"
/>
<TextField
margin="normal"
required
fullWidth
id="last_name"
label="Last Name"
name="last_name"
autoComplete="last_name"
autoFocus
color="primary"
/>
<TextField
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
/>
<TextField
margin="normal"
fullWidth
id="username"
label="Username"
name="username"
autoComplete="username"
autoFocus
color="primary"
/>
<TextField
margin="normal"
fullWidth
id="email"
label="Email"
name="email"
autoComplete="email"
autoFocus
color="primary"
/>
<Select
color="primary"
required
fullWidth
id="role"
name="role"
autoComplete="role"
autoFocus
label="Role"
defaultValue="student"
>
<MenuItem value="student">Student</MenuItem>
<MenuItem value="teacher">Teacher</MenuItem>
<MenuItem value="admin">Admin</MenuItem>
</Select>
{regStatus === "success" && (
<Typography variant="body1" color="success">
Registration successful!
</Typography>
)}
{regStatus === "failed" && (
<Typography variant="body1" color="error">
Registration failed. Please try again.
</Typography>
)}
<Button
type="submit"
fullWidth
variant="contained"
sx={{ mt: 3, mb: 2 }}
>
Create
</Button>
</Box>
</Box>
</Container>
</RootPage>
);
};
export default CreateUser;