From 0a4828429596253375ee75bd435f933b0dcdc131 Mon Sep 17 00:00:00 2001 From: Vy San Date: Wed, 27 May 2026 12:06:01 -0400 Subject: [PATCH 1/3] Add questionaire --- src/index.css | 7 +++++ src/routes/BlogPostView.tsx | 57 +++++++++++++++++++++++++++++++++++-- src/types.ts | 7 +++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/index.css b/src/index.css index ef2bee8d..1914d4fa 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 48941337..c293426f 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,59 @@ 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 + {questions.map((question, questionIndex) => { + const correctAnswers = Array.isArray(question.correct) ? question.correct : [question.correct]; + const isMultiSelect = correctAnswers.length > 1; + const selectedAnswer = selectedAnswers[questionIndex] ?? (isMultiSelect ? [] : ""); + const selectedAnswerList = Array.isArray(selectedAnswer) ? selectedAnswer : [selectedAnswer]; + const hasSelection = Boolean(answeredQuestions[questionIndex]); + 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}
+
+ )} + {isMultiSelect ? ( + + {question.options.map((opt) => ( + { + const nextSelected = event.target.checked ? [...selectedAnswerList, opt] : selectedAnswerList.filter((answer) => answer !== opt); + setSelectedAnswers({...selectedAnswers, [questionIndex]: nextSelected}); + setAnsweredQuestions({...answeredQuestions, [questionIndex]: nextSelected.length > 0}); + }} + />))} + ) : ( + { + setSelectedAnswers({...selectedAnswers, [questionIndex]: e.target.value}); + setAnsweredQuestions({...answeredQuestions, [questionIndex]: true}); + } + }> + {question.options.map((opt) => ( ))} + + )} + {hasSelection && ( {isCorrect ? "Correct" : "Incorrect"} + )} + {hasSelection && isCorrect && question.explanation && ( {question.explanation} + )} +
+ ); + })} +
+ )}
diff --git a/src/types.ts b/src/types.ts index 4bc8a3b4..5bf7751d 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 { From 57f3580af20e838c75884f502c9d8b27e182e67e Mon Sep 17 00:00:00 2001 From: Vy San Date: Wed, 27 May 2026 12:32:19 -0400 Subject: [PATCH 2/3] MAke small changes and add comments --- src/routes/BlogPostView.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/routes/BlogPostView.tsx b/src/routes/BlogPostView.tsx index c293426f..a05db43e 100644 --- a/src/routes/BlogPostView.tsx +++ b/src/routes/BlogPostView.tsx @@ -117,12 +117,15 @@ export default function BlogPostView() { {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; @@ -135,16 +138,18 @@ export default function BlogPostView() {
{question.code}
)} + {/* multi-select questions (checkbox)*/} {isMultiSelect ? ( - {question.options.map((opt) => ( - { - const nextSelected = event.target.checked ? [...selectedAnswerList, opt] : selectedAnswerList.filter((answer) => answer !== opt); + {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}); @@ -154,6 +159,7 @@ export default function BlogPostView() { {question.options.map((opt) => ( ))} )} + {/* Feedback after user answer*/} {hasSelection && ( {isCorrect ? "Correct" : "Incorrect"} )} {hasSelection && isCorrect && question.explanation && ( {question.explanation} From af58468c92bdba58b1eef638417a0969cc89cacd Mon Sep 17 00:00:00 2001 From: Vy San Date: Wed, 27 May 2026 13:48:30 -0400 Subject: [PATCH 3/3] Add color --- src/routes/BlogPostView.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/routes/BlogPostView.tsx b/src/routes/BlogPostView.tsx index a05db43e..abb74c73 100644 --- a/src/routes/BlogPostView.tsx +++ b/src/routes/BlogPostView.tsx @@ -160,7 +160,10 @@ export default function BlogPostView() { )} {/* Feedback after user answer*/} - {hasSelection && ( {isCorrect ? "Correct" : "Incorrect"} + {hasSelection && ( + + {isCorrect ? "Correct" : "Incorrect"} + )} {hasSelection && isCorrect && question.explanation && ( {question.explanation} )}