Skip to content

Commit

Permalink
Added frontend exam & questions fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
vilnor committed Apr 25, 2024
1 parent 7c30c70 commit 169b9f1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
17 changes: 17 additions & 0 deletions frontend/src/api/useExam.ts
Original file line number Diff line number Diff line change
@@ -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<Exam, string> = (...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
}
}
17 changes: 17 additions & 0 deletions frontend/src/api/useQuestions.ts
Original file line number Diff line number Diff line change
@@ -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<Question[], string> = (...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
}
}
34 changes: 21 additions & 13 deletions frontend/src/app/courses/[courseCode]/exams/[examId]/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<main>
<h1>{`${params.courseCode} ${exam.examtype}`}</h1>
<h2>{`${exam.examyear} Semester ${exam.examsemester}`}</h2>
There will be some questions here
<h1>{`${params.courseCode} ${exam?.examtype}`}</h1>
<h2>{`${exam?.examyear} Semester ${exam?.examsemester}`}</h2>
{isLoadingQuestions && <p>Loading...</p>}
{!isErrorQuestions && !isLoadingQuestions && questions?.map(question => (
<>
<hr />
<div key={question.questionid}>
<Link href={`/courses/${params.courseCode}/exams/${question.questionid}`}>
<h2>{question.questiontype}</h2>
</Link>
<p>{question.questiontext}</p>
</div>
</>
))
}
</main>
);
}
7 changes: 7 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ export type Exam = {
examyear: number
examsemester: number
examtype: string
}

export type Question = {
questionid: number
questiontext: string
questiontype: string
questionpng: string
}

0 comments on commit 169b9f1

Please sign in to comment.