-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
112 lines (102 loc) · 3.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
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 } from "@mui/material";
import AppRegistrationIcon from "@mui/icons-material/AppRegistration";
import RootPage from "../root";
import "./create.css"; // Import CSS file for additional styling
import { backend_post } from "../../utils";
// import { post_db } from "../../utils";
const CreateCourse = () => {
// const navigate = useNavigate();
const [regStatus, setRegStatus] = useState("");
const handleResponse = (data: any) => {
console.log(data);
if (
!("course_name" in data) ||
typeof data["course_name"] !== "string"
) {
setRegStatus("failed");
return;
} else {
setRegStatus("success");
}
};
// TODO: Add first and last name validation
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
setRegStatus("submitting");
event.preventDefault();
const data = new FormData(event.currentTarget);
backend_post(
"course/create/",
JSON.stringify({
course_name: data.get("course_name")
})
)
.then((resp) => resp.json())
.then((data) => handleResponse(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" }}>
<AppRegistrationIcon />
</Avatar>
<Typography component="h1" variant="h5">
Create Course
</Typography>
<Box
component="form"
onSubmit={handleSubmit}
noValidate
sx={{ mt: 1 }}
color={"primary"}
>
<TextField
margin="normal"
required
fullWidth
id="course_name"
label="Course Name"
name="course_name"
autoComplete="course_name"
autoFocus
color="primary"
/>
{regStatus === "success" && (
<Typography variant="body1" color="success">
Creation successful!
</Typography>
)}
{regStatus === "failed" && (
<Typography variant="body1" color="error">
Creation failed. Please try again.
</Typography>
)}
<Button
type="submit"
fullWidth
variant="contained"
sx={{ mt: 3, mb: 2 }}
>
Create
</Button>
</Box>
</Box>
</Container>
</RootPage>
);
};
export default CreateCourse;