refactor [P-36]: декомпозиция конструктора + адаптация под создания тестов

This commit is contained in:
Denzel committed 2025-02-28 09:54:47 +03:00
1 parent d3edc62c0b
commit 869264bd66
27 files changed
+889 -910

No files matched your search

+28 -395
View File
@@ -1,21 +1,13 @@
<template>
<v-row class="pt-2 mx-1 flex-column fill-height flex-grow-0">
<template v-if="isVoteCreated">
<v-card class="d-flex flex-column flex-grow-1 flex-shrink-0 text-center pa-8" flat>
<v-spacer></v-spacer>
<div class>
<v-icon color="success" size="48">mdi-check-circle</v-icon>
<h2 class="mt-4">Голосование создано!</h2>
<v-btn class="mt-4" color="teal-darken-4" @click="resetForm">Создать ещё</v-btn>
</div>
<v-spacer></v-spacer>
</v-card>
</template>
<CreatedVote v-if="isVoteCreated">
<v-btn class="mt-4" color="teal-darken-4" @click="resetForm">Создать ещё</v-btn>
</CreatedVote>
<template v-else>
<div class="v-col flex-grow-1 flex-shrink-0">
<v-stepper v-model="step" class="d-flex flex-column fill-height" editable>
<v-stepper-header class="flex-grow-0 flex-shrink-1">
<v-stepper-item :complete="step > 0" step="1">Основная информация </v-stepper-item>
<v-stepper-item :complete="step > 0" step="1">Основная информация</v-stepper-item>
<v-divider></v-divider>
<v-stepper-item :complete="step > 1" step="2">Создание опроса</v-stepper-item>
<v-divider></v-divider>
@@ -25,78 +17,11 @@
<v-stepper-window class="fill-height">
<!-- Основная информация -->
<v-stepper-window-item class="fill-height" step="1">
<v-card class="fill-height d-flex flex-column" flat>
<v-card-title>Основная информация</v-card-title>
<v-card-text class="d-flex flex-column overflow-auto">
<v-text-field
v-model="form.title"
:rules="[rules.required]"
class="flex-grow-0 flex-shrink-1"
label="Название голосования"
variant="underlined"
></v-text-field>
<div class="flex-grow-1 flex-shrink-0">
<v-textarea
v-model="form.description"
:rules="[rules.required]"
label="Описание"
variant="outlined"
></v-textarea>
</div>
<v-divider></v-divider>
<div class="flex-grow-0 flex-shrink-1">
<div class="pt-2 d-flex align-center">
<span class="text-overline text-grey text-center">Публикация новости: </span>
<v-switch
v-model="form.createnews"
class="px-2"
density="comfortable"
hide-details
color="primary"
:label="form.createnews ? 'да' : 'нет'"
></v-switch>
</div>
<div class="pt-2 text-overline text-grey">Дата и время:</div>
<div class="d-flex justify-space-around">
<div class="d-flex ga-3 align-center">
<h6 class="text-subtitle-1 pt-2">начало:</h6>
<date-select :datedefault="form.startDate" @returndate="returnstart" />
<v-select
v-model="selectedTimeStart"
:items="timeOptions"
density="compact"
hide-details
variant="plain"
>
<template v-slot:prepend-inner>
<v-icon color="red" icon="mdi-timer" size="xsmall"></v-icon>
</template>
</v-select>
</div>
<div class="d-flex ga-3 align-center">
<h6 class="text-subtitle-1 pt-2">окончание:</h6>
<date-select :datedefault="form.endDate" @returndate="returnend" />
<v-select
v-model="selectedTimeEnd"
:items="timeOptions"
density="compact"
hide-details
outlined
variant="plain"
>
<template v-slot:prepend-inner>
<v-icon color="red" icon="mdi-timer" size="xsmall"></v-icon>
</template>
</v-select>
</div>
</div>
</div>
</v-card-text>
<MainInfo v-model:form="form" :rules="rules">
<v-card-actions>
<v-btn color="teal-darken-4" size="medium" @click="step = 1">Далее</v-btn>
</v-card-actions>
</v-card>
</MainInfo>
</v-stepper-window-item>
<!-- Создание опроса -->
@@ -104,261 +29,23 @@
<v-card class="fill-height d-flex flex-column" flat>
<v-card-title>Создание опроса:</v-card-title>
<v-card-text style="max-height: 62vh; overflow-y: auto">
<transition-group name="list" tag="div">
<div v-for="(question, index) in form.questions" :key="index" class="mb-4">
<div class="d-flex">
<!-- <v-text-field v-model="question.text" :label="'Вопрос ' + (index + 1)" :rules="[rules.required]"-->
<!-- density="comfortable" variant="underlined">-->
<v-text-field
v-model="question.text"
:label="'Вопрос ' + (index + 1)"
density="comfortable"
variant="underlined"
:rules="[rules.required]"
@input="updateQuest(index)"
>
<template v-slot:append>
<v-btn
color="error"
icon="mdi-delete"
size="small"
@click="removeQuestion(index)"
/>
</template>
</v-text-field>
</div>
<v-file-input
v-model="question.image"
accept="image/*"
density="comfortable"
label="Картинка для вопроса"
variant="outlined"
></v-file-input>
<v-divider></v-divider>
<div class="py-2 text-overline text-grey">Настройки вопроса</div>
<div class="v-row">
<v-col class="align-self-center font-weight-bold py-0" cols="auto">
Тип ответа
</v-col>
<v-col class="py-0 align-self-center" cols="3">
<v-select
v-model="question.settings.choice"
:items="questionTypes"
:list-props="{ density: 'compact' }"
density="compact"
hide-details
item-title="name"
item-value="choice"
label=""
variant="outlined"
@update:model-value="question.answers = []"
></v-select>
<!-- choice -->
</v-col>
<v-spacer />
<v-col class="align-self-center font-weight-bold" cols="auto">
Число возможных ответов
</v-col>
<v-col class="" cols="1">
<v-text-field
v-model="question.settings.multiChoice"
:rules="[rules.required && question.settings.multiChoice > 0]"
density="compact"
hide-details
min="1"
type="number"
variant="outlined"
>
<template v-slot:prepend> </template>
</v-text-field>
</v-col>
</div>
<div class="v-row"></div>
<v-card-text class="px-0">
<div v-if="question.settings.choice === 'free'">
<transition-group name="list" tag="div">
<div
v-for="(answer, ind) in question.answers"
:key="ind"
class="d-flex align-start mb-4"
>
<v-text-field
v-model="answer.text"
:label="'Ответ ' + (ind + 1)"
:rules="[rules.required]"
class="mr-4"
density="compact"
variant="outlined"
>
</v-text-field>
<v-file-input
v-model="answer.image"
accept="image/*"
dense
density="compact"
hide-details
label="Картинка"
variant="outlined"
>
</v-file-input>
<v-btn
class="ms-1"
color="error"
icon="mdi-delete"
size="small"
@click="removeAnswer(index, ind)"
>
</v-btn>
</div>
</transition-group>
<div class="d-flex justify-start">
<v-btn color="teal-darken-4" size="small" @click="addAnswer(index)"
>Добавить ответ</v-btn
>
</div>
</div>
<div v-else>
<div class="v-row">
<v-radio-group
v-model="question.settings.onlyDep"
class="text-caption"
inline
>
<template v-slot:label>
<div class="text-body-2 font-weight-bold">
Голосование по отделам:
</div>
</template>
<v-radio v-for="x in onlyDepdict" :key="x.name" :value="x.value">
<template v-slot:label>
<div class="text-caption">{{ x.name }}</div>
</template>
</v-radio>
</v-radio-group>
</div>
<v-autocomplete
v-model="question.selectedEmployees"
:items="allEmployeesgroupedbydep"
class="text-no-wrap"
density="compact"
item-title="fio"
item-value="id"
label="Выберите сотрудников"
multiple
variant="outlined"
@update:model-value="
(selected) =>
(question.answers = question.answers.filter((e) =>
selected.includes(e.text),
))
"
>
<template #prepend-item>
<v-checkbox
:model-value="
question.selectedEmployees.length === allEmployees.length
"
class="pa-0"
label="Все"
@update:model-value="
handleSelectAll(
question.selectedEmployees.length === allEmployees.length,
index,
)
"
></v-checkbox>
</template>
<template #selection="{ item }">
<v-chip
:prepend-avatar="checkremote(`${spravURL}${item.raw.login}.jpg`)"
>
{{ item.raw.familyname }}
</v-chip>
</template>
<template #item="data">
<template v-if="typeof data.item.raw !== 'object'">
<v-divider />
<v-list-group
class="pa-4 text-overline text-grey"
v-text="data.item.raw"
></v-list-group>
</template>
<template v-else>
<v-list-item
:append-avatar="
checkremote(`${spravURL}${data.item.raw.login}.jpg`)
"
density="compact"
slim
@click="onEmployeesChange(data.item.raw.id, index)"
>
<template #prepend>
<v-checkbox
:model-value="
question.selectedEmployees.includes(data.item.raw.id)
"
density="compact"
hide-details
></v-checkbox>
</template>
<v-list-item-title
class="text-third"
v-text="data.item.raw.fio"
></v-list-item-title>
</v-list-item>
</template>
</template>
</v-autocomplete>
<transition-group name="list" tag="div">
<div
v-for="(person, ind) in question.answers"
:key="person"
class="v-row align-center mb-4"
>
<v-col class="py-0" cols="7">
<v-chip
:prepend-avatar="
checkremote(
`${spravURL}${findEmployeeById(person.text)?.login}.jpg`,
)
"
:text="findEmployeeById(person.text)?.fio"
>
</v-chip>
</v-col>
<v-col class="py-0 d-flex" cols="5">
<v-file-input
v-model="person.image"
accept="image/*"
dense
density="compact"
hide-details
label="Картинка"
variant="outlined"
></v-file-input>
<v-btn
class="ms-1"
color="error"
icon="mdi-delete"
size="small"
@click="
question.selectedEmployees.splice(ind, 1);
removeAnswer(index, ind);
"
>
</v-btn>
</v-col>
</div>
</transition-group>
</div>
</v-card-text>
</div>
</transition-group>
<NewQuestion
:form="form"
:rules="rules"
:quests="quests"
@update:form="val => form.value = val"
:allEmployees="allEmployees"
:allEmployeesgroupedbydep="allEmployeesgroupedbydep"
:handleSelectAll="handleSelectAll"
:onEmployeesChange="onEmployeesChange"
:findEmployeeById="findEmployeeById"
:onlyDepdict="onlyDepdict"
/>
<v-divider class="ma-2 pa-1"></v-divider>
<div class="d-flex justify-center">
<v-btn color="orange-darken-4" size="small" @click="addQuestion"
>Добавить вопрос</v-btn
>Добавить вопрос
</v-btn
>
</div>
</v-card-text>
@@ -377,7 +64,6 @@
<v-alert v-if="errorMessage" class="mb-4" type="error">
{{ errorMessage }}
</v-alert>
<p>
<v-icon
:class="form.title === null || form.title.length === 0 ? '' : 'opacity-0'"
@@ -454,7 +140,7 @@
<v-card-actions>
<v-btn color="teal-darken-4" size="medium" @click="step -= 1">Назад</v-btn>
<v-btn
:disabled="errorMessage != ''"
:disabled="errorMessage !== ''"
color="teal-darken-4"
size="medium"
@click="saveVoting"
@@ -473,12 +159,14 @@
<script>
import { computed, ref, watch } from 'vue';
import { createVote, getEmployees, spravURL } from '@/utils/axios-api';
import { createVote, getEmployees } from '@/utils/axios-api';
import DateSelect from '@/components/UI/date-select.vue';
import { checkremote } from '@/utils/universal_functions';
import MainInfo from '@/components/vote/ConstructorVote/MainInfo.vue';
import CreatedVote from '@/components/vote/ConstructorVote/CreatedVote.vue';
import NewQuestion from '@/components/vote/ConstructorVote/NewQuestion.vue';
export default {
components: { DateSelect },
components: { NewQuestion, CreatedVote, MainInfo, DateSelect },
emits: ['updating'],
setup(props, { emit }) {
const step = ref(0);
@@ -497,10 +185,6 @@ export default {
};
const currentQuestionIndex = ref(0);
const questionTypes = [
{ name: 'Ввести вручную', choice: 'free' },
{ name: 'Выбрать сотрудника', choice: 'employee' },
];
const groupedEmployees = ref([]);
const allEmployees = computed(() => [...groupedEmployees.value]);
@@ -560,10 +244,6 @@ export default {
}
};
const updateQuest = (index) => {
quests[index] = form.value.questions[index].text.length > 0;
};
const addAnswer = (index) => {
if (currentQuestion.value) {
form.value.questions[index].answers.push({ text: '', image: null });
@@ -601,12 +281,11 @@ export default {
}
try {
const response = await createVote(form.value);
console.log('Голосование успешно создано:', response);
await createVote(form.value);
isVoteCreated.value = true;
emit('updating', 'active');
} catch (error) {
console.error('Ошибка при создании голосования:', error);
return new Error(error)
}
};
@@ -622,20 +301,6 @@ export default {
isVoteCreated.value = false;
};
const timeOptions = Array.from({ length: 48 }, (_, i) => {
const hours = String(Math.floor(i / 2)).padStart(2, '0');
const minutes = i % 2 === 0 ? '00' : '30';
return `${hours}:${minutes}`;
});
const selectedTimeStart = ref(
`${(form.value.startDate.getHours() + 1).toString().padStart(2, '0')}:00`,
);
const selectedTimeEnd = ref(
`${(form.value.endDate.getHours() + 1).toString().padStart(2, '0')}:00`,
);
watch(
form.value,
() => {
@@ -651,7 +316,6 @@ export default {
watch(
() => form.value.questions,
(newQuestions) => {
// Обновляем quests
quests.value = newQuestions.map((q) => !!q.text.trim());
// Проверяем, есть ли пустые вопросы или невалидные ответы
@@ -666,28 +330,6 @@ export default {
{ deep: true },
);
watch([selectedTimeStart, selectedTimeEnd], ([newStart, newEnd]) => {
updateStartDate();
updateEndDate();
});
const updateStartDate = () => {
const [hours, minutes] = selectedTimeStart.value.split(':').map(Number);
form.value.startDate.setHours(hours, minutes);
};
const updateEndDate = () => {
const [hours, minutes] = selectedTimeEnd.value.split(':').map(Number);
form.value.endDate.setHours(hours, minutes);
};
const returnstart = (dates) => {
form.value.startDate = dates;
};
const returnend = (dates) => {
form.value.endDate = dates;
};
const onEmployeesChange = (selected, index) => {
let ind = form.value.questions[index].selectedEmployees.findIndex((e) => e === selected);
if (ind > -1) {
@@ -713,15 +355,11 @@ export default {
return {
quests,
updateQuest,
onlyDepdict,
allEmployeesgroupedbydep,
onEmployeesChange,
returnend,
returnstart,
step,
form,
questionTypes,
addQuestion,
removeQuestion,
addAnswer,
@@ -732,14 +370,9 @@ export default {
resetForm,
rules,
errorMessage,
timeOptions,
selectedTimeStart,
selectedTimeEnd,
allEmployees,
handleSelectAll,
isAllSelected,
checkremote,
spravURL,
};
},
};