@@ -4,18 +4,22 @@ import { computed, ref } from 'vue'
4
4
import { useCollectionAggregator } from ' ../composables/useCollectionAggregator'
5
5
import config from ' ~/utils/searchConfig'
6
6
import normalizeTitleForAlphabeticalBrowse from ' ~/utils/normalizeTitleForAlphabeticalBrowseBy'
7
+ import useMobileOnlyInfiniteScroll from ' @/composables/useMobileOnlyInfiniteScroll'
7
8
8
9
const attrs = useAttrs () as { page? : { title: string , ftvaFilters: string [], ftvaHomepageDescription: string , titleBrowse: string , groupName: string } }
9
10
10
11
const route = useRoute ()
11
12
const router = useRouter ()
13
+
12
14
defineOptions ({
13
15
inheritAttrs: false
14
16
})
17
+
15
18
interface AggregationBucket {
16
19
key: string
17
20
doc_count: number
18
21
}
22
+
19
23
interface Aggregations {
20
24
[key : string ]: { buckets: AggregationBucket [] }
21
25
}
@@ -37,20 +41,51 @@ if (attrs.page && import.meta.prerender) {
37
41
}
38
42
39
43
// "STATE"
40
- const currentPage = ref (1 )
41
44
const documentsPerPage = 15 // show 15 search results at a time
42
- const totalPages = ref (0 )
43
45
const totalResults = ref (0 )
44
46
const noResultsFound = ref (false )
45
- const isLoading = ref <boolean >(false )
46
- const isMobile = ref (false )
47
- const hasMore = ref (true ) // Flag to control infinite scroll
48
47
49
- const collectionResults = ref ([])
48
+ // "STATE"
49
+ const collectionFetchFunction = async () => {
50
+ const { paginatedCollectionSearchFilters } = useListSearchFilter () // composable
51
+
52
+ const currpage = currentPage .value
53
+ const size = documentsPerPage
54
+ let results: any = {}
55
+
56
+ results = await paginatedCollectionSearchFilters (currpage , size , ' ftvaItemInCollection' , titleForSearch .value , selectedFilters .value , selectedSortFilters .value .sortField )
57
+
58
+ return results
59
+ }
60
+
61
+ const onResults = (results ) => {
62
+ if (results && results .hits && results ?.hits ?.hits ?.length > 0 ) {
63
+ const newCollectionResults = results .hits .hits || []
64
+ totalResults .value = results .hits .total .value
65
+
66
+ if (isMobile .value ) {
67
+ totalPages .value = 0
68
+ mobileItemList .value .push (... newCollectionResults )
69
+ hasMore .value = currentPage .value < Math .ceil (results .hits .total .value / documentsPerPage )
70
+ } else {
71
+ desktopItemList .value = newCollectionResults
72
+ totalPages .value = Math .ceil (results .hits .total .value / documentsPerPage )
73
+ }
74
+ noResultsFound .value = false
75
+ } else {
76
+ noResultsFound .value = true
77
+ totalPages .value = 0
78
+ hasMore .value = false
79
+ }
80
+ }
81
+
82
+ // INFINITE SCROLL
83
+ const { isLoading, isMobile, hasMore, desktopPage, desktopItemList, mobileItemList, totalPages, currentPage, currentList, scrollElem, reset, searchES } = useMobileOnlyInfiniteScroll (collectionFetchFunction , onResults )
84
+
50
85
// format search results for SectionTeaserCard
51
86
const parsedCollectionResults = computed (() => {
52
- if (collectionResults .value .length === 0 ) return []
53
- return collectionResults .value .map ((obj ) => {
87
+ if (currentList .value .length === 0 ) return []
88
+ return currentList .value .map ((obj ) => {
54
89
const objImage = obj ._source .ftvaImage .length ? obj ._source .ftvaImage [0 ] : null
55
90
return {
56
91
... obj ._source ,
@@ -78,7 +113,9 @@ const sortDropdownData = {
78
113
label: ' Sort by' ,
79
114
fieldName: ' sortField'
80
115
}
116
+
81
117
const selectedSortFilters = ref ({ sortField: ' asc' })
118
+
82
119
function updateSort(newSort ) {
83
120
router .push ({
84
121
path: route .path ,
@@ -102,7 +139,9 @@ function parseESConfigFilters(configFilters, ftvaFiltersArg) {
102
139
}
103
140
return parsedfilters
104
141
}
142
+
105
143
const searchFilters = ref ([])
144
+
106
145
function parseAggRes(response : Aggregations ) {
107
146
// console.log('parseAggRes response', response)
108
147
const filters = (Object .entries (response ) || []).map (([key , value ]) => ({
@@ -128,6 +167,7 @@ function parseAggRes(response: Aggregations) {
128
167
129
168
return filters
130
169
}
170
+
131
171
// fetch filters for the page from ES after page loads in Onmounted hook on the client side
132
172
async function setFilters() {
133
173
const parsedESConfigFiltersRes = parseESConfigFilters (config .collection .filters , ftvaFilters .value )
@@ -142,12 +182,14 @@ async function setFilters() {
142
182
searchAggsResponse
143
183
)
144
184
}
185
+
145
186
const selectedFilters = ref ({}) // initialise with empty filter
146
187
// Object w key filter label and value ESFieldName for selected filter lookup
147
188
const fieldNamefromLabel = {
148
189
' Filter by Topic' : ' ftvaCollectionGroup.title.keyword' ,
149
190
' Filter by Season' : ' episodeSeason.keyword'
150
191
}
192
+
151
193
function updateFilters(newFilter ) {
152
194
const newFilterValue = Object .values (newFilter )[0 ]
153
195
if (newFilterValue === ' (none selected)' ) {
@@ -169,6 +211,7 @@ function updateFilters(newFilter) {
169
211
})
170
212
}
171
213
}
214
+
172
215
const titleForSearch = computed (() => {
173
216
// TODO: get the title from ES for the slug `in-the-life or la-rebellion`
174
217
if (route .path .endsWith (' filmography' )) {
@@ -180,41 +223,6 @@ const titleForSearch = computed(() => {
180
223
}
181
224
})
182
225
183
- // ELASTIC SEARCH FUNCTION
184
- async function searchES() {
185
- if (isLoading .value || ! hasMore .value ) return
186
-
187
- isLoading .value = true
188
-
189
- try {
190
- const currpage = currentPage .value
191
- const size = documentsPerPage
192
- let results: any = {}
193
-
194
- const { paginatedCollectionSearchFilters } = useListSearchFilter ()
195
-
196
- results = await paginatedCollectionSearchFilters (currpage , size , ' ftvaItemInCollection' , titleForSearch .value , selectedFilters .value , selectedSortFilters .value .sortField )
197
- if (results && results .hits && results .hits .hits .length > 0 ) {
198
- const newCollectionResults = results .hits .hits || []
199
-
200
- collectionResults .value = newCollectionResults
201
- totalResults .value = results .hits .total .value
202
- totalPages .value = Math .ceil (results .hits .total .value / documentsPerPage )
203
-
204
- noResultsFound .value = false
205
- } else {
206
- noResultsFound .value = true
207
- hasMore .value = false
208
- }
209
- } catch (error ) {
210
- // eslint-disable-next-line no-console
211
- console .error (' Error fetching data:' , error )
212
- hasMore .value = false
213
- } finally {
214
- isLoading .value = false
215
- }
216
- }
217
-
218
226
// WATCHERS
219
227
// This watcher is called when router pushes updates the query params
220
228
watch (
0 commit comments