+
+
+
+
+
+ {!isVerifying && correctAnswer && (
+
+ )}
+
+
+
+ );
+}
diff --git a/src/components/AIQuiz/AIQuiz.tsx b/src/components/AIQuiz/AIQuiz.tsx
index a663e03e9..1d0f18ea5 100644
--- a/src/components/AIQuiz/AIQuiz.tsx
+++ b/src/components/AIQuiz/AIQuiz.tsx
@@ -118,6 +118,7 @@ export function AIQuiz(props: AIQuizProps) {
{quizSlug && !aiQuizError && (
diff --git a/src/components/AIQuiz/AIQuizContent.tsx b/src/components/AIQuiz/AIQuizContent.tsx
index 48020b1cc..ada4414e9 100644
--- a/src/components/AIQuiz/AIQuizContent.tsx
+++ b/src/components/AIQuiz/AIQuizContent.tsx
@@ -6,43 +6,98 @@ import {
type LucideIcon,
} from 'lucide-react';
import { AIMCQQuestion } from './AIMCQQuestion';
+import { AIOpenEndedQuestion } from './AIOpenEndedQuestion';
+
+export type QuestionState = {
+ isSubmitted: boolean;
+ selectedOptions?: number[];
+ userAnswer?: string;
+ correctAnswer?: string;
+ status: 'correct' | 'incorrect' | 'skipped' | 'pending';
+};
+
+const DEFAULT_QUESTION_STATE: QuestionState = {
+ isSubmitted: false,
+ selectedOptions: [],
+ userAnswer: '',
+ correctAnswer: '',
+ status: 'pending',
+};
type AIQuizContentProps = {
+ quizSlug?: string;
questions: QuizQuestion[];
isLoading?: boolean;
};
export function AIQuizContent(props: AIQuizContentProps) {
- const { questions, isLoading } = props;
+ const { quizSlug, questions, isLoading } = props;
const [activeQuestionIndex, setActiveQuestionIndex] = useState(0);
const activeQuestion = questions[activeQuestionIndex];
const [selectedOptions, setSelectedOptions] = useState<
- Record<
- number,
- {
- selectedOptions: number[];
- isSubmitted: boolean;
- }
- >
+ Record
>({});
const hasMoreQuestions = activeQuestionIndex < questions.length - 1;
const hasPreviousQuestions = activeQuestionIndex > 0;
- const activeQuestionSelectedOptions =
- selectedOptions[activeQuestionIndex]?.selectedOptions ?? [];
- const activeQuestionIsSubmitted =
- selectedOptions[activeQuestionIndex]?.isSubmitted ?? false;
+ const activeQuestionState =
+ selectedOptions[activeQuestionIndex] ?? DEFAULT_QUESTION_STATE;
- const handleSubmit = (questionIndex: number) => {
+ const activeQuestionSelectedOptions =
+ activeQuestionState.selectedOptions ?? [];
+ const activeQuestionIsSubmitted = activeQuestionState.isSubmitted ?? false;
+
+ const handleSubmit = (
+ questionIndex: number,
+ status: QuestionState['status'],
+ ) => {
setSelectedOptions((prev) => {
+ const oldState = prev[questionIndex] ?? DEFAULT_QUESTION_STATE;
+
const newSelectedOptions = {
...prev,
[questionIndex]: {
- selectedOptions: prev[questionIndex].selectedOptions,
+ ...oldState,
isSubmitted: true,
+ status,
+ },
+ };
+
+ return newSelectedOptions;
+ });
+ };
+
+ const handleSetUserAnswer = (questionIndex: number, userAnswer: string) => {
+ setSelectedOptions((prev) => {
+ const oldState = prev[questionIndex] ?? DEFAULT_QUESTION_STATE;
+
+ const newSelectedOptions = {
+ ...prev,
+ [questionIndex]: {
+ ...oldState,
+ userAnswer,
+ },
+ };
+
+ return newSelectedOptions;
+ });
+ };
+
+ const handleSetCorrectAnswer = (
+ questionIndex: number,
+ correctAnswer: string,
+ ) => {
+ setSelectedOptions((prev) => {
+ const oldState = prev[questionIndex] ?? DEFAULT_QUESTION_STATE;
+
+ const newSelectedOptions = {
+ ...prev,
+ [questionIndex]: {
+ ...oldState,
+ correctAnswer,
},
};
@@ -52,9 +107,15 @@ export function AIQuizContent(props: AIQuizContentProps) {
const handleSelectOptions = (questionIndex: number, options: number[]) => {
setSelectedOptions((prev) => {
+ const oldState = prev[questionIndex] ?? DEFAULT_QUESTION_STATE;
+
const newSelectedOptions = {
...prev,
- [questionIndex]: { selectedOptions: options, isSubmitted: false },
+ [questionIndex]: {
+ ...oldState,
+ selectedOptions: options,
+ isSubmitted: true,
+ },
};
return newSelectedOptions;
@@ -72,7 +133,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
Question {activeQuestionIndex + 1} of {questions.length}
-
+
handleSelectOptions(activeQuestionIndex, options)
}
- onSubmit={() => handleSubmit(activeQuestionIndex)}
+ onSubmit={(status) => handleSubmit(activeQuestionIndex, status)}
onNext={() => setActiveQuestionIndex(activeQuestionIndex + 1)}
/>
)}
+
+ {activeQuestion && activeQuestion.type === 'open-ended' && (
+
handleSubmit(activeQuestionIndex, status)}
+ onNext={() => setActiveQuestionIndex(activeQuestionIndex + 1)}
+ userAnswer={activeQuestionState.userAnswer ?? ''}
+ setUserAnswer={(userAnswer) =>
+ handleSetUserAnswer(activeQuestionIndex, userAnswer)
+ }
+ correctAnswer={activeQuestionState.correctAnswer ?? ''}
+ setCorrectAnswer={(correctAnswer) =>
+ handleSetCorrectAnswer(activeQuestionIndex, correctAnswer)
+ }
+ status={activeQuestionState.status}
+ />
+ )}
);
}
diff --git a/src/components/AIQuiz/GenerateAIQuiz.tsx b/src/components/AIQuiz/GenerateAIQuiz.tsx
index f955880dc..cc455c005 100644
--- a/src/components/AIQuiz/GenerateAIQuiz.tsx
+++ b/src/components/AIQuiz/GenerateAIQuiz.tsx
@@ -113,5 +113,5 @@ export function GenerateAIQuiz(props: GenerateAIQuizProps) {
);
}
- return
;
+ return
;
}