/
VLoadMore.vue
161 lines (143 loc) · 4.17 KB
/
VLoadMore.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
<template>
<div ref="loadMoreSectionRef" class="w-full">
<VButton
v-show="canLoadMore"
class="label-bold lg:description-bold h-16 w-full lg:h-18"
variant="filled-gray"
size="disabled"
:disabled="isFetching"
data-testid="load-more"
@click="onLoadMore"
>
{{ buttonLabel }}
</VButton>
</div>
</template>
<script lang="ts">
import {
computed,
defineComponent,
onMounted,
type PropType,
ref,
watch,
} from "vue"
import { storeToRefs } from "pinia"
import { useElementVisibility } from "@vueuse/core"
import { useRoute } from "@nuxtjs/composition-api"
import { useAnalytics } from "~/composables/use-analytics"
import { useMediaStore } from "~/stores/media"
import { useSearchStore } from "~/stores/search"
import { useI18n } from "~/composables/use-i18n"
import { defineEvent } from "~/types/emits"
import type { ResultKind } from "~/types/result"
import type { SupportedSearchType } from "~/constants/media"
import VButton from "~/components/VButton.vue"
export default defineComponent({
name: "VLoadMore",
components: {
VButton,
},
props: {
searchType: {
type: String as PropType<SupportedSearchType>,
required: true,
},
searchTerm: {
type: String,
required: true,
},
kind: {
type: String as PropType<ResultKind>,
required: true,
},
isFetching: {
type: Boolean,
required: true,
},
},
emits: {
"load-more": defineEvent(),
},
setup(props, { emit }) {
const loadMoreSectionRef = ref(null)
const route = useRoute()
const i18n = useI18n()
const mediaStore = useMediaStore()
const searchStore = useSearchStore()
const { sendCustomEvent } = useAnalytics()
const { currentPage } = storeToRefs(mediaStore)
/**
* Whether we should show the "Load more" button.
* If the user has entered a search term, there is at least 1 page of results,
* there has been no fetching error, and there are more results to fetch,
* we show the button.
*/
const canLoadMore = computed(
() => mediaStore.canLoadMore && searchStore.searchStarted
)
const reachResultEndEventSent = ref(false)
/**
* On button click, fetch media, persisting the existing results.
* The button is disabled when we are fetching, but we still check
* whether we are currently fetching to be sure we don't fetch multiple times.
*
*/
const onLoadMore = async () => {
if (props.isFetching) {
return
}
reachResultEndEventSent.value = false
sendCustomEvent("LOAD_MORE_RESULTS", eventPayload.value)
emit("load-more")
}
const eventPayload = computed(() => {
return {
searchType: props.searchType,
query: props.searchTerm,
resultPage: currentPage.value || 1,
}
})
const sendReachResultEnd = () => {
// This function can be called before the media is fetched and
// currentPage is updated from 0, so we use the value or 1.
// The currentPage can never be 0 here because then the loadMore
// button would not be visible.
sendCustomEvent("REACH_RESULT_END", eventPayload.value)
}
const buttonLabel = computed(() =>
props.isFetching
? i18n.t("browsePage.loading")
: i18n.t("browsePage.load")
)
const mainPageElement = ref<HTMLElement | null>(null)
onMounted(() => {
mainPageElement.value = document.getElementById("main-page")
})
const isLoadMoreButtonVisible = useElementVisibility(loadMoreSectionRef, {
scrollTarget: mainPageElement,
})
// Reset the reachResultEndEvent whenever the route changes,
// to make sure the result end is tracked properly whenever
// the search query or content type changes
watch(route, () => {
reachResultEndEventSent.value = false
})
watch(isLoadMoreButtonVisible, (isVisible) => {
if (isVisible) {
if (reachResultEndEventSent.value) {
return
}
sendReachResultEnd()
reachResultEndEventSent.value = true
}
})
return {
buttonLabel,
onLoadMore,
canLoadMore,
loadMoreSectionRef,
}
},
})
</script>