@@ -13,23 +13,7 @@ import {
13
13
} from "api/graphql/generated/graphql" ;
14
14
15
15
import TestView from "../views/test-view" ;
16
-
17
- interface TestQuestion {
18
- id : string ;
19
- text : string ;
20
- }
21
-
22
- interface TestAnswer {
23
- id : string ;
24
- text : string ;
25
- correct : boolean ;
26
- testQuestion : TestQuestion ;
27
- }
28
-
29
- interface UserAnswer {
30
- questionId : string ;
31
- answerId : string ;
32
- }
16
+ import { TestAnswer , UserAnswer } from "../types" ;
33
17
34
18
interface TestContainerProps {
35
19
testId : string ;
@@ -112,10 +96,20 @@ const TestContainer: FC<TestContainerProps> = ({
112
96
answerResult . answer === true
113
97
) {
114
98
if ( question . id && answerResult . testAnswer . id ) {
115
- restoredAnswers . push ( {
116
- questionId : question . id ,
117
- answerId : answerResult . testAnswer . id ,
118
- } ) ;
99
+ const existingAnswerIndex = restoredAnswers . findIndex (
100
+ ( answer ) => answer . questionId === question . id
101
+ ) ;
102
+
103
+ if ( existingAnswerIndex >= 0 ) {
104
+ restoredAnswers [ existingAnswerIndex ] . answerIds . push (
105
+ answerResult . testAnswer . id
106
+ ) ;
107
+ } else {
108
+ restoredAnswers . push ( {
109
+ questionId : question . id ,
110
+ answerIds : [ answerResult . testAnswer . id ] ,
111
+ } ) ;
112
+ }
119
113
}
120
114
}
121
115
} ) ;
@@ -256,7 +250,7 @@ const TestContainer: FC<TestContainerProps> = ({
256
250
fetchCurrentAnswers ( ) ;
257
251
} , [ currentQuestion , getTestAnswers ] ) ;
258
252
259
- const handleAnswerSelect = ( answerId : string ) => {
253
+ const handleAnswerSelect = ( answerId : string , isSelected : boolean ) => {
260
254
if ( ! currentQuestion ?. id ) return ;
261
255
262
256
const existingAnswerIndex = userAnswers . findIndex (
@@ -265,15 +259,27 @@ const TestContainer: FC<TestContainerProps> = ({
265
259
266
260
if ( existingAnswerIndex >= 0 ) {
267
261
const updatedAnswers = [ ...userAnswers ] ;
268
- updatedAnswers [ existingAnswerIndex ] = {
269
- questionId : currentQuestion . id ,
270
- answerId,
271
- } ;
262
+ const currentAnswerIds = updatedAnswers [ existingAnswerIndex ] . answerIds ;
263
+
264
+ if ( isSelected ) {
265
+ if ( ! currentAnswerIds . includes ( answerId ) ) {
266
+ updatedAnswers [ existingAnswerIndex ] = {
267
+ questionId : currentQuestion . id ,
268
+ answerIds : [ ...currentAnswerIds , answerId ] ,
269
+ } ;
270
+ }
271
+ } else {
272
+ updatedAnswers [ existingAnswerIndex ] = {
273
+ questionId : currentQuestion . id ,
274
+ answerIds : currentAnswerIds . filter ( ( id ) => id !== answerId ) ,
275
+ } ;
276
+ }
277
+
272
278
setUserAnswers ( updatedAnswers ) ;
273
- } else {
279
+ } else if ( isSelected ) {
274
280
setUserAnswers ( [
275
281
...userAnswers ,
276
- { questionId : currentQuestion . id , answerId } ,
282
+ { questionId : currentQuestion . id , answerIds : [ answerId ] } ,
277
283
] ) ;
278
284
}
279
285
} ;
@@ -285,77 +291,25 @@ const TestContainer: FC<TestContainerProps> = ({
285
291
( answer ) => answer . questionId === currentQuestion . id
286
292
) ;
287
293
288
- if ( currentAnswer ) {
289
- await handleSendAnswer ( currentQuestion . id , [ currentAnswer . answerId ] ) ;
294
+ if ( currentAnswer && currentAnswer . answerIds . length > 0 ) {
295
+ await handleSendAnswer ( currentQuestion . id , currentAnswer . answerIds ) ;
290
296
}
291
297
292
298
if ( currentQuestionIndex < testQuestions . length - 1 ) {
293
299
setCurrentQuestionIndex ( currentQuestionIndex + 1 ) ;
294
- } else {
295
- }
296
- } ;
297
-
298
- const handleFinishTest = async ( ) => {
299
- if ( ! testAttemptId ) {
300
- const errorMsg = "❌ Нет ID попытки теста" ;
301
- console . error ( errorMsg ) ;
302
- setErrorMessage ( errorMsg ) ;
303
- return ;
304
- }
305
-
306
- if ( userAnswers . length < testQuestions . length ) {
307
- const errorMsg = `❌ Не все вопросы отвечены: ${ userAnswers . length } из ${ testQuestions . length } ` ;
308
- console . error ( errorMsg ) ;
309
- setErrorMessage ( errorMsg ) ;
310
- return ;
311
- }
312
-
313
- const successThreshold =
314
- testData ?. testTestGroupsById ?. successThreshold ?? 0 ;
315
- if ( score < successThreshold ) {
316
- const errorMsg = `❌ Порог прохождения не достигнут: ${ score } правильных ответов из ${ successThreshold } требуемых. Тест не может быть завершен.` ;
317
- console . error ( errorMsg ) ;
318
- setErrorMessage ( errorMsg ) ;
319
- return ;
320
- }
321
-
322
- try {
323
- console . log ( "🏁 Завершаем тест, ID попытки:" , testAttemptId ) ;
324
- console . log ( "📊 Статистика ответов:" , {
325
- всего : testQuestions . length ,
326
- отвечено : userAnswers . length ,
327
- правильных : score ,
328
- порог : successThreshold ,
329
- достигнут : score >= successThreshold ,
330
- } ) ;
331
-
332
- setIsCompleted ( true ) ;
333
- setSuccessMessage ( "✅ Тест успешно завершен!" ) ;
334
- setErrorMessage ( null ) ;
335
- } catch ( error : any ) {
336
- console . error ( "❌ Ошибка при завершении теста:" , error ) ;
337
-
338
- const errorMsg = `❌ Ошибка при завершении теста: ${
339
- error . message || "Неизвестная ошибка"
340
- } `;
341
- setErrorMessage ( errorMsg ) ;
342
300
}
343
301
} ;
344
302
345
303
const currentAnswer = userAnswers . find (
346
304
( ua ) => ua . questionId === currentQuestion ?. id
347
305
) ;
348
- const isCurrentQuestionAnswered = ! ! currentAnswer ;
306
+ const isCurrentQuestionAnswered =
307
+ ! ! currentAnswer && currentAnswer . answerIds . length > 0 ;
349
308
350
309
if ( testLoading || answersLoading ) return < AppSpinner /> ;
351
310
if ( ! testData ?. testTestGroupsById || ! currentQuestion )
352
311
return < NoDataErrorMessage /> ;
353
312
354
- const typedCurrentQuestion : TestQuestion = {
355
- id : currentQuestion . id ! ,
356
- text : currentQuestion . text ! ,
357
- } ;
358
-
359
313
return (
360
314
< >
361
315
< TestView
0 commit comments