Files
PORTAL_COD2/src/front/src/views/VoteHomePage.vue
T
2025-01-30 13:30:36 +03:00

228 lines
6.9 KiB
Vue

<template>
<v-container class="pt-2 d-flex flex-column" fluid>
<v-row class="flex-column">
<v-col cols="auto">
<v-tabs
v-model="currentTab"
align="center">
<v-tab v-for="x in tabs" :key="x.value" :value="x.value">{{ x.text }}</v-tab>
<v-tab v-if="accountStore.user.is_staff" value="constructor">Создать новое голосование</v-tab>
</v-tabs>
</v-col>
<v-col class="pa-0 overflow-hidden">
<v-window v-model="currentTab" class="h-100">
<!-- Текущие голосования -->
<v-tabs-window-item v-for="x in ['active','completed']" :value="x" class="h-100 overflow-auto">
<v-row v-if="sortedVotes.length > 0" class="mx-2">
<v-col
v-for="voting in sortedVotes"
:key="voting.id"
cols="12"
lg="3"
md="4"
>
<VoteCardVue
:isFinished="currentTab==='active'"
:voteCard="voting"
@deleteVoting="deleteVoting"
@openResultModal="(voting) => openModal('results', voting)"
@openVoteModal="(voting) => openModal('vote', voting)"
/>
</v-col>
</v-row>
<NoneVote v-else :current="currentTab == 'active' ? 'Текущих' : 'Завершенных'"/>
</v-tabs-window-item>
<!-- Конструктор -->
<v-tabs-window-item v-if="accountStore.user.is_staff" class="h-100 overflow-auto" value="constructor">
<VoteConstructor @updating="updating" ref="constructor"/>
</v-tabs-window-item>
</v-window>
<!-- Модальные окна -->
<v-dialog v-model="voteModal" @click:outside="closeModal">
<CurrentVote :data="res" @closer="closeModal"/>
</v-dialog>
<v-dialog v-model="resultsModal" max-width="1200px" @click:outside="closeModal">
<v-card>
<VoteResult :data="result" @closer="closeModal" />
</v-card>
</v-dialog>
</v-col>
</v-row>
</v-container>
</template>
<script>
import {computed, ref, 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 NoneVote from "@/components/vote/NoneVote.vue"
import {useRoute, useRouter} from "vue-router";
import VoteResult from "@/components/vote/VoteResult.vue";
import {deleteVote, getVoteDetail, getVoteResult, getVotes} from "@/utils/axios-api";
// import {formatDate} from "@vueuse/shared";
import { useAccountStore } from "@/store/AccountStore";
import {pagecounter} from '@/utils/universal_functions'
export default {
components: {CurrentVote, VoteConstructor, VoteCardVue, VoteResult, NoneVote},
setup() {
pagecounter()
const accountStore = useAccountStore();
const tabs = [
{value: "active", text: 'Текущие голосования'},
{value: "completed", text: 'Завершенные голосования'},
]
const currentTab = ref("active");
const votes = ref([]);
const router = useRouter();
const route = useRoute();
const res = ref(null);
const result = ref(null);
const constructor = ref(null)
const updating = async (param) => {
const data = await getVotes(param);
votes.value = data;
};
updating("active");
const voteModal = ref(false);
const resultsModal = ref(false);
const selectedVoting = ref(null);
const sortedVotes = computed(() => currentTab.value === 'active' ? votes.value.sort((a, b) =>
new Date(a.dateend) - new Date(b.dateend)) :
votes.value.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 = {};
throw error;
}
};
const loadVoteResult = async (id) => {
try {
const data = await getVoteResult(id);
result.value = data;
} catch (error) {
console.error("Ошибка при загрузке данных голосования:", error);
res.value = {};
}
};
const closeModal = () => {
voteModal.value = false;
resultsModal.value = false;
selectedVoting.value = null;
router.push({name: "VoteHomePage"});
if (route.name === "Voting") updating('active')
if (route.name === "VoteResults") updating('completed')
};
const openModal = async (type, voting) => {
selectedVoting.value = voting;
if (type === "vote") {
try{
await loadVoteDetails(voting.id);
console.log("sd;lkjs;ljlmnjlk");
voteModal.value = true;
router.push({name: "Voting", params: {id: voting.id}});
}catch(e){
router.push({ name: "VoteHomePage" })
}
} else if (type === "results") {
await loadVoteResult(voting.id);
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(currentTab, async () => {
if (currentTab.value !== 'constructor') {
await updating(currentTab.value)
console.log(constructor.value, '!!!');
if (constructor.value !== null) {
constructor.value.resetForm()
}
}
})
watch(
() => route,
async () => {
if (!voteModal.value && !resultsModal.value) {
const voting = votes.value.find((v) => v.id === route.params.id);
if (route.name === "Voting") {
try{
await loadVoteDetails(route.params.id);
voteModal.value = true;}
catch(e){
router.push({ name: "VoteHomePage" })
}
}
// if (route.name === "EditVote") editVoteModal.value = true;
if (route.name === "VoteResults") {
await loadVoteResult(route.params.id)
resultsModal.value = true;
}
selectedVoting.value = voting || null;
}
},
{immediate: true}
);
updating("active");
return {
accountStore,
currentTab,
votes,
sortedVotes,
voteModal,
resultsModal,
selectedVoting,
openModal,
closeModal,
deleteVoting,
updating,
res,
tabs,
result,
constructor
};
},
};
</script>
<style>
.voted-card {
opacity: 0.6;
}
</style>