Skip to content

Commit

Permalink
Fix #265
Browse files Browse the repository at this point in the history
  • Loading branch information
AronBuzogany committed May 12, 2024
1 parent cbd7f83 commit 5a59a58
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
60 changes: 42 additions & 18 deletions frontend/src/components/Courses/AllCoursesTeacher.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { Button, Dialog, DialogActions, DialogTitle, FormControl, FormHelperText, Grid, Input, InputLabel } from "@mui/material";
import {
Button,
Dialog,
DialogActions,
DialogTitle,
FormControl,
FormHelperText,
Grid,
Input,
InputLabel,
} from "@mui/material";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { SideScrollableCourses } from "./CourseUtilComponents";
import { Course, callToApiToCreateCourse } from "./CourseUtils";
import { Title } from "../Header/Title";
import { useLoaderData } from "react-router-dom";
import { Me } from "../../types/me";

/**
* @returns A jsx component representing all courses for a teacher
*/
export function AllCoursesTeacher(): JSX.Element {
const [open, setOpen] = useState(false);
const courses = (useLoaderData() as Course[]);
const { courses, me } = useLoaderData() as { courses: Course[]; me: Me };

const [courseName, setCourseName] = useState('');
const [error, setError] = useState('');
const [courseName, setCourseName] = useState("");
const [error, setError] = useState("");

const navigate = useNavigate();

const { t } = useTranslation('translation', { keyPrefix: 'allCoursesTeacher' });
const { t } = useTranslation("translation", {
keyPrefix: "allCoursesTeacher",
});

const handleClickOpen = () => {
setOpen(true);
Expand All @@ -31,14 +44,14 @@ export function AllCoursesTeacher(): JSX.Element {

const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setCourseName(event.target.value);
setError(''); // Clearing error message when user starts typing
setError(""); // Clearing error message when user starts typing
};

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault(); // Prevents the default form submission behaviour

if (!courseName.trim()) {
setError(t('emptyCourseNameError'));
setError(t("emptyCourseNameError"));
return;
}

Expand All @@ -47,33 +60,44 @@ export function AllCoursesTeacher(): JSX.Element {
};
return (
<>
<Title title={t('title')}></Title>
<Grid container direction={'column'} style={{marginTop: '1rem', width:'100vw', height: '80vh'}}>
<Title title={t("title")}></Title>
<Grid
container
direction={"column"}
style={{ marginTop: "1rem", width: "100vw", height: "80vh" }}
>
<SideScrollableCourses courses={courses}></SideScrollableCourses>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>{t('courseForm')}</DialogTitle>
<DialogTitle>{t("courseForm")}</DialogTitle>
<form style={{ margin: "2rem" }} onSubmit={handleSubmit}>
<FormControl>
<InputLabel htmlFor="course-name">{t('courseName')}</InputLabel>
<InputLabel htmlFor="course-name">{t("courseName")}</InputLabel>
<Input
id="course-name"
value={courseName}
onChange={handleInputChange}
error={!!error}
aria-describedby="my-helper-text"
/>
{error && <FormHelperText id="my-helper-text">{error}</FormHelperText>}
{error && (
<FormHelperText id="my-helper-text">{error}</FormHelperText>
)}
</FormControl>
<DialogActions>
<Button onClick={handleClose}>{t('cancel')}</Button>
<Button type="submit">{t('submit')}</Button>
<Button onClick={handleClose}>{t("cancel")}</Button>
<Button type="submit">{t("submit")}</Button>
</DialogActions>
</form>
</Dialog>
<Grid item style={{position: "absolute", left: "2rem", bottom: "5rem"}}>
<Button onClick={handleClickOpen} >{t('create')}</Button>
</Grid>
{me && me.role === "TEACHER" && (
<Grid
item
style={{ position: "absolute", left: "2rem", bottom: "5rem" }}
>
<Button onClick={handleClickOpen}>{t("create")}</Button>
</Grid>
)}
</Grid>
</>
);
}
}
3 changes: 2 additions & 1 deletion frontend/src/components/Courses/CourseUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NavigateFunction, Params } from "react-router-dom";
import { authenticatedFetch } from "../../utils/authenticated-fetch";
import { Me } from "../../types/me";
import { fetchMe } from "../../utils/fetches/FetchMe";

export interface Course {
course_id: string;
Expand Down Expand Up @@ -133,7 +134,7 @@ const fetchData = async (url: string, params?: URLSearchParams) => {

export const dataLoaderCourses = async () => {
//const params = new URLSearchParams({ 'teacher': loggedInUid() });
return fetchData(`courses`);
return {'courses': await fetchData(`courses`), 'me': await fetchMe()};
};

const dataLoaderCourse = async (courseId: string) => {
Expand Down

0 comments on commit 5a59a58

Please sign in to comment.