33import { useState , useEffect } from 'react' ;
44import Link from 'next/link' ;
55import { allQuestions } from '@/lib/questionsData' ;
6+ import { getResponse , saveResponse , deleteResponse } from '@/lib/responseStorage' ;
67
78interface Question {
89 id : string ;
@@ -17,6 +18,7 @@ export default function PracticePage() {
1718 const [ timer , setTimer ] = useState ( 0 ) ;
1819 const [ isActive , setIsActive ] = useState ( false ) ;
1920 const [ usedQuestions , setUsedQuestions ] = useState < Set < string > > ( new Set ( ) ) ;
21+ const [ response , setResponse ] = useState ( '' ) ;
2022
2123 useEffect ( ( ) => {
2224 let interval : NodeJS . Timeout | null = null ;
@@ -43,6 +45,11 @@ export default function PracticePage() {
4345 const getRandomQuestion = ( ) => {
4446 if ( questions . length === 0 ) return ;
4547
48+ // Save current response before switching questions (only if not empty)
49+ if ( currentQuestion && response . trim ( ) ) {
50+ saveResponse ( currentQuestion . id , response ) ;
51+ }
52+
4653 // If all questions have been used, reset
4754 if ( usedQuestions . size >= questions . length ) {
4855 setUsedQuestions ( new Set ( ) ) ;
@@ -58,21 +65,40 @@ export default function PracticePage() {
5865 const newQuestion = questions [ randomIndex ] ;
5966 setCurrentQuestion ( newQuestion ) ;
6067 setUsedQuestions ( new Set ( [ newQuestion . id ] ) ) ;
68+ setResponse ( '' ) ; // Clear the text box for new question
6169 } else {
6270 const randomIndex = Math . floor ( Math . random ( ) * availableQuestions . length ) ;
6371 const newQuestion = availableQuestions [ randomIndex ] ;
6472 setCurrentQuestion ( newQuestion ) ;
6573 setUsedQuestions ( prev => new Set ( [ ...prev , newQuestion . id ] ) ) ;
74+ setResponse ( '' ) ; // Clear the text box for new question
6675 }
6776
6877 setTimer ( 0 ) ;
6978 setIsActive ( true ) ;
7079 } ;
7180
7281 const skipQuestion = ( ) => {
82+ // Save current response before skipping (only if not empty)
83+ if ( currentQuestion && response . trim ( ) ) {
84+ saveResponse ( currentQuestion . id , response ) ;
85+ }
7386 getRandomQuestion ( ) ;
7487 } ;
7588
89+ const handleSaveResponse = ( ) => {
90+ if ( currentQuestion ) {
91+ saveResponse ( currentQuestion . id , response ) ;
92+ }
93+ } ;
94+
95+ const handleClearResponse = ( ) => {
96+ if ( currentQuestion ) {
97+ deleteResponse ( currentQuestion . id ) ;
98+ setResponse ( '' ) ;
99+ }
100+ } ;
101+
76102 return (
77103 < div className = "min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 dark:from-slate-900 dark:to-slate-800" >
78104 < div className = "container mx-auto px-4 py-8 max-w-4xl" >
@@ -131,6 +157,36 @@ export default function PracticePage() {
131157 { currentQuestion . text }
132158 </ p >
133159 </ div >
160+
161+ < div className = "mt-6" >
162+ < div className = "flex justify-between items-center mb-2" >
163+ < label
164+ htmlFor = "response"
165+ className = "block text-sm font-medium text-slate-700 dark:text-slate-300"
166+ >
167+ Your Response:
168+ </ label >
169+ { response . trim ( ) && (
170+ < button
171+ onClick = { handleClearResponse }
172+ className = "text-xs px-3 py-1 bg-red-100 dark:bg-red-900 text-red-700 dark:text-red-200 hover:bg-red-200 dark:hover:bg-red-800 rounded transition-colors"
173+ >
174+ Clear Response
175+ </ button >
176+ ) }
177+ </ div >
178+ < textarea
179+ id = "response"
180+ value = { response }
181+ onChange = { ( e ) => setResponse ( e . target . value ) }
182+ onBlur = { handleSaveResponse }
183+ placeholder = "Type your answer here... (auto-saves)"
184+ className = "w-full h-48 px-4 py-3 bg-slate-50 dark:bg-slate-900 border-2 border-slate-200 dark:border-slate-700 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent text-slate-900 dark:text-slate-100 placeholder-slate-400 dark:placeholder-slate-500 resize-y"
185+ />
186+ < p className = "mt-2 text-xs text-slate-500 dark:text-slate-400" >
187+ 💾 Your response is automatically saved to local storage when you type or switch questions
188+ </ p >
189+ </ div >
134190 </ div >
135191
136192 < div className = "flex gap-4 justify-center" >
0 commit comments