refactor [P-36 УМТ]: страница конкретного теста

This commit is contained in:
Denzel committed 2025-02-17 16:32:30 +03:00
1 parent 09b9980c09
commit 7c24a8af7f
6 files changed
+98 -49

No files matched your search

@@ -6,19 +6,20 @@
:formattedTime="formattedTime"
:finishTest="openFinishDialog"
:currentQuestion="currentQuestion"
:totalQuestions="questions.length"
:totalQuestions="data?.question_count"
@change-question="changeQuestion"
:data="data"
/>
<!-- Блок информации о тесте -->
<BodyDetailTesting v-if="!testStarted && !testFinished" :startTest="startTest" />
<BodyDetailTesting v-if="!testStarted && !testFinished" :data="data" :startTest="startTest" />
<!-- Вопросы теста -->
<QuestionBlock
v-if="testStarted"
:question="questions[currentQuestion]"
:question="data.questions[currentQuestion]"
:currentQuestion="currentQuestion"
:totalQuestions="questions.length"
:totalQuestions="data.question_count"
:answers="answers"
@change-question="changeQuestion"
/>
@@ -30,7 +31,7 @@
:testPassed="testPassed"
:correctAnswers="correctAnswers"
:answeredQuestions="answeredQuestions"
:totalQuestions="questions.length"
:totalQuestions="data.question_count"
:formattedTime="formattedTime"
/>
@@ -49,21 +50,28 @@
</template>
<script>
import { computed, ref, onUnmounted } from 'vue';
import { computed, ref, onUnmounted, onMounted, watch } from 'vue';
import HeaderDetailTesting from '@/components/testing/HeaderDetailTesting.vue';
import BodyDetailTesting from '@/components/testing/BodyDetailTesting.vue';
import QuestionBlock from '@/components/testing/QuestionBlock.vue';
import TestResults from '@/components/testing/TestResults.vue';
import { axiosApiInstance } from '@/utils/axios-api';
export default {
components: { BodyDetailTesting, HeaderDetailTesting, QuestionBlock, TestResults },
setup() {
const timeLeft = ref(900);
const testStarted = ref(false);
const testFinished = ref(false);
const showFinishDialog = ref(false);
const currentQuestion = ref(0);
let timer = null;
const data = ref(null);
const getTestById = async (id) => {
const resp = await axiosApiInstance.get(`/api/tests/testdetail/${id}`);
data.value = resp.data;
console.log(data.value);
};
const questions = ref([
{
@@ -85,48 +93,36 @@ export default {
{ question: 'Сколько ног у паука?', options: ['4', '6', '8', '10'], correct: '8' },
]);
const answers = ref(new Array(questions.value.length).fill(null));
const formattedTime = computed(() => {
const minutes = Math.floor(timeLeft.value / 60);
const seconds = timeLeft.value % 60;
if (!data.value?.time) return '00:00';
// Если тест не запущен и не завершён — считаем, что время в data.value.time в минутах
if (!testStarted.value && !testFinished.value) {
return `${String(data.value.time).padStart(2, '0')}:00`;
}
// Если тест запущен или завершён — data.value.time уже в секундах
const minutes = Math.floor(data.value.time / 60);
const seconds = data.value.time % 60;
return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
});
const correctAnswers = computed(
() =>
answers.value.filter((answer, index) => answer === questions.value[index].correct).length,
);
const answeredQuestions = computed(
() => answers.value.filter((answer) => answer !== null).length,
);
const testPassed = computed(() => correctAnswers.value >= questions.value.length / 2);
const startTest = () => {
testStarted.value = true;
testFinished.value = false;
// Переводим минуты в секунды перед запуском таймера
data.value = { ...data.value, time: data.value.time * 60 };
timer = setInterval(() => {
if (timeLeft.value > 0) {
timeLeft.value--;
if (data.value?.time > 0) {
data.value = { ...data.value, time: data.value.time - 1 };
} else {
clearInterval(timer);
openFinishDialog();
finishTest(); // Завершаем тест, если время закончилось
}
}, 1000);
};
const changeQuestion = (newIndex) => {
if (newIndex >= 0 && newIndex < questions.value.length) {
currentQuestion.value = newIndex;
}
};
const openFinishDialog = () => {
showFinishDialog.value = true;
};
const finishTest = () => {
clearInterval(timer);
testStarted.value = false;
@@ -134,10 +130,45 @@ export default {
showFinishDialog.value = false;
};
const answers = ref([]);
watch(data?.questions, (newData) => {
if (newData && Array.isArray(newData.questions)) {
answers.value = newData.questions.map(() => null);
}
});
const correctAnswers = computed(
() =>
answers.value.filter(
(answer, index) => answer === data?.value.questions[index].settings.correct,
).length,
);
const answeredQuestions = computed(
() => answers.value.filter((answer) => answer !== null).length,
);
const testPassed = computed(() => correctAnswers.value >= data?.value.questions.length / 2);
const changeQuestion = (newIndex) => {
if (newIndex >= 0 && newIndex < data?.value.questions.length) {
currentQuestion.value = newIndex;
}
};
const openFinishDialog = () => {
showFinishDialog.value = true;
};
onUnmounted(() => {
if (timer) clearInterval(timer);
});
onMounted(() => {
getTestById(1);
});
return {
testStarted,
testFinished,
@@ -153,6 +184,7 @@ export default {
correctAnswers,
answeredQuestions,
testPassed,
data,
};
},
};