@@ -18,7 +18,7 @@ import {HistoricalEvent, IDataForQuiz, IQuizBlockProps, SubtopicsProps} from "..
18
18
import { makeStyles } from "tss-react/mui" ;
19
19
import axiosClient from "../../axios" ;
20
20
import QuizResults from "./components/QuizResults/QuizResults" ;
21
- import QuestionContainer from "./components/QuestionContainer/QuestionContainer" ;
21
+ import QuestionContainer , { IQuestionContainerProps } from "./components/QuestionContainer/QuestionContainer" ;
22
22
23
23
24
24
const useStyles = makeStyles ( ) ( ( theme ) => ( {
@@ -86,7 +86,11 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
86
86
setCurrentUser,
87
87
currentUser
88
88
} ) => {
89
+ console . log ( "Current Props:" ,
90
+ historyList ,
91
+ currentUser ) ;
89
92
93
+ console . log ( "QuizBlock.tsx start" )
90
94
91
95
const [ currentQuestion , setCurrentQuestion ] = useState ( 0 ) ;
92
96
const [ results , setResults ] = useState ( { correct : 0 , incorrect : 0 } ) ;
@@ -99,7 +103,7 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
99
103
const [ answerChosen , setAnswerChosen ] = useState ( false ) ;
100
104
const [ currentAnswerStatus , setCurrentAnswerStatus ] = useState ( false ) ;
101
105
const maxTimeStatic = 30 ;
102
- const [ remainingTime , setRemainingTime ] = useState ( maxTimeStatic ) ;
106
+ const [ timeIsFinished , setTimeIsFinished ] = useState < boolean > ( false )
103
107
const [ currentTestId , setCurrentTestId ] = useState < number | null | undefined > ( null )
104
108
const [ currentArticleTitle , setCurrentArticleTitle ] = useState < string > ( "" )
105
109
const [ currentArticle , setCurrentArticle ] = useState < SubtopicsProps | HistoricalEvent | null > ( null ) ;
@@ -121,17 +125,22 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
121
125
const smUp = useMediaQuery ( themeDefault . breakpoints . up ( 'sm' ) ) ;
122
126
123
127
124
- useEffect ( ( ) => {
125
- if ( setSelectedSubArticle ) {
126
- setSelectedSubArticle ( selectedSubArticleNumber ) ;
127
- }
128
- } , [ setSelectedSubArticle , selectedSubArticleNumber ] ) ;
128
+ // useEffect(() => {
129
+ // if (setSelectedSubArticle) {
130
+ // setSelectedSubArticle(selectedSubArticleNumberFromUrl );
131
+ // }
132
+ // }, [selectedSubArticleNumberFromUrl ]);
129
133
130
134
useEffect ( ( ) => {
131
- if ( historyList . length > 0 ) {
135
+ console . log ( " historyList" , historyList ) ;
132
136
137
+ if ( historyList . length > 0 ) {
138
+ console . log ( "testType" , testType ) ;
133
139
if ( testType === "subArticle" ) {
134
140
selectedSubArticleNumber = parseInt ( subtopicId || '0' , 10 ) ;
141
+ if ( setSelectedSubArticle ) {
142
+ setSelectedSubArticle ( selectedSubArticleNumber ) ;
143
+ }
135
144
const selectedArticle = historyList [ selectedArticleNumber ] ;
136
145
if ( selectedArticle && selectedArticle . subtopics ) {
137
146
const selectedSubtopic = selectedArticle . subtopics [ selectedSubArticleNumber ] ;
@@ -148,21 +157,41 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
148
157
}
149
158
150
159
}
151
- } , [ historyList , subtopicId ] ) ;
160
+ } , [ subtopicId , historyList ] ) ;
152
161
153
- useEffect ( ( ) => {
154
- const updateStateFromArticle = ( questions : IDataForQuiz [ ] ) => {
155
- const questionsArray : string [ ] = questions . map ( item => item . question ) || [ ] ;
156
- setQuizQuestions ( questionsArray ) ;
162
+ const updateStateFromArticle = ( questions : IDataForQuiz [ ] ) => {
163
+ const questionsArray : string [ ] = questions . map ( item => item . question ) || [ ] ;
164
+ setQuizQuestions ( questionsArray ) ;
165
+ console . log ( "questionsArray" , questionsArray ) ;
166
+
167
+ const optionsArray : string [ ] [ ] = questions . map ( item => item . options ) || [ ] ;
168
+ setQuizOptions ( optionsArray ) ;
169
+ console . log ( "optionsArray" , optionsArray ) ;
170
+
171
+ const correctAnswersArray : number [ ] = questions . map ( item => item . correct_answers ) || [ ] ;
172
+ setQuizCorrectAnswers ( correctAnswersArray ) ;
173
+ console . log ( "correctAnswersArray" , correctAnswersArray ) ;
157
174
158
- const optionsArray : string [ ] [ ] = questions . map ( item => item . options ) || [ ] ;
159
- setQuizOptions ( optionsArray ) ;
160
175
161
- const correctAnswersArray : number [ ] = questions . map ( item => item . correct_answers ) || [ ] ;
162
- setQuizCorrectAnswers ( correctAnswersArray ) ;
163
- } ;
176
+ // inspect
177
+
178
+ const isValid = optionsArray . every ( ( options , index ) =>
179
+ correctAnswersArray [ index ] >= 0 && correctAnswersArray [ index ] < options . length
180
+ ) ;
181
+
182
+ const equalArrayLenght = optionsArray . length === correctAnswersArray . length && correctAnswersArray . length === questions . length ;
183
+
184
+ if ( ! equalArrayLenght || ! isValid ) console . warn ( "Length mismatch: optionsArray and correctAnswersArray should be" +
185
+ " of" +
186
+ " the same length as questionsArray" ) ;
187
+
188
+ } ;
189
+
190
+ useEffect ( ( ) => {
164
191
165
192
if ( currentArticle ) {
193
+ console . log ( "currentArticle1111" , currentArticle )
194
+
166
195
if ( testType === "subArticle" ) {
167
196
setCurrentArticleTitle ( "title" in currentArticle ? currentArticle . title : "" ) ;
168
197
setCurrentTestId ( "sub_article_test_id" in currentArticle ? currentArticle . sub_article_test_id : undefined ) ;
@@ -179,7 +208,7 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
179
208
}
180
209
}
181
210
}
182
- } , [ currentArticle , testType ] ) ;
211
+ } , [ currentArticle ] ) ;
183
212
184
213
185
214
// Успішне завершення тесту
@@ -242,19 +271,19 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
242
271
243
272
useEffect ( ( ) => {
244
273
245
- if ( remainingTime === 0 && ! answerChosen ) {
274
+ if ( timeIsFinished && ! answerChosen ) {
246
275
setResults ( { ...results , incorrect : results . incorrect + 1 } ) ;
247
276
setCurrentAnswerStatus ( false ) ;
248
277
setIsNextButtonActive ( true ) ;
249
278
}
250
279
251
- } , [ answerChosen , remainingTime ] )
280
+ } , [ answerChosen , timeIsFinished ] )
252
281
253
282
const clearSettingsBeforeNewQuestion = ( ) => {
254
283
setSelectedAnswer ( null ) ;
255
284
setIsNextButtonActive ( false ) ;
256
285
setAnswerChosen ( false )
257
- setRemainingTime ( maxTimeStatic ) ;
286
+ setTimeIsFinished ( false ) ;
258
287
}
259
288
const handleRetakeQuiz = ( ) => {
260
289
setCurrentQuestion ( 0 ) ;
@@ -267,7 +296,7 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
267
296
268
297
269
298
const handleNextQuestion = ( ) => {
270
- if ( selectedAnswer === null && remainingTime > 0 ) {
299
+ if ( selectedAnswer === null && ! timeIsFinished ) {
271
300
return ;
272
301
}
273
302
@@ -295,50 +324,55 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
295
324
} ;
296
325
297
326
298
- useEffect ( ( ) => {
299
- let timer : NodeJS . Timeout | undefined ;
300
-
301
- // Timer effect
302
- const startTimer = ( ) => {
303
- timer = setInterval ( ( ) => {
304
- setRemainingTime ( ( prevTime ) => {
305
- if ( prevTime === 0 ) {
306
- clearInterval ( timer ) ; // Clear the timer when it reaches 0
307
- return 0 ;
308
- } else {
309
- return prevTime - 1 ; // Decrement the remaining time
310
- }
311
- } ) ;
312
- } , 1000 ) ;
313
- } ;
314
-
315
- if ( ! answerChosen ) {
316
- // Run the timer only if answerChosen is false
317
- startTimer ( ) ;
318
- } else {
319
- clearInterval ( timer ) ;
320
- }
321
-
322
- // Cleanup function
323
- return ( ) => {
324
- if ( timer ) clearInterval ( timer ) ;
325
- } ;
326
-
327
- } , [ answerChosen ] ) ; // Runs whenever answerChosen changes; // Runs whenever answerChosen changes; // Runs once when the component mounts
327
+ // useEffect(() => {
328
+ // let timer: NodeJS.Timeout | undefined;
329
+ //
330
+ // // Timer effect
331
+ // const startTimer = () => {
332
+ // Timertimer = setInterval(() => {
333
+ // setRemainingTime((prevTime) => {
334
+ // if (prevTime === 0) {
335
+ // clearInterval(timer); // Clear the timer when it reaches 0
336
+ // setTimeIsFinished(true)
337
+ // return 0;
338
+ // } else {
339
+ // return prevTime - 1; // Decrement the remaining time
340
+ // }
341
+ // });
342
+ // }, 1000);
343
+ // };
344
+ //
345
+ // if (!answerChosen) {
346
+ // // Run the timer only if answerChosen is false
347
+ // console.log("Run the timer ")
348
+ // startTimer();
349
+ // } else {
350
+ // clearInterval(timer);
351
+ // }
352
+ // // startTimer()
353
+ // // Cleanup function
354
+ // return () => {
355
+ // if (timer) clearInterval(timer);
356
+ // };
357
+ //
358
+ // }, [answerChosen]); // Runs whenever answerChosen changes; // Runs whenever answerChosen changes; // Runs once when the component mounts
328
359
329
360
// Calculate the progress for CircularProgress
330
361
331
362
const optionHighlight = ( option : number ) => {
332
363
333
- if ( option === selectedAnswer ) {
364
+ if ( selectedAnswer !== null ) {
365
+ if ( option === selectedAnswer ) {
334
366
335
- if ( option === quizCorrectAnswers [ currentQuestion ] ) {
367
+ if ( option === quizCorrectAnswers [ currentQuestion ] ) {
368
+ return classes . sucessOptionSelected ;
369
+ } else return classes . errorOptionSelected ;
370
+
371
+ } else if ( option === quizCorrectAnswers [ currentQuestion ] ) {
336
372
return classes . sucessOptionSelected ;
337
- } else return classes . errorOptionSelected ;
373
+ } else return "" ;
374
+ }
338
375
339
- } else if ( option === quizCorrectAnswers [ currentQuestion ] ) {
340
- return classes . sucessOptionSelected ;
341
- } else return "" ;
342
376
343
377
}
344
378
@@ -348,21 +382,6 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
348
382
} else return "" ;
349
383
}
350
384
351
- useEffect ( ( ) => {
352
-
353
- const handleKeyPress = ( event : KeyboardEvent ) => {
354
- if ( event . key === 'Enter' ) {
355
- handleNextQuestion ( ) ;
356
- }
357
- } ;
358
-
359
- document . addEventListener ( 'keypress' , handleKeyPress ) ;
360
-
361
- return ( ) => {
362
- document . removeEventListener ( 'keypress' , handleKeyPress ) ;
363
- } ;
364
- } , [ handleNextQuestion ] ) ;
365
-
366
385
367
386
const quizResultsProps = {
368
387
results,
@@ -382,7 +401,6 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
382
401
selectedAnswer,
383
402
handleAnswer,
384
403
handleAnswerKeyPress,
385
- remainingTime,
386
404
maxTimeStatic,
387
405
answerChosen,
388
406
currentAnswerStatus,
@@ -394,9 +412,13 @@ const QuizBlock: React.FC<IQuizBlockProps> = ({
394
412
currentArticleTitle,
395
413
percentCompleted,
396
414
currentQuestionText : quizQuestions [ currentQuestion ] ,
415
+ setTimeIsFinished,
416
+ timeIsFinished,
397
417
} ;
398
418
399
419
420
+ console . log ( timeIsFinished ? "timeIsFinished Yes" : "timeIsFinished No" )
421
+
400
422
return (
401
423
< Container >
402
424
< Helmet >
0 commit comments