Skip to content

Commit 264f9b9

Browse files
authored
Merge pull request #390 from qa-guru/QAGDEV-723
QAGDEV-723 - [FE] Прикрепления тестов к заданиям v9
2 parents 242fe0c + 99ee6d7 commit 264f9b9

File tree

4 files changed

+122
-139
lines changed

4 files changed

+122
-139
lines changed

src/features/test/containers/test-container.tsx

Lines changed: 38 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,7 @@ import {
1313
} from "api/graphql/generated/graphql";
1414

1515
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";
3317

3418
interface TestContainerProps {
3519
testId: string;
@@ -112,10 +96,20 @@ const TestContainer: FC<TestContainerProps> = ({
11296
answerResult.answer === true
11397
) {
11498
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+
}
119113
}
120114
}
121115
});
@@ -256,7 +250,7 @@ const TestContainer: FC<TestContainerProps> = ({
256250
fetchCurrentAnswers();
257251
}, [currentQuestion, getTestAnswers]);
258252

259-
const handleAnswerSelect = (answerId: string) => {
253+
const handleAnswerSelect = (answerId: string, isSelected: boolean) => {
260254
if (!currentQuestion?.id) return;
261255

262256
const existingAnswerIndex = userAnswers.findIndex(
@@ -265,15 +259,27 @@ const TestContainer: FC<TestContainerProps> = ({
265259

266260
if (existingAnswerIndex >= 0) {
267261
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+
272278
setUserAnswers(updatedAnswers);
273-
} else {
279+
} else if (isSelected) {
274280
setUserAnswers([
275281
...userAnswers,
276-
{ questionId: currentQuestion.id, answerId },
282+
{ questionId: currentQuestion.id, answerIds: [answerId] },
277283
]);
278284
}
279285
};
@@ -285,77 +291,25 @@ const TestContainer: FC<TestContainerProps> = ({
285291
(answer) => answer.questionId === currentQuestion.id
286292
);
287293

288-
if (currentAnswer) {
289-
await handleSendAnswer(currentQuestion.id, [currentAnswer.answerId]);
294+
if (currentAnswer && currentAnswer.answerIds.length > 0) {
295+
await handleSendAnswer(currentQuestion.id, currentAnswer.answerIds);
290296
}
291297

292298
if (currentQuestionIndex < testQuestions.length - 1) {
293299
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);
342300
}
343301
};
344302

345303
const currentAnswer = userAnswers.find(
346304
(ua) => ua.questionId === currentQuestion?.id
347305
);
348-
const isCurrentQuestionAnswered = !!currentAnswer;
306+
const isCurrentQuestionAnswered =
307+
!!currentAnswer && currentAnswer.answerIds.length > 0;
349308

350309
if (testLoading || answersLoading) return <AppSpinner />;
351310
if (!testData?.testTestGroupsById || !currentQuestion)
352311
return <NoDataErrorMessage />;
353312

354-
const typedCurrentQuestion: TestQuestion = {
355-
id: currentQuestion.id!,
356-
text: currentQuestion.text!,
357-
};
358-
359313
return (
360314
<>
361315
<TestView

src/features/test/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export interface TestQuestion {
2+
id: string;
3+
text: string;
4+
}
5+
6+
export interface TestAnswer {
7+
id: string;
8+
text: string;
9+
correct: boolean;
10+
testQuestion: TestQuestion;
11+
}
12+
13+
export interface UserAnswer {
14+
questionId: string;
15+
answerIds: string[];
16+
}

0 commit comments

Comments
 (0)