|
1 | 1 | <script async setup lang="ts"> |
| 2 | +import { ref, computed, watch } from 'vue'; |
| 3 | +import { storeToRefs } from 'pinia'; |
2 | 4 | import { router } from '@/router'; |
3 | | -import { ref, Ref } from 'vue'; |
4 | | -import apiClient from '@/api'; |
5 | 5 | import { useProfileStore } from '@/store'; |
6 | | -import Placeholder from '@/assets/profile_image_placeholder.webp'; |
| 6 | +import { useSearchStore } from '@/store/searchStore'; |
| 7 | +import { useLecturerStore } from '@/store/lecturerStore'; |
| 8 | +import { useMainPageStateStore } from '@/store/mainPageStateStore'; // Обновленный импорт |
| 9 | +import { Order, OrderFromText, Subject } from '@/models'; |
| 10 | +
|
7 | 11 | import TheSearchBar from '@/components/TheSearchBar.vue'; |
8 | 12 | import TheLecturerSearchCard from '@/components/TheLecturerSearchCard.vue'; |
9 | | -import { Lecturer, Order, OrderFromText, Subject } from '@/models'; |
10 | | -import { getPhoto } from '@/utils'; |
11 | | -import { useSearchStore } from '@/store/searchStore'; |
| 13 | +import TheLecturerSearchTable from '@/components/TheLecturerSearchTable.vue'; |
12 | 14 |
|
13 | | -// const |
14 | 15 | const profileStore = useProfileStore(); |
15 | 16 | const searchStore = useSearchStore(); |
16 | | -const userAdmin = ref<boolean>(false); |
17 | | -userAdmin.value = profileStore.isAdmin(); |
18 | | -const itemsPerPage = 10; |
| 17 | +const lecturerStore = useLecturerStore(); |
| 18 | +const mainPageStateStore = useMainPageStateStore(); // Обновленная инициализация |
| 19 | +
|
| 20 | +const { lecturers, lecturersPhotos, totalPages } = storeToRefs(lecturerStore); |
| 21 | +const { isCompactView } = storeToRefs(mainPageStateStore); // Получение состояния из нового стора |
19 | 22 |
|
20 | | -// utils |
21 | | -const totalPages: Ref<number> = ref(1); |
| 23 | +const userAdmin = computed(() => profileStore.isAdmin()); |
| 24 | +const itemsPerPage = 10; |
22 | 25 |
|
23 | | -// search state |
| 26 | +// Параметры поиска |
24 | 27 | const name = ref(searchStore.name); |
25 | | -const subject: Ref<Subject> = ref(searchStore.subject); |
| 28 | +const subject = ref<Subject>(searchStore.subject); |
26 | 29 | const order = ref(searchStore.order || 'по релевантности'); |
27 | | -const orderValues = ref(OrderFromText[order.value as keyof typeof OrderFromText] as Order); |
28 | 30 | const ascending = ref(searchStore.ascending); |
29 | 31 | const page = ref(searchStore.page); |
30 | 32 |
|
31 | | -const lecturers: Ref<Lecturer[] | undefined> = ref(); |
32 | | -const lecturersPhotos = ref<string[]>(Array<string>(itemsPerPage)); |
33 | | -
|
34 | | -await loadLecturers(); |
35 | | -
|
36 | | -function toLecturerPage(id: number) { |
37 | | - searchStore.setParams(name.value, subject.value, order.value, ascending.value, page.value); |
38 | | - router.push({ path: 'lecturer', query: { lecturer_id: id } }); |
39 | | -} |
40 | | -
|
41 | | -async function loadLecturers() { |
42 | | - const offset = (page.value - 1) * itemsPerPage; |
43 | | - const res = await apiClient.GET('/rating/lecturer', { |
44 | | - params: { |
45 | | - query: { |
46 | | - limit: itemsPerPage, |
47 | | - name: name.value, |
48 | | - offset, |
49 | | - info: ['comments', 'mark'], |
50 | | - subject: subject.value, |
51 | | - order_by: orderValues.value, |
52 | | - asc_order: ascending.value, |
53 | | - }, |
54 | | - }, |
| 33 | +// Вычисляем порядковые номера для компактного режима |
| 34 | +const lecturerRatings = computed(() => { |
| 35 | + if (!lecturers.value) return []; |
| 36 | + return lecturers.value.map((_, idx) => (page.value - 1) * itemsPerPage + idx + 1); |
| 37 | +}); |
| 38 | +
|
| 39 | +async function updateLecturersList() { |
| 40 | + await lecturerStore.fetchLecturers({ |
| 41 | + page: page.value, |
| 42 | + itemsPerPage, |
| 43 | + name: name.value, |
| 44 | + subject: subject.value, |
| 45 | + orderBy: OrderFromText[order.value as keyof typeof OrderFromText] as Order, |
| 46 | + ascending: ascending.value, |
55 | 47 | }); |
56 | | - lecturers.value = res.data?.lecturers; |
57 | | - totalPages.value = res.data?.total ? Math.ceil(res.data?.total / itemsPerPage) : 1; |
58 | | - loadPhotos(); |
59 | 48 | } |
60 | 49 |
|
61 | | -function loadPhotos() { |
62 | | - lecturersPhotos.value = lecturers.value?.map(item => getPhoto(item.avatar_link)) ?? [Placeholder]; |
63 | | -} |
| 50 | +// Загрузка при первой загрузке |
| 51 | +await updateLecturersList(); |
64 | 52 |
|
65 | | -async function findLecturer() { |
| 53 | +// Обработчики изменений |
| 54 | +async function onSearchParamChange() { |
66 | 55 | page.value = 1; |
67 | | - await loadLecturers(); |
| 56 | + await updateLecturersList(); |
68 | 57 | } |
69 | 58 |
|
70 | | -async function orderLecturers() { |
71 | | - page.value = 1; |
72 | | - orderValues.value = OrderFromText[order.value as keyof typeof OrderFromText] as Order; |
73 | | - await loadLecturers(); |
74 | | -} |
| 59 | +watch(page, updateLecturersList); |
75 | 60 |
|
76 | | -async function filterLecturers() { |
77 | | - page.value = 1; |
78 | | - await loadLecturers(); |
| 61 | +function toLecturerPage(id: number) { |
| 62 | + searchStore.setParams(name.value, subject.value, order.value, ascending.value, page.value); |
| 63 | + router.push({ path: 'lecturer', query: { lecturer_id: id } }); |
79 | 64 | } |
80 | 65 |
|
81 | | -async function changeAscOrder() { |
82 | | - page.value = 1; |
83 | | - ascending.value = !ascending.value; |
84 | | - await loadLecturers(); |
| 66 | +function toggleViewMode() { |
| 67 | + mainPageStateStore.toggleCompactView(); // Используем метод из нового стора |
85 | 68 | } |
86 | 69 | </script> |
87 | 70 |
|
88 | 71 | <template> |
89 | 72 | <v-container class="ma-0 py-2"> |
90 | | - <v-data-iterator :items="lecturers" :items-per-page="itemsPerPage"> |
91 | | - <template #header> |
92 | | - <TheSearchBar |
93 | | - v-model:search-query="name" |
94 | | - v-model:subject="subject" |
95 | | - v-model:order="order" |
96 | | - :is-admin="userAdmin" |
97 | | - :ascending="ascending" |
98 | | - :page="page" |
99 | | - @update:subject="filterLecturers" |
100 | | - @update:order="orderLecturers" |
101 | | - @update:search-query="findLecturer" |
102 | | - @changed-asc-desc="changeAscOrder" |
103 | | - /> |
104 | | - </template> |
105 | | - |
106 | | - <template #default="{ items }"> |
107 | | - <TheLecturerSearchCard |
108 | | - v-for="(item, idx) in items" |
109 | | - :key="idx" |
110 | | - :lecturer="item.raw" |
111 | | - :photo="lecturersPhotos[idx]" |
112 | | - :rating="(page - 1) * itemsPerPage + idx + 1" |
113 | | - class="py-0" |
114 | | - variant="elevated" |
115 | | - @click="toLecturerPage(item.raw.id)" |
116 | | - /> |
117 | | - </template> |
118 | | - |
119 | | - <template #no-data> |
120 | | - <div class="ma-2">Ничего не нашли :(</div> |
121 | | - </template> |
122 | | - |
123 | | - <template #footer> |
124 | | - <div v-if="lecturers && totalPages > 1"> |
125 | | - <v-pagination |
126 | | - v-model="page" |
127 | | - active-color="primary" |
| 73 | + <TheSearchBar |
| 74 | + v-model:search-query="name" |
| 75 | + v-model:subject="subject" |
| 76 | + v-model:order="order" |
| 77 | + :is-admin="userAdmin" |
| 78 | + :ascending="ascending" |
| 79 | + :page="page" |
| 80 | + @update:subject="onSearchParamChange" |
| 81 | + @update:order="onSearchParamChange" |
| 82 | + @update:search-query="onSearchParamChange" |
| 83 | + @changed-asc-desc=" |
| 84 | + () => { |
| 85 | + ascending = !ascending; |
| 86 | + onSearchParamChange(); |
| 87 | + } |
| 88 | + " |
| 89 | + /> |
| 90 | + |
| 91 | + <div v-if="!isCompactView"> |
| 92 | + <v-data-iterator :items="lecturers" :items-per-page="itemsPerPage"> |
| 93 | + <template #default="{ items }"> |
| 94 | + <TheLecturerSearchCard |
| 95 | + v-for="(item, idx) in items" |
| 96 | + :key="idx" |
| 97 | + :lecturer="item.raw" |
| 98 | + :photo="lecturersPhotos[idx]" |
| 99 | + :rating="(page - 1) * itemsPerPage + idx + 1" |
| 100 | + class="py-0" |
128 | 101 | variant="elevated" |
129 | | - :length="totalPages" |
130 | | - :total-visible="1" |
131 | | - :show-first-last-page="true" |
132 | | - ellipsis="" |
133 | | - @update:model-value="loadLecturers" |
| 102 | + @click="toLecturerPage(item.raw.id)" |
134 | 103 | /> |
135 | | - </div> |
136 | | - </template> |
137 | | - </v-data-iterator> |
| 104 | + </template> |
| 105 | + <template #no-data> |
| 106 | + <div class="ma-2">Ничего не нашли :(</div> |
| 107 | + </template> |
| 108 | + </v-data-iterator> |
| 109 | + </div> |
| 110 | + |
| 111 | + <div v-else> |
| 112 | + <TheLecturerSearchTable |
| 113 | + v-if="lecturers && lecturers.length > 0" |
| 114 | + :lecturers="lecturers" |
| 115 | + :ratings="lecturerRatings" |
| 116 | + @lecturer-click="toLecturerPage" |
| 117 | + /> |
| 118 | + <div v-else class="ma-2">Ничего не нашли :(</div> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div v-if="lecturers && totalPages > 1" class="d-flex align-center justify-center mt-4"> |
| 122 | + <v-btn icon class="mr-2" :title="isCompactView ? 'Обычный вид' : 'Компактный вид'" @click="toggleViewMode"> |
| 123 | + <v-icon>{{ isCompactView ? 'mdi-view-agenda' : 'mdi-table' }}</v-icon> |
| 124 | + </v-btn> |
| 125 | + |
| 126 | + <v-pagination |
| 127 | + v-model="page" |
| 128 | + active-color="primary" |
| 129 | + variant="elevated" |
| 130 | + :length="totalPages" |
| 131 | + :total-visible="1" |
| 132 | + :show-first-last-page="true" |
| 133 | + ellipsis="" |
| 134 | + /> |
| 135 | + </div> |
138 | 136 | </v-container> |
139 | 137 | </template> |
0 commit comments