diff --git a/src/components/AIQuiz/AIMCQQuestion.tsx b/src/components/AIQuiz/AIMCQQuestion.tsx index 41a83fdf4..2e1f34cb0 100644 --- a/src/components/AIQuiz/AIMCQQuestion.tsx +++ b/src/components/AIQuiz/AIMCQQuestion.tsx @@ -1,56 +1,69 @@ import type { QuizQuestion } from '../../queries/ai-quiz'; -import { useState } from 'react'; import { cn } from '../../lib/classname'; import { CheckIcon, XIcon, InfoIcon } from 'lucide-react'; import { markdownToHtml } from '../../lib/markdown'; type AIMCQQuestionProps = { question: QuizQuestion; - onNextQuestion?: () => void; + selectedOptions: number[]; + setSelectedOptions: (options: number[]) => void; + isSubmitted: boolean; + onSubmit: () => void; + onNext: () => void; }; export function AIMCQQuestion(props: AIMCQQuestionProps) { - const { question, onNextQuestion } = props; + const { + question, + selectedOptions, + setSelectedOptions, + isSubmitted, + onSubmit, + onNext, + } = props; const { title: questionText, options, answerExplanation } = question; - const [selectedOptions, setSelectedOptions] = useState([]); - const [isSubmitted, setIsSubmitted] = useState(false); - const canSubmitMultipleAnswers = options.filter((option) => option.isCorrect).length > 1; const handleSelectOption = (index: number) => { - if (!canSubmitMultipleAnswers) { - setSelectedOptions((prev) => (prev.includes(index) ? [] : [index])); + if (isSubmitted) { return; } - setSelectedOptions((prev) => { - if (prev.includes(index)) { - return prev.filter((id) => id !== index); - } - return [...prev, index]; - }); + if (!canSubmitMultipleAnswers) { + const newSelectedOptions = selectedOptions.includes(index) + ? selectedOptions.filter((id) => id !== index) + : [...selectedOptions, index]; + setSelectedOptions(newSelectedOptions); + return; + } + + const newSelectedOptions = selectedOptions.includes(index) + ? selectedOptions.filter((id) => id !== index) + : [...selectedOptions, index]; + setSelectedOptions(newSelectedOptions); }; const handleSubmit = () => { if (isSubmitted) { - onNextQuestion?.(); - setSelectedOptions([]); - setIsSubmitted(false); + onNext?.(); return; } - setIsSubmitted(true); + onSubmit(); }; const canSubmit = selectedOptions.length > 0; const titleHtml = markdownToHtml(questionText, false); + const markdownClassName = + 'prose prose-lg prose-p:text-lg prose-p:font-normal prose-p:my-0 prose-pre:my-0 prose-p:prose-code:text-base! prose-p:prose-code:px-2 prose-p:prose-code:py-0.5 prose-p:prose-code:rounded-lg prose-p:prose-code:border prose-p:prose-code:border-black text-left text-black'; + return (
@@ -68,11 +81,14 @@ export function AIMCQQuestion(props: AIMCQQuestionProps) { const html = markdownToHtml(option.title, false); + const isOptionDisabled = + isSubmitted && !isSelected && !isCorrectOption; + return ( @@ -117,13 +136,18 @@ export function AIMCQQuestion(props: AIMCQQuestionProps) { })}
- {isSubmitted && ( + {isSubmitted && answerExplanation && (

Explanation

-

{answerExplanation}

+

)} diff --git a/src/components/AIQuiz/AIQuizContent.tsx b/src/components/AIQuiz/AIQuizContent.tsx index 28088b0cb..48020b1cc 100644 --- a/src/components/AIQuiz/AIQuizContent.tsx +++ b/src/components/AIQuiz/AIQuizContent.tsx @@ -18,24 +18,74 @@ export function AIQuizContent(props: AIQuizContentProps) { const [activeQuestionIndex, setActiveQuestionIndex] = useState(0); const activeQuestion = questions[activeQuestionIndex]; + const [selectedOptions, setSelectedOptions] = useState< + Record< + number, + { + selectedOptions: number[]; + isSubmitted: boolean; + } + > + >({}); + const hasMoreQuestions = activeQuestionIndex < questions.length - 1; const hasPreviousQuestions = activeQuestionIndex > 0; - console.log('-'.repeat(20)); - console.log(questions); - console.log('-'.repeat(20)); + const activeQuestionSelectedOptions = + selectedOptions[activeQuestionIndex]?.selectedOptions ?? []; + const activeQuestionIsSubmitted = + selectedOptions[activeQuestionIndex]?.isSubmitted ?? false; + + const handleSubmit = (questionIndex: number) => { + setSelectedOptions((prev) => { + const newSelectedOptions = { + ...prev, + [questionIndex]: { + selectedOptions: prev[questionIndex].selectedOptions, + isSubmitted: true, + }, + }; + + return newSelectedOptions; + }); + }; + + const handleSelectOptions = (questionIndex: number, options: number[]) => { + setSelectedOptions((prev) => { + const newSelectedOptions = { + ...prev, + [questionIndex]: { selectedOptions: options, isSubmitted: false }, + }; + + return newSelectedOptions; + }); + }; + + const progressPercentage = isLoading + ? 0 + : Math.min(((activeQuestionIndex + 1) / questions.length) * 100, 100); return (
-
+
+ + Question {activeQuestionIndex + 1} of {questions.length} + + +
+
+
+ setActiveQuestionIndex(activeQuestionIndex - 1)} icon={ChevronLeftIcon} /> - - Question {activeQuestionIndex + 1} of {questions.length} - setActiveQuestionIndex(activeQuestionIndex + 1)} @@ -46,7 +96,13 @@ export function AIQuizContent(props: AIQuizContentProps) { {activeQuestion && activeQuestion.type === 'mcq' && ( setActiveQuestionIndex(activeQuestionIndex + 1)} + selectedOptions={activeQuestionSelectedOptions} + isSubmitted={activeQuestionIsSubmitted} + setSelectedOptions={(options) => + handleSelectOptions(activeQuestionIndex, options) + } + onSubmit={() => handleSubmit(activeQuestionIndex)} + onNext={() => setActiveQuestionIndex(activeQuestionIndex + 1)} /> )}