diff --git a/frontend/src/api/useComments.ts b/frontend/src/api/useComments.ts new file mode 100644 index 0000000..73a0960 --- /dev/null +++ b/frontend/src/api/useComments.ts @@ -0,0 +1,17 @@ +'use client' +import useSWR, { Fetcher } from 'swr'; +import { Comment as IComment }from '@/types'; + +const ENDPOINT = `${process.env.API_URL}/api/questions/`; + +const fetcher: Fetcher = (...args) => fetch(...args).then(res => res.json()) + +export default function useComments(questionId: number) { + const { data, error, isLoading } = useSWR(ENDPOINT + questionId +'/comments', fetcher) + + return { + comments: data, + isLoading, + isError: error + } +} \ No newline at end of file diff --git a/frontend/src/app/courses/[courseCode]/exams/[examId]/[questionId]/page.tsx b/frontend/src/app/courses/[courseCode]/exams/[examId]/[questionId]/page.tsx new file mode 100644 index 0000000..884af66 --- /dev/null +++ b/frontend/src/app/courses/[courseCode]/exams/[examId]/[questionId]/page.tsx @@ -0,0 +1,20 @@ +'use client'; +import useComments from '@/api/useComments'; +import Comment from '@/components/Comment'; + +export default function Question({ params }: { params: { courseCode: string, examId: number, questionId: number }}) { + const { comments, isLoading, isError } = useComments(params.questionId); + + return ( +
+ {isLoading &&

Loading...

} + {!isError && !isLoading && comments?.map(comment => ( + <> +
+ + + )) + } +
+ ); +} \ 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 9eff9d7..7ae0531 100644 --- a/frontend/src/app/courses/[courseCode]/exams/[examId]/page.tsx +++ b/frontend/src/app/courses/[courseCode]/exams/[examId]/page.tsx @@ -16,7 +16,7 @@ export default function Exam({ params }: { params: { courseCode: string, examId: <>
- +

{question.questionType}

{question.questionText}

diff --git a/frontend/src/components/Comment.tsx b/frontend/src/components/Comment.tsx new file mode 100644 index 0000000..64a9794 --- /dev/null +++ b/frontend/src/components/Comment.tsx @@ -0,0 +1,11 @@ +import { Comment as IComment } from '@/types'; + +export default function Comment({ comment }: { comment: IComment }) { + return ( +
+

(id: {comment.commentId}) | {comment.commentText}

+

reply

+ {comment.children?.map((c) => )} +
+ ) +} \ No newline at end of file diff --git a/frontend/src/types.ts b/frontend/src/types.ts index 25d74ef..69d101e 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -16,4 +16,18 @@ export type Question = { questionText: string questionType: string questionPng: string +} + +export type Comment = { + commentId: number + parentCommentId: number | null + commentText: string + commentPNG: string | null + isCorrect: boolean + isEndorsed: boolean + upvotes: number + downvotes: number + created_at: string + updated_at: string + children?: Comment[] } \ No newline at end of file