/
DrawerViewPage.vue
286 lines (260 loc) · 9.21 KB
/
DrawerViewPage.vue
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<template>
<DefaultLayout class="drawer-view-page">
<div class="px-4">
<header class="my-8">
<Link
:to="`/drawers/listDrawers`"
class="flex items-center gap-1 mb-4 hover:no-underline"
>
<ArrowForwardIcon class="transform rotate-180 h-4 w-4" />
Back to Drawers
</Link>
<h2 class="text-4xl font-bold">{{ drawerTitle }}</h2>
</header>
<Tabs
labelsClass="drawer-view-page__tabs sticky top-14 z-20 -mx-4 px-4 border-b border-neutral-200 pt-4"
:activeTabId="activeTabId"
@tabChange="handleTabChange"
>
<div
class="bg-transparent-black-50 rounded-md mb-4 sm:flex justify-between items-center p-2"
>
<div>
<ResultsCount
v-if="drawer.contents"
class="mb-2 sm:mb-0"
:total="drawer.contents.totalResults"
:fetchStatus="fetchStatus"
:showingCount="drawer.contents.matches.length"
@loadMore="handleLoadMore"
@loadAll="handleLoadAll"
/>
<div
v-else
class="flex items-center gap-2 text-neutral-500 text-sm"
>
<SpinnerIcon class="animate-spin h-5 w-5" />
Loading...
</div>
</div>
<div class="flex items-baseline gap-2">
<label for="location" class="sr-only">Sort</label>
<select
id="sort"
name="sort"
class="block w-full rounded-md border-0 py-1.5 pl-3 pr-10 text-neutral-900 ring-1 ring-inset ring-neutral-300 focus:ring-2 focus:ring-indigo-600 text-sm sm:leading-6 max-w-full bg-transparent-white-800"
:value="selectedSortOption"
@change="handleSortOptionChange"
>
<option
v-for="(sortOptionLabel, sortOptionKey) in sortOptions"
:key="sortOptionKey"
:value="sortOptionKey"
>
{{ sortOptionLabel }}
</option>
</select>
</div>
</div>
<Tab id="grid" label="Grid">
<Transition name="fade">
<DrawerItemsGrid
v-if="drawer.contents"
:totalResults="drawer.contents.totalResults"
:matches="drawer.contents.matches"
:status="fetchStatus"
:drawerId="drawerId"
:isDraggable="selectedSortOption === SORT_KEYS.CUSTOM"
@dragEnd="handleDragEnd"
/>
</Transition>
</Tab>
<Tab id="list" label="List">
<Transition name="fade">
<DrawerItemsList
v-if="drawer.contents"
:totalResults="drawer.contents.totalResults"
:matches="drawer.contents.matches"
:status="fetchStatus"
:drawerId="drawerId"
:isDraggable="selectedSortOption === SORT_KEYS.CUSTOM"
@dragEnd="handleDragEnd"
/>
</Transition>
</Tab>
<Tab id="timeline" label="Timeline">
<Transition name="fade">
<SearchResultsTimeline
v-if="drawer.contents"
:totalResults="drawer.contents.totalResults"
:matches="drawer.contents.matches"
:status="fetchStatus"
/>
</Transition>
</Tab>
<Tab id="map" label="Map">
<Transition name="fade">
<SearchResultsMap
v-if="drawer.contents"
:totalResults="drawer.contents.totalResults"
:matches="drawer.contents.matches"
:status="fetchStatus"
/>
</Transition>
</Tab>
<Tab id="gallery" label="Gallery">
<Transition name="fade">
<SearchResultsGallery
v-if="drawer.contents"
:totalResults="drawer.contents.totalResults"
:matches="drawer.contents.matches"
:status="fetchStatus"
/>
</Transition>
</Tab>
</Tabs>
</div>
</DefaultLayout>
</template>
<script setup lang="ts">
import { onMounted, ref, computed } from "vue";
import { useRouter, useRoute } from "vue-router";
import DefaultLayout from "@/layouts/DefaultLayout.vue";
import Tab from "@/components/Tabs/Tab.vue";
import Tabs from "@/components/Tabs/Tabs.vue";
import SearchResultsTimeline from "@/components/SearchResultsTimeline/SearchResultsTimeline.vue";
import SearchResultsMap from "@/components/SearchResultsMap/SearchResultsMap.vue";
import SearchResultsGallery from "@/components/SearchResultsGallery/SearchResultsGallery.vue";
import ArrowForwardIcon from "@/icons/ArrowForwardIcon.vue";
import Link from "@/components/Link/Link.vue";
import ResultsCount from "@/components/ResultsCount/ResultsCount.vue";
import {
SearchResultsView,
Tab as TabType,
FetchStatus,
SearchResultMatch,
} from "@/types";
import { SEARCH_RESULTS_VIEWS } from "@/constants/constants";
import { useDrawerStore } from "@/stores/drawerStore";
import { SORT_KEYS } from "@/constants/constants";
import { useErrorStore } from "@/stores/errorStore";
import SpinnerIcon from "@/icons/SpinnerIcon.vue";
import DrawerItemsGrid from "./DrawerItemsGrid.vue";
import DrawerItemsList from "./DrawerItemsList.vue";
const props = withDefaults(
defineProps<{
drawerId: number;
resultsView?: SearchResultsView;
}>(),
{
resultsView: "grid",
}
);
const isValidResultsView = (view: string): view is SearchResultsView => {
return SEARCH_RESULTS_VIEWS.includes(view as SearchResultsView);
};
const initialTab = isValidResultsView(props.resultsView)
? props.resultsView
: "grid";
const activeTabId = ref<SearchResultsView>(initialTab);
const fetchStatus = ref<FetchStatus>("idle");
const drawerStore = useDrawerStore();
const router = useRouter();
const route = useRoute();
const drawer = computed(() => drawerStore.getDrawerById(props.drawerId));
const sortOptions = {
[SORT_KEYS.TITLE]: "Default Title",
[SORT_KEYS.CUSTOM]: "Custom",
};
const selectedSortOption = ref<keyof typeof sortOptions>(SORT_KEYS.TITLE);
const drawerTitle = computed(() => {
return drawerStore.drawerRecords[props.drawerId]?.title;
});
type SearchViewTab = TabType & { id: SearchResultsView };
type supportedSortOption = keyof typeof sortOptions;
const isValidTab = (tab: TabType): tab is SearchViewTab => {
return SEARCH_RESULTS_VIEWS.includes(tab.id as SearchResultsView);
};
function handleTabChange(tab: TabType) {
if (!isValidTab(tab)) throw new Error(`Invalid tab id: ${tab.id}`);
activeTabId.value = tab.id;
// update the url query param to match state
router.replace({
query: {
...route.query,
resultsView: tab.id,
},
});
}
function handleDragEnd(updatedListOfItems: SearchResultMatch[]) {
drawerStore.setDrawerItems(props.drawerId, updatedListOfItems);
}
function handleLoadMore() {
console.log("load more");
}
function handleLoadAll() {
console.log("load all");
}
const isValidSortOption = (sort: unknown): sort is supportedSortOption =>
typeof sort === "string" && sort in sortOptions;
function handleSortOptionChange(event: Event) {
const sortOption = (event.target as HTMLSelectElement).value;
if (!isValidSortOption(sortOption)) {
throw new Error(`Invalid sort option: ${sortOption}`);
}
selectedSortOption.value = sortOption;
setSortQueryParam(sortOption);
drawerStore.setDrawerSortBy(props.drawerId, sortOption);
}
function setSortQueryParam(sortOption: supportedSortOption) {
router.replace({
query: {
...route.query,
sort: sortOption,
},
});
}
const errorStore = useErrorStore();
onMounted(async () => {
fetchStatus.value = "fetching";
// get current drawer contents
await drawerStore.refreshDrawer(props.drawerId);
// sortBy is persisted in the drawer record, so check its current value
const drawerRecord = drawerStore.drawerRecords[props.drawerId];
if (!drawerRecord.contents) {
fetchStatus.value = "error";
return errorStore.setError(new Error("Couldn't find drawer content."));
}
// first check the query param for sort.
// If it's invalid or empty, just set it to match the drawer record
if (!route.query.sort || !isValidSortOption(route.query.sort)) {
selectedSortOption.value = drawerRecord.contents.sortBy ?? SORT_KEYS.TITLE;
setSortQueryParam(selectedSortOption.value);
fetchStatus.value = "success";
return;
}
// if the query param is set, valid, and matches the drawer record
// then just set the initial selected sort option
if (route.query.sort === drawerRecord.contents.sortBy) {
selectedSortOption.value = route.query.sort;
fetchStatus.value = "success";
return;
}
// finally, if the query param is set, valid, but
// doesn't match the drawer record, then set the sortby option
// and refresh the drawer contents
selectedSortOption.value = route.query.sort;
await drawerStore.setDrawerSortBy(props.drawerId, route.query.sort);
fetchStatus.value = "success";
});
</script>
<style scoped></style>
<style>
.drawer-view-page__tabs {
background: var(--app-backgroundColor);
}
.drawer-view-page .app-header {
z-index: 30; /* keep app header dropdowns above tabs */
border-bottom-color: transparent !important;
}
</style>