-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
213 lines (200 loc) · 8 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
import RootPage from "../root";
import Container from "@mui/material/Container";
import "./courses.css"; // Import CSS file for additional styling
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { backend_delete, backend_post, useAxiosRequest } from "../../utils";
import { Course, Empty } from "../../types/common";
import { IsAdmin } from "../../utils";
import {
Button,
Checkbox,
Icon,
IconButton,
Typography,
styled
} from "@mui/material";
import AddCircleOutlineIcon from "@mui/icons-material/AddCircleOutline";
import CheckCircleIcon from "@mui/icons-material/CheckCircle";
import CancelIcon from "@mui/icons-material/Cancel";
type ResponseData = Course[];
const Courses = () => {
const navigate = useNavigate();
const { response, error, loading, sendRequest } = useAxiosRequest<
Empty,
ResponseData
>();
const [course_data, setCourseData] = useState<Course[]>();
const alternatingColor = ["#424242", "#595959"];
useEffect(() => {
sendRequest({
method: "GET",
route: "/course/getall/",
useJWT: true
});
}, [sendRequest]);
useEffect(() => {
console.log(response);
if (response) setCourseData(response);
}, [response]);
if (error) {
console.error("Error fetching courses:", error);
// Optionally, render an error message in the UI
}
const StyledTable = styled("table")({
borderCollapse: "collapse",
width: "100%",
"& th, & tr": {
padding: "8px",
borderBottom: "1px solid #ddd",
textAlign: "left"
},
"& td": {
padding: "8px",
textAlign: "left"
},
"& th": {
fontWeight: "bold" // Add bold font weight to header cells if needed
},
"& .type-column": {
width: "10%", // Adjust the width of the actions column
textAlign: "left" // Align content to the left
},
"& .actions-column": {
width: "5%", // Adjust the width of the actions column
textAlign: "left" // Align content to the left
},
"& .enroll-column": {
width: "5%"
},
"& .avatar-column": {
width: "5%", // Adjust the width of the avatar column
textAlign: "left" // Align content to the left
},
"& .actions-icon": {
display: "flex",
justifyContent: "center",
alignItems: "center"
}
});
const handleNewCourseClick = () => {
navigate(`/create_course`);
};
const handleCourseClick = (course_id: number) => {
navigate(`/course/${course_id}`);
};
const deleteCourse = (course_id: number) => {
backend_delete("/course/delete/" + course_id, true).then((resp) => {
if (resp.ok) {
if (course_data == undefined) return;
let tempCourses: Course[] = [...course_data];
for (let i = 0; i < tempCourses.length; i++) {
if (tempCourses[i].id === course_id) {
tempCourses.splice(i, 1);
break;
}
}
setCourseData(tempCourses);
}
});
};
return (
<RootPage>
<Container component="main" className="mainComponent">
<Typography variant="h4" gutterBottom>
Courses
{IsAdmin() ? (
<IconButton
onClick={() => handleNewCourseClick()}
style={{
marginLeft: "2%",
color: "white",
backgroundColor: "rgba(255, 255, 255, 0.5)"
}}
>
<AddCircleOutlineIcon style={{ fontSize: "1em" }} />
</IconButton>
) : null}
</Typography>
<StyledTable>
<thead>
<tr>
<th>Course Name</th>
<th className="type-column">Students</th>
<th className="type-column">Teachers</th>
{IsAdmin() ? (
<th className="actions-column">Actions</th>
) : (
<th className="enroll-column">Enrolled</th>
)}
</tr>
</thead>
<tbody>
{course_data?.map((course: Course, index: number) => (
<tr
key={course.id}
style={{
backgroundColor: alternatingColor[index % 2]
}}
>
<td>
<Button
style={{
color: "white",
textTransform: "none",
fontSize: "1em"
}}
sx={{
"&.MuiButtonBase-root:hover": {
bgcolor: "transparent",
textDecoration: "underline"
}
}}
onClick={() =>
handleCourseClick(course.id)
}
>
{`${course.course_name}`}
</Button>
</td>
<td>{course.num_students}</td>
<td>{course.num_teachers}</td>
{IsAdmin() ? (
<td className="actions-icon">
<IconButton
onClick={() => {
deleteCourse(course.id);
}}
sx={{
"&.MuiButtonBase-root:hover": {
bgcolor: "transparent",
color: "red"
}
}}
>
<CancelIcon />
</IconButton>
</td>
) : (
<Checkbox
icon={<Icon />}
checkedIcon={<CheckCircleIcon />}
className="actions-icon"
checked={course.enrolled}
disabled={true}
sx={{
"& .MuiSvgIcon-root": {
color: "white"
}
}}
/>
)}
</tr>
))}
</tbody>
</StyledTable>
</Container>
</RootPage>
);
};
export default Courses;