All files / src/views VoteHomePage.vue

0% Statements 0/92
0% Branches 0/22
0% Functions 0/46
0% Lines 0/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
<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>