1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-09-09 16:53:33 +02:00

fix: wait for to finish quiz

This commit is contained in:
Arik Chakma
2025-07-07 23:50:15 +06:00
parent b8c6800a92
commit bdad50666b
3 changed files with 45 additions and 10 deletions

View File

@@ -14,11 +14,20 @@ type AIMCQQuestionProps = {
setSelectedOptions: (options: number[]) => void; setSelectedOptions: (options: number[]) => void;
onSubmit: (status: QuestionState['status']) => void; onSubmit: (status: QuestionState['status']) => void;
onNext: () => void; onNext: () => void;
isLastQuestion: boolean;
onComplete: () => void;
}; };
export function AIMCQQuestion(props: AIMCQQuestionProps) { export function AIMCQQuestion(props: AIMCQQuestionProps) {
const { question, questionState, setSelectedOptions, onSubmit, onNext } = const {
props; question,
questionState,
setSelectedOptions,
onSubmit,
onNext,
isLastQuestion,
onComplete,
} = props;
const { title: questionText, options, answerExplanation } = question; const { title: questionText, options, answerExplanation } = question;
const { isSubmitted, selectedOptions = [] } = questionState; const { isSubmitted, selectedOptions = [] } = questionState;
@@ -45,7 +54,11 @@ export function AIMCQQuestion(props: AIMCQQuestionProps) {
const handleSubmit = () => { const handleSubmit = () => {
if (isSubmitted) { if (isSubmitted) {
onNext?.(); if (isLastQuestion) {
onComplete();
} else {
onNext();
}
return; return;
} }
@@ -143,7 +156,11 @@ export function AIMCQQuestion(props: AIMCQQuestionProps) {
onClick={handleSubmit} onClick={handleSubmit}
disabled={!canSubmit} disabled={!canSubmit}
> >
{isSubmitted ? 'Next Question' : 'Submit Answer'} {isSubmitted
? isLastQuestion
? 'Finish Quiz'
: 'Next Question'
: 'Submit Answer'}
</button> </button>
</div> </div>
); );

View File

@@ -20,6 +20,9 @@ type AIOpenEndedQuestionProps = {
setUserAnswer: (answer: string) => void; setUserAnswer: (answer: string) => void;
setCorrectAnswer: (answer: string) => void; setCorrectAnswer: (answer: string) => void;
isLastQuestion: boolean;
onComplete: () => void;
}; };
export function AIOpenEndedQuestion(props: AIOpenEndedQuestionProps) { export function AIOpenEndedQuestion(props: AIOpenEndedQuestionProps) {
@@ -31,6 +34,8 @@ export function AIOpenEndedQuestion(props: AIOpenEndedQuestionProps) {
onNext, onNext,
setUserAnswer, setUserAnswer,
setCorrectAnswer, setCorrectAnswer,
isLastQuestion,
onComplete,
} = props; } = props;
const { title: questionText } = question; const { title: questionText } = question;
@@ -63,7 +68,11 @@ export function AIOpenEndedQuestion(props: AIOpenEndedQuestionProps) {
const handleSubmit = async () => { const handleSubmit = async () => {
if (isSubmitted) { if (isSubmitted) {
onNext?.(); if (isLastQuestion) {
onComplete();
} else {
onNext();
}
return; return;
} }
@@ -126,7 +135,11 @@ export function AIOpenEndedQuestion(props: AIOpenEndedQuestionProps) {
{isVerifying ? ( {isVerifying ? (
<Loader2Icon className="size-4 animate-spin stroke-[2.5]" /> <Loader2Icon className="size-4 animate-spin stroke-[2.5]" />
) : isSubmitted ? ( ) : isSubmitted ? (
'Next Question' isLastQuestion ? (
'Finish Quiz'
) : (
'Next Question'
)
) : ( ) : (
'Submit Answer' 'Submit Answer'
)} )}

View File

@@ -46,6 +46,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
const activeQuestionState = const activeQuestionState =
questionStates[activeQuestionIndex] ?? DEFAULT_QUESTION_STATE; questionStates[activeQuestionIndex] ?? DEFAULT_QUESTION_STATE;
const isLastQuestion = activeQuestionIndex === questions.length - 1;
const handleSubmit = (status: QuestionState['status']) => { const handleSubmit = (status: QuestionState['status']) => {
setQuestionStates((prev) => { setQuestionStates((prev) => {
@@ -62,10 +63,6 @@ export function AIQuizContent(props: AIQuizContentProps) {
return newSelectedOptions; return newSelectedOptions;
}); });
setQuizStatus(
activeQuestionIndex === questions.length - 1 ? 'submitted' : 'answering',
);
}; };
const handleSetUserAnswer = (userAnswer: string) => { const handleSetUserAnswer = (userAnswer: string) => {
@@ -147,6 +144,10 @@ export function AIQuizContent(props: AIQuizContentProps) {
setActiveQuestionIndex(activeQuestionIndex + 1); setActiveQuestionIndex(activeQuestionIndex + 1);
}; };
const handleComplete = () => {
setQuizStatus('submitted');
};
return ( return (
<div className="flex h-full w-full flex-col"> <div className="flex h-full w-full flex-col">
<div className="relative flex h-full flex-col overflow-y-auto"> <div className="relative flex h-full flex-col overflow-y-auto">
@@ -192,6 +193,8 @@ export function AIQuizContent(props: AIQuizContentProps) {
setSelectedOptions={handleSelectOptions} setSelectedOptions={handleSelectOptions}
onSubmit={handleSubmit} onSubmit={handleSubmit}
onNext={handleNextQuestion} onNext={handleNextQuestion}
isLastQuestion={isLastQuestion}
onComplete={handleComplete}
/> />
)} )}
@@ -205,6 +208,8 @@ export function AIQuizContent(props: AIQuizContentProps) {
onNext={handleNextQuestion} onNext={handleNextQuestion}
setUserAnswer={handleSetUserAnswer} setUserAnswer={handleSetUserAnswer}
setCorrectAnswer={handleSetCorrectAnswer} setCorrectAnswer={handleSetCorrectAnswer}
isLastQuestion={isLastQuestion}
onComplete={handleComplete}
/> />
)} )}
</> </>