Files
PORTAL_COD2/src/front/src/views/VoteHomePage.vue
T

244 lines
7.6 KiB
Vue

<template>
<v-container>
<v-toolbar color="transparent">
<v-toolbar-title>Голосования</v-toolbar-title>
<template v-slot:extension>
<v-tabs v-model="currentTab">
<v-tab value="current">Текущие голосования</v-tab>
<v-tab value="completed">Завершенные голосования</v-tab>
<v-tab value="constructor">Создать новое голосование</v-tab>
</v-tabs>
</template>
</v-toolbar>
<v-tabs-window v-model="currentTab">
<!-- Текущие голосования -->
<v-tabs-window-item value="current">
<v-container>
<v-row>
<v-col
v-for="voting in sortedCurrentVotes"
:key="voting.id"
cols="12"
lg="3"
md="4"
>
<VoteCardVue
:voteCard="voting"
@openVoteModal="(voting) => openModal('vote', voting)"
@openEditVoteModal="(voting) => openModal('edit', voting)"
@deleteVoting="deleteVoting"
/>
</v-col>
</v-row>
</v-container>
</v-tabs-window-item>
<!-- Завершенные голосования -->
<v-tabs-window-item value="completed">
<v-container>
<v-row>
<v-col
v-for="voting in sortedCompletedVotes"
:key="voting.id"
cols="12"
lg="3"
md="4"
:class="{ 'voted-card': voting.voted }"
>
<v-card>
<v-card-title>{{ voting.title }}</v-card-title>
<v-card-subtitle>
Завершено: {{ formatDate(voting.endDate) }}
</v-card-subtitle>
<v-card-actions class="justify-space-between">
<v-btn color="teal-darken-4" size="medium" @click="(voting) => openModal('results', voting)">
Результаты
</v-btn>
<v-btn color="error" size="medium" @click="deleteVoting(voting.id)">
<v-icon>mdi-delete</v-icon>
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</v-container>
</v-tabs-window-item>
<!-- Конструктор -->
<v-tabs-window-item value="constructor">
<VoteConstructor @updating="updating" />
</v-tabs-window-item>
</v-tabs-window>
<!-- Модальные окна -->
<v-dialog v-model="voteModal" max-width="1200px" @click:outside="closeModal">
<v-card>
<v-card-actions>
<v-btn color="red-darken-1" size="medium" @click="closeModal">
<v-icon>mdi-close-thick</v-icon>
</v-btn>
</v-card-actions>
<v-card-text class="pt-0">
<CurrentVote :data="res" />
</v-card-text>
</v-card>
</v-dialog>
{{res}}
<v-dialog v-model="editVoteModal" max-width="1200px" @click:outside="closeModal">
<v-card>
<v-card-title>Редактировать голосование: {{ selectedVoting?.title }}</v-card-title>
<v-card-text>
<VoteConstructor />
</v-card-text>
<v-card-actions>
<v-btn color="teal-darken-4">Сохранить</v-btn>
<v-btn @click="closeModal">Закрыть</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
<v-dialog v-model="resultsModal" max-width="1200px" @click:outside="closeModal">
<v-card>
<v-card-title>Результаты голосования: {{ selectedVoting?.title }}</v-card-title>
<v-card-text>
<p>Здесь отображаются результаты голосования...</p>
</v-card-text>
<v-card-actions>
<v-btn @click="closeModal">Закрыть</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-container>
</template>
<script>
import { ref, computed, watch } from "vue";
import VoteConstructor from "@/components/vote/VoteConstructor.vue";
import CurrentVote from "@/components/vote/CurrentVote.vue";
import VoteCardVue from "@/components/vote/VoteCard.vue";
import { useRoute, useRouter } from "vue-router";
import { deleteVote, getVoteDetail, getVotes } from "@/utils/axios-api";
import { formatDate } from "@vueuse/shared";
export default {
methods: { formatDate },
components: { CurrentVote, VoteConstructor, VoteCardVue },
setup() {
const currentTab = ref("current");
const votes = ref([]);
const router = useRouter();
const route = useRoute();
const res = ref(null);
const updating = async (param) => {
const data = await getVotes(param);
votes.value = data;
};
updating("active");
const voteModal = ref(false);
const editVoteModal = ref(false);
const resultsModal = ref(false);
const selectedVoting = ref(null);
const sortedCurrentVotes = computed(() =>
votes.value
.filter((v) => new Date(v.dateend) > new Date())
.sort((a, b) => new Date(a.dateend) - new Date(b.dateend))
);
const sortedCompletedVotes = computed(() =>
votes.value
.filter((v) => new Date(v.dateend) <= new Date())
.sort((a, b) => new Date(b.dateend) - new Date(a.dateend))
);
const loadVoteDetails = async (id) => {
try {
const data = await getVoteDetail(id);
res.value = data;
} catch (error) {
console.error("Ошибка при загрузке данных голосования:", error);
res.value = null;
}
};
const closeModal = () => {
voteModal.value = false;
editVoteModal.value = false;
resultsModal.value = false;
selectedVoting.value = null;
router.push({ name: "VoteHomePage" });
};
const openModal = async (type, voting) => {
selectedVoting.value = voting;
if (type === "vote") {
await loadVoteDetails(voting.id);
voteModal.value = true;
router.push({ name: "Voting", params: { id: voting.id } });
} else if (type === "edit") {
editVoteModal.value = true;
router.push({ name: "EditVote", params: { id: voting.id } });
} else if (type === "results") {
resultsModal.value = true;
router.push({ name: "VoteResults", params: { id: voting.id } });
}
};
const deleteVoting = async (id) => {
try {
await deleteVote(id);
votes.value = votes.value.filter((v) => v.id !== id);
} catch (error) {
console.error("Ошибка при удалении голосования:", error);
}
};
watch(
() => route,
async () => {
if (!voteModal.value && !editVoteModal.value && !resultsModal.value) {
const voting = votes.value.find((v) => v.id === route.params.id);
if (route.name === "Voting") {
await loadVoteDetails(route.params.id);
voteModal.value = true;
}
if (route.name === "EditVote") editVoteModal.value = true;
if (route.name === "VoteResults") resultsModal.value = true;
selectedVoting.value = voting || null;
}
},
{ immediate: true }
);
updating("active");
return {
currentTab,
votes,
sortedCurrentVotes,
sortedCompletedVotes,
voteModal,
editVoteModal,
resultsModal,
selectedVoting,
openModal,
closeModal,
deleteVoting,
updating,
res,
};
},
};
</script>
<style>
.voted-card {
opacity: 0.6;
}
</style>