-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
245 lines (226 loc) · 8.84 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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,
List,
ListItemText,
MenuItem,
Select,
capitalize
} 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";
import { MuiFileInput } from "mui-file-input";
import * as Papa from "papaparse";
import { ConfirmUserCSV, User, UserCSV } from "../../types/common";
const CreateUser = () => {
// const navigate = useNavigate();
const [regStatus, setRegStatus] = useState("");
const [file, setFile] = useState<File | null>();
const addUsersCSV = (parsed_users: any) => {
const users: UserCSV[] = parsed_users.data;
for (const user of users) {
const first_name = capitalize(user.first_name);
const last_name = capitalize(user.last_name);
let password = user.password;
let username = user.username;
let email = user.email;
let role = user.role;
if (username == "" || username == null)
username = first_name + "." + last_name;
if (email == "" || email == null)
email = first_name + "." + last_name + "@uni.org";
if (username !== null) username = username.toString().toLowerCase();
if (email !== null) email = email.toString().toLowerCase();
if (role !== null) role = role.toString().toLowerCase();
if (password == "" || password == null)
password = "defaultpassword";
if (role == "" || role == null) role = "student";
backend_post(
"user/register/",
JSON.stringify({
username: username,
password: password,
first_name: first_name,
last_name: last_name,
email: email,
role: role
})
);
}
};
const handleFileUpload = (newFile: File | null) => {
if (newFile == null) return;
setFile(newFile);
Papa.parse(newFile, {
complete: addUsersCSV,
header: 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) => {
if (
!("username" in data) ||
typeof data["username"] !== "string"
) {
setRegStatus("failed");
return;
} else {
setRegStatus("success");
}
})
.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>
<MuiFileInput
label="Import Users CSV"
value={file}
onChange={handleFileUpload}
style={{ textDecorationColor: "white" }}
inputProps={{ accept: ".csv" }}
/>
</Box>
</Box>
</Container>
</RootPage>
);
};
export default CreateUser;