@@ -20,9 +20,25 @@ export default function Quiz({ applicantId, onQuizComplete, onBackToVideos }: Qu
2020
2121 const currentQuestion = quizQuestions [ currentQuestionIndex ] ;
2222 const isLastQuestion = currentQuestionIndex === quizQuestions . length - 1 ;
23- const hasSelectedAnswer = selectedAnswers [ currentQuestion . id ] !== undefined ;
23+ const hasSelectedAnswer = currentQuestion ? selectedAnswers [ currentQuestion . id ] !== undefined : false ;
24+
25+ // Early return if no current question (safety check)
26+ if ( ! currentQuestion && ! showResults ) {
27+ return (
28+ < div className = "space-y-6 text-center" >
29+ < p className = "text-red-400" > Error: No quiz questions available.</ p >
30+ { onBackToVideos && (
31+ < Button onClick = { onBackToVideos } variant = "outline" >
32+ ← Back to Videos
33+ </ Button >
34+ ) }
35+ </ div >
36+ ) ;
37+ }
2438
2539 const handleSelectAnswer = ( optionIndex : number ) => {
40+ if ( ! currentQuestion ) return ;
41+
2642 setSelectedAnswers ( {
2743 ...selectedAnswers ,
2844 [ currentQuestion . id ] : optionIndex ,
@@ -131,7 +147,7 @@ export default function Quiz({ applicantId, onQuizComplete, onBackToVideos }: Qu
131147 </ div >
132148 < p className = "mb-1 text-sm text-gray-400" >
133149 Your answer: < span className = { isCorrect ? "text-green-400" : "text-red-400" } >
134- { question . options [ userAnswer ] }
150+ { userAnswer !== undefined ? question . options [ userAnswer ] : "Not answered" }
135151 </ span >
136152 </ p >
137153 { ! isCorrect && (
@@ -184,6 +200,9 @@ export default function Quiz({ applicantId, onQuizComplete, onBackToVideos }: Qu
184200 ) ;
185201 }
186202
203+ // This check is now safe because of the early return above
204+ if ( ! currentQuestion ) return null ;
205+
187206 return (
188207 < div className = "space-y-6" >
189208 { /* Progress */ }
@@ -258,4 +277,4 @@ export default function Quiz({ applicantId, onQuizComplete, onBackToVideos }: Qu
258277 </ div >
259278 </ div >
260279 ) ;
261- }
280+ }
0 commit comments