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

Only shows create course button when you have privilege to see it #353

Merged
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
3 changes: 1 addition & 2 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 48 additions & 22 deletions frontend/src/components/Courses/AllCoursesTeacher.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
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, ProjectDetail} from "./CourseUtils";
import { Course, callToApiToCreateCourse, ProjectDetail } 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 loader = useLoaderData() as {
const { courses, projects, me } = useLoaderData() as {
courses: Course[];
me: Me;
projects: { [courseId: string]: ProjectDetail[] };
};
const courses = loader.courses;
const projects = loader.projects;

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 @@ -36,14 +48,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 @@ -52,33 +64,47 @@ export function AllCoursesTeacher(): JSX.Element {
};
return (
<>
<Title title={t('title')}></Title>
<Grid container direction={'column'} style={{marginTop: '1rem', width:'100vw', height: '80vh'}}>
<SideScrollableCourses courses={courses} projects={projects}></SideScrollableCourses>
<Title title={t("title")}></Title>
<Grid
container
direction={"column"}
style={{ marginTop: "1rem", width: "100vw", height: "80vh" }}
>
<SideScrollableCourses
courses={courses}
projects={projects}
></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>
</>
);
}
}
4 changes: 3 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 @@ -125,11 +126,12 @@ export const dataLoaderCourses = async () => {

const courses = await fetchData(`courses`);
const projects = await fetchProjectsCourse(courses);
const me = await fetchMe();
for( const c of courses){
const teacher = await fetchData(`users/${c.teacher}`)
c.teacher = teacher.display_name
}
return {courses, projects}
return {courses, projects, me}
};

/**
Expand Down