diff --git a/src/index.css b/src/index.css index ef2bee8..1914d4f 100644 --- a/src/index.css +++ b/src/index.css @@ -128,6 +128,13 @@ table input{ padding: 0 5px; } +.blog-post-content{ + display: flex; + flex-direction: column; + gap: 1rem; + width: 100%; +} + [aria-disabled="true"]{ color: grey; cursor: not-allowed; diff --git a/src/routes/BlogPostView.tsx b/src/routes/BlogPostView.tsx index 4894133..abb74c7 100644 --- a/src/routes/BlogPostView.tsx +++ b/src/routes/BlogPostView.tsx @@ -2,7 +2,7 @@ import { useLoaderData, useNavigate } from "react-router-dom"; import getBlogPosts from "../utils/blogs/getBlogPosts"; import { BlogPost } from "../types"; import Markdown from "markdown-to-jsx"; -import { Box, Button, Sheet, Stack, Typography } from "@mui/joy"; +import { Box, Button, Sheet, Stack, Typography, Radio, RadioGroup, Checkbox } from "@mui/joy"; import { useCallback, useEffect, useState } from "react"; import { getOrderedBlogPosts } from "../utils/blogs/getOrderedBlogPosts"; @@ -19,6 +19,10 @@ export default function BlogPostView() { const [currCategoryBlogs, setCurrCategoryBlogs] = useState([]); const [currIndex, setCurrIndex] = useState(-1); const navigate = useNavigate(); + const [selectedAnswers, setSelectedAnswers] = useState>({}); + const [answeredQuestions, setAnsweredQuestions] = useState>({}); + const questions = currBlog.questions ?? []; + useEffect(() => { (async () => { @@ -106,10 +110,68 @@ export default function BlogPostView() { {!!currBlog.meta.author && Authored by {currBlog.meta.author}} {!!currBlog.meta.editor && Edited by {currBlog.meta.editor}} - + {currBlog.blog_text} + {questions.length > 0 && ( + + Quiz + {/* loop through each questions*/} + {questions.map((question, questionIndex) => { + const correctAnswers = Array.isArray(question.correct) ? question.correct : [question.correct]; + const isMultiSelect = correctAnswers.length > 1; + //Get the user’s selected answer for a question or default to empty value depending on question type + const selectedAnswer = selectedAnswers[questionIndex] ?? (isMultiSelect ? [] : ""); + const selectedAnswerList = Array.isArray(selectedAnswer) ? selectedAnswer : [selectedAnswer]; + const hasSelection = Boolean(answeredQuestions[questionIndex]); + //check if answer is correct and use radio or checkbox depending on how many answers + const isCorrect = isMultiSelect + ? selectedAnswerList.length === correctAnswers.length && selectedAnswerList.every((answer) => correctAnswers.includes(answer)) + : typeof selectedAnswer === "string" && selectedAnswer === question.correct; + + return ( + + {question.question} + {question.code && ( + +
{question.code}
+
+ )} + {/* multi-select questions (checkbox)*/} + {isMultiSelect ? ( + + {question.options.map((option) => ( + { + const nextSelected = event.target.checked ? [...selectedAnswerList, option] : selectedAnswerList.filter((answer) => answer !== option); + setSelectedAnswers({...selectedAnswers, [questionIndex]: nextSelected}); + setAnsweredQuestions({...answeredQuestions, [questionIndex]: nextSelected.length > 0}); + }} + />))} + ) : ( + // single-select questions (radio) + { + setSelectedAnswers({...selectedAnswers, [questionIndex]: e.target.value}); + setAnsweredQuestions({...answeredQuestions, [questionIndex]: true}); + } + }> + {question.options.map((opt) => ( ))} + + )} + {/* Feedback after user answer*/} + {hasSelection && ( + + {isCorrect ? "Correct" : "Incorrect"} + + )} + {hasSelection && isCorrect && question.explanation && ( {question.explanation} + )} +
+ ); + })} +
+ )}
diff --git a/src/types.ts b/src/types.ts index 4bc8a3b..5bf7751 100644 --- a/src/types.ts +++ b/src/types.ts @@ -182,6 +182,13 @@ export interface BlogPost { category: string; order?: number; }; + questions?: { + question: string; + code?: string; + options: string[]; + correct: string | string[]; + explanation?: string; + }[]; } export interface ProblemLog {