Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main to side #33

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 60 additions & 32 deletions src/pages/course/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,13 @@ import { useState, useEffect } from "react";
import RootPage from "../root";
import Container from "@mui/material/Container";
import Typography from "@mui/material/Typography";
import Cookies from "js-cookie";
import "./course.css"; // Import CSS file for additional styling

import {
IsStudent,
IsAdmin,
backend_get,
deleteAuthCookies,
useAxiosRequest,
backend_post
} from "../../utils";
import { IsStudent, IsAdmin, useAxiosRequest, backend_post } from "../../utils";
import { useNavigate, useParams } from "react-router-dom";
import { JwtPayload, jwtDecode } from "jwt-decode";
import {
CookieJWT,
Empty,
FullCourse,
FullCourseUser
} from "../../types/common";
import { Empty, FullCourse, FullCourseUser } from "../../types/common";
import { Avatar, Button, IconButton, capitalize, styled } from "@mui/material";
import MoreVertIcon from "@mui/icons-material/MoreVert";
import { render } from "@testing-library/react";
import CancelIcon from "@mui/icons-material/Cancel";

const Course = () => {
const { response, error, loading, sendRequest } = useAxiosRequest<
Expand Down Expand Up @@ -99,9 +84,10 @@ const Course = () => {
);
};

const handleEnrollment = (enroll: boolean) => {
const handleEnrollment = (enroll: boolean, username: string = "") => {
let url = "course/enroll/" + id;
if (!enroll) url = "course/disenroll/" + id;
if (username != "") url = `${url}/${username}`;
backend_post(url, "", true).then((resp) => {
if (resp.status == 200) {
getCourseData();
Expand Down Expand Up @@ -151,6 +137,32 @@ const Course = () => {
marginRight: "5%"
}}
>
<Button
variant="contained"
style={{
marginBottom: "5%",
textTransform: "none"
}}
onClick={() => navigate(`/course/${id}/schedule`)}
>
Schedule
</Button>

{!IsStudent() ? (
<Button
variant="contained"
style={{
marginBottom: "5%",
textTransform: "none"
}}
onClick={() =>
navigate(`/course/${id}/create_lecture`)
}
>
Create Lecture
</Button>
) : null}

{!IsAdmin() ? (
courseData?.enrolled == true ? (
<Button
Expand All @@ -159,7 +171,6 @@ const Course = () => {
handleEnrollment(false);
}}
style={{
marginBottom: "5%",
textTransform: "none"
}}
sx={{
Expand All @@ -177,22 +188,13 @@ const Course = () => {
handleEnrollment(true);
}}
style={{
marginBottom: "5%",
textTransform: "none"
}}
>
Enroll
</Button>
)
) : null}

<Button
variant="contained"
style={{ textTransform: "none" }}
onClick={() => navigate(`/course/${id}/schedule`)}
>
Schedule
</Button>
</div>
</div>

Expand All @@ -208,7 +210,9 @@ const Course = () => {
<th>Name</th>
<th className="type-column">Role</th>
{IsAdmin() ? (
<th className="actions-column">Actions</th>
<th className="actions-column">
Disenroll
</th>
) : null}
</tr>
</thead>
Expand All @@ -235,6 +239,15 @@ const Course = () => {
textTransform: "none",
fontSize: "1em"
}}
sx={{
"&.MuiButtonBase-root:hover":
{
bgcolor:
"transparent",
textDecoration:
"underline"
}
}}
onClick={() =>
handleProfileClick(
user.username
Expand All @@ -249,8 +262,23 @@ const Course = () => {
</td>
{IsAdmin() ? (
<td className="actions-icon">
<IconButton>
<MoreVertIcon />
<IconButton
onClick={() => {
handleEnrollment(
false,
user.username
);
}}
sx={{
"&.MuiButtonBase-root:hover":
{
bgcolor:
"transparent",
color: "red"
}
}}
>
<CancelIcon />
</IconButton>
</td>
) : null}
Expand Down
69 changes: 44 additions & 25 deletions src/pages/courses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ 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_post, useAxiosRequest } from "../../utils";
import { backend_delete, backend_post, useAxiosRequest } from "../../utils";
import { Course, Empty } from "../../types/common";
import { IsAdmin } from "../../utils";
import { User } from "../../types/common";
import {
Button,
Checkbox,
Icon,
IconButton,
Typography,
styled
} from "@mui/material";
import MoreVertIcon from "@mui/icons-material/MoreVert";
import AddCircleOutlineIcon from "@mui/icons-material/AddCircleOutline";
import Avatar from "@mui/material/Avatar";
import CheckCircleIcon from "@mui/icons-material/CheckCircle";
import CancelIcon from "@mui/icons-material/Cancel";

type ResponseData = Course[];

Expand All @@ -39,6 +39,7 @@ const Courses = () => {
}, [sendRequest]);

useEffect(() => {
console.log(response);
if (response) setCourseData(response);
}, [response]);

Expand Down Expand Up @@ -92,20 +93,21 @@ const Courses = () => {
navigate(`/course/${course_id}`);
};

const handleEnrollment =
(courseId: number, enroll: boolean) =>
(event: React.ChangeEvent<HTMLInputElement>) => {
if (enroll) {
backend_post("course/enroll/" + courseId, "", true);
if (response == null) return;
const new_data = course_data?.map((course: Course) =>
course.id == courseId
? { ...course, enrolled: true }
: course
);
setCourseData(new_data);
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>
Expand All @@ -131,10 +133,11 @@ const Courses = () => {
<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">Enroll</th>
<th className="enroll-column">Enrolled</th>
)}
</tr>
</thead>
Expand All @@ -153,28 +156,44 @@ const Courses = () => {
textTransform: "none",
fontSize: "1em"
}}
sx={{
"&.MuiButtonBase-root:hover": {
bgcolor: "transparent",
textDecoration: "underline"
}
}}
onClick={() =>
handleCourseClick(course.id)
}
>
{`${course.course_name}`}
</Button>
</td>
<td></td>
<td>{course.num_students}</td>
<td>{course.num_teachers}</td>
{IsAdmin() ? (
<td className="actions-icon">
<IconButton>
<MoreVertIcon />
<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}
onChange={handleEnrollment(
course.id,
!course.enrolled
)}
disabled={true}
sx={{
"& .MuiSvgIcon-root": {
color: "white"
Expand Down
9 changes: 3 additions & 6 deletions src/pages/create_course/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 { Box } from "@mui/material";
import AppRegistrationIcon from "@mui/icons-material/AppRegistration";

import RootPage from "../root";
Expand All @@ -17,10 +17,7 @@ const CreateCourse = () => {
// const navigate = useNavigate();
const [regStatus, setRegStatus] = useState("");

const handleResponse = (
data: any,
event: React.FormEvent<HTMLFormElement>
) => {
const handleResponse = (data: any) => {
console.log(data);
if (
!("course_name" in data) ||
Expand All @@ -46,7 +43,7 @@ const CreateCourse = () => {
})
)
.then((resp) => resp.json())
.then((data) => handleResponse(data, event))
.then((data) => handleResponse(data))
.catch((error) => console.log(error));
};

Expand Down
8 changes: 2 additions & 6 deletions src/pages/create_lecture/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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";
Expand All @@ -21,10 +20,7 @@ const CreateLecture = () => {
const [regStatus, setRegStatus] = useState("");
const { id } = useParams();

const handleResponse = (
data: any,
event: React.FormEvent<HTMLFormElement>
) => {
const handleResponse = (data: any) => {
console.log(data);
if ("ok" in data) {
setRegStatus("success");
Expand Down Expand Up @@ -56,7 +52,7 @@ const CreateLecture = () => {
})
)
.then((resp) => resp.json())
.then((data) => handleResponse(data, event))
.then((data) => handleResponse(data))
.catch((error) => console.log(error));
};

Expand Down
7 changes: 2 additions & 5 deletions src/pages/create_user/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ const CreateUser = () => {
// const navigate = useNavigate();
const [regStatus, setRegStatus] = useState("");

const handleTokenResponse = (
data: any,
event: React.FormEvent<HTMLFormElement>
) => {
const handleTokenResponse = (data: any) => {
console.log(data);
if (!("username" in data) || typeof data["username"] !== "string") {
setRegStatus("failed");
Expand Down Expand Up @@ -61,7 +58,7 @@ const CreateUser = () => {
})
)
.then((resp) => resp.json())
.then((data) => handleTokenResponse(data, event))
.then((data) => handleTokenResponse(data))
.catch((error) => console.log(error));
};

Expand Down
1 change: 0 additions & 1 deletion src/pages/home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FC } from "react";
import Rootpage from "../root";
import { Box, Typography } from "@mui/material";
import backgroundImage from "../../assets/logo-clear.png";

const HomePage: FC = () => (
<Rootpage>
Expand Down
Loading
Loading