-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
138 lines (126 loc) · 5.3 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
import React, { useState } from "react";
import Button from "@mui/material/Button";
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 AppRegistrationIcon from "@mui/icons-material/AppRegistration";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import { DesktopDateTimePicker } from "@mui/x-date-pickers/DesktopDateTimePicker";
import RootPage from "../root";
import "./create.css"; // Import CSS file for additional styling
import { backend_post } from "../../utils";
import { useParams } from "react-router-dom";
const CreateLecture = () => {
// const navigate = useNavigate();
const [regStatus, setRegStatus] = useState("");
const { id } = useParams();
const handleResponse = (data: any) => {
console.log(data);
if ("ok" in data) {
setRegStatus("success");
} else {
setRegStatus("failed");
}
};
// TODO: Add first and last name validation
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
setRegStatus("submitting");
event.preventDefault();
const data = new FormData(event.currentTarget);
const start_time = data.get("start_time");
const end_time = data.get("end_time");
if (start_time == null || end_time == null) return;
if (start_time == "" || end_time == "") return;
const start_date = new Date(Date.parse(start_time.toString()));
const end_date = new Date(Date.parse(end_time.toString()));
backend_post(
"course/lecture/" + id + "/add",
JSON.stringify({
start_time: start_date.toISOString(),
end_time: end_date.toISOString(),
lecture_type: data.get("lecture_type")
})
)
.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 Lecture
</Typography>
<Box
component="form"
onSubmit={handleSubmit}
noValidate
sx={{ mt: 1 }}
color={"primary"}
>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DesktopDateTimePicker
label="Start time"
name="start_time"
slotProps={{ textField: { required: true } }}
/>
<DesktopDateTimePicker
label="End time"
name="end_time"
slotProps={{ textField: { required: true } }}
/>
</LocalizationProvider>
<Select
color="primary"
required
fullWidth
id="lecture_type"
name="lecture_type"
autoComplete="lecture_type"
autoFocus
// label="Lecture Type"
defaultValue="lecture"
>
<MenuItem value="lecture">Lecture</MenuItem>
<MenuItem value="practical">Practical</MenuItem>
<MenuItem value="workshop">Workshop</MenuItem>
<MenuItem value="exam">Exam</MenuItem>
</Select>
{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 CreateLecture;