From 169b9f12df37aa6a7cd41b9fa090cc50d69fa8cc Mon Sep 17 00:00:00 2001 From: liv Date: Wed, 24 Apr 2024 23:52:29 +1000 Subject: [PATCH] Added frontend exam & questions fetching --- frontend/src/api/useExam.ts | 17 ++++++++++ frontend/src/api/useQuestions.ts | 17 ++++++++++ .../[courseCode]/exams/[examId]/page.tsx | 34 ++++++++++++------- frontend/src/types.ts | 7 ++++ 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 frontend/src/api/useExam.ts create mode 100644 frontend/src/api/useQuestions.ts diff --git a/frontend/src/api/useExam.ts b/frontend/src/api/useExam.ts new file mode 100644 index 0000000..0154f66 --- /dev/null +++ b/frontend/src/api/useExam.ts @@ -0,0 +1,17 @@ +'use client' +import useSWR, { Fetcher } from 'swr'; +import { Exam } from '@/types'; + +const ENDPOINT = `${process.env.API_URL}/api/exams/`; + +const fetcher: Fetcher = (...args) => fetch(...args).then(res => res.json()) + +export default function useExam(examId: number) { + const { data, error, isLoading } = useSWR(ENDPOINT + examId, fetcher) + + return { + exam: data, + isLoading, + isError: error + } +} \ No newline at end of file diff --git a/frontend/src/api/useQuestions.ts b/frontend/src/api/useQuestions.ts new file mode 100644 index 0000000..f7e81fe --- /dev/null +++ b/frontend/src/api/useQuestions.ts @@ -0,0 +1,17 @@ +'use client' +import useSWR, { Fetcher } from 'swr'; +import { Question } from '@/types'; + +const ENDPOINT = `${process.env.API_URL}/api/exams/`; + +const fetcher: Fetcher = (...args) => fetch(...args).then(res => res.json()) + +export default function useQuestions(examId: number) { + const { data, error, isLoading } = useSWR(ENDPOINT + examId +'/questions', fetcher) + + return { + questions: data, + isLoading, + isError: error + } +} \ No newline at end of file diff --git a/frontend/src/app/courses/[courseCode]/exams/[examId]/page.tsx b/frontend/src/app/courses/[courseCode]/exams/[examId]/page.tsx index 5e3d614..dc69cbc 100644 --- a/frontend/src/app/courses/[courseCode]/exams/[examId]/page.tsx +++ b/frontend/src/app/courses/[courseCode]/exams/[examId]/page.tsx @@ -1,21 +1,29 @@ -import { useMemo } from 'react'; +'use client' +import useExam from '@/api/useExam'; +import useQuestions from '@/api/useQuestions'; +import Link from 'next/link'; export default function Exam({ params }: { params: { courseCode: string, examId: number }}) { - const exam = useMemo(() => { - // query backend here - return { - examid: params.examId, - examyear: 2023, - examsemester: 1, - examtype: 'Final' - } - }, [params.examId]) + const { exam, isLoading, isError } = useExam(params.examId); + const { questions, isLoading: isLoadingQuestions, isError: isErrorQuestions } = useQuestions(params.examId); return (
-

{`${params.courseCode} ${exam.examtype}`}

-

{`${exam.examyear} Semester ${exam.examsemester}`}

- There will be some questions here +

{`${params.courseCode} ${exam?.examtype}`}

+

{`${exam?.examyear} Semester ${exam?.examsemester}`}

+ {isLoadingQuestions &&

Loading...

} + {!isErrorQuestions && !isLoadingQuestions && questions?.map(question => ( + <> +
+
+ +

{question.questiontype}

+ +

{question.questiontext}

+
+ + )) + }
); } \ No newline at end of file diff --git a/frontend/src/types.ts b/frontend/src/types.ts index eeeba62..a9d181e 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -9,4 +9,11 @@ export type Exam = { examyear: number examsemester: number examtype: string +} + +export type Question = { + questionid: number + questiontext: string + questiontype: string + questionpng: string } \ No newline at end of file