Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Fix load more not showing when single media type has no results (#1786)

Olga Bulat0c294c1e034f
diff --git a/src/components/VImageDetails/VRelatedImages.vue b/src/components/VImageDetails/VRelatedImages.vue
index 89fd9e87b..f53abc7a0 100644
--- a/src/components/VImageDetails/VRelatedImages.vue
+++ b/src/components/VImageDetails/VRelatedImages.vue
@@ -6,7 +6,7 @@
<VLoadingIcon v-if="fetchState.isFetching" />
<VImageGrid
:images="media"
- :show-load-more="false"
+ :is-single-page="true"
:fetch-state="fetchState"
/>
</aside>
diff --git a/src/components/VImageGrid/VImageGrid.vue b/src/components/VImageGrid/VImageGrid.vue
index 8285c4710..19dc6b2f7 100644
--- a/src/components/VImageGrid/VImageGrid.vue
+++ b/src/components/VImageGrid/VImageGrid.vue
@@ -11,7 +11,7 @@
<h5 v-if="isError && !fetchState.isFinished" class="py-4">
{{ fetchState.fetchingError }}
</h5>
- <footer v-if="showLoadMore" class="pt-4">
+ <footer v-if="!isSinglePage" class="pt-4">
<VLoadMore />
</footer>
</section>
@@ -44,12 +44,14 @@ export default defineComponent({
default: () => [],
},
/**
- * Whether to show the 'Load More' button.
- * Is false for related images
+ * VImageGrid is used for the search grid and the related images.
+ * In the related images, it is just a single page of results without the
+ * "Load More" button, and in the search grid it is a grid that can load
+ * more images on the "Load More" button click.
*/
- showLoadMore: {
+ isSinglePage: {
type: Boolean,
- default: true,
+ required: true,
},
fetchState: {
type: Object as PropType<FetchState>,
@@ -57,16 +59,11 @@ export default defineComponent({
},
},
emits: {
- 'load-more': defineEvent(),
'shift-tab': defineEvent(),
},
setup(props, { emit }) {
const isError = computed(() => Boolean(props.fetchState.fetchingError))
- const onLoadMore = () => {
- emit('load-more')
- }
-
const handleShiftTab = (event: KeyboardEvent, index: number) => {
if (index === 0) {
event.preventDefault()
@@ -74,7 +71,7 @@ export default defineComponent({
}
}
- return { isError, onLoadMore, handleShiftTab }
+ return { isError, handleShiftTab }
},
})
</script>
diff --git a/src/components/VLoadMore.vue b/src/components/VLoadMore.vue
index 82a1953f2..4a0226fc8 100644
--- a/src/components/VLoadMore.vue
+++ b/src/components/VLoadMore.vue
@@ -1,9 +1,9 @@
<template>
<VButton
- v-show="!endOfResults"
+ v-show="canLoadMore"
size="large"
variant="full"
- :disabled="!canLoadMore || isFetching"
+ :disabled="isFetching"
data-testid="load-more"
@click="onLoadMore"
>
@@ -29,27 +29,40 @@ export default defineComponent({
const mediaStore = useMediaStore()
const searchStore = useSearchStore()
+ const isFetching = computed(() => mediaStore.fetchState.isFetching)
+
+ /**
+ * 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(
() =>
searchStore.searchTerm !== '' &&
- !mediaStore.fetchState.isFetching &&
!mediaStore.fetchState.fetchingError &&
+ !mediaStore.fetchState.isFinished &&
mediaStore.resultCount > 0
)
+
+ /**
+ * 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 (!canLoadMore.value) return
+ if (isFetching.value) return
await mediaStore.fetchMedia({
shouldPersistMedia: true,
})
}
- const isFetching = computed(() => mediaStore.fetchState.isFetching)
- const endOfResults = computed(
- () => !(canLoadMore.value || isFetching.value)
- )
const buttonLabel = computed(() =>
- i18n.t(`browse-page.${isFetching.value ? 'loading' : 'load'}`)
+ isFetching.value
+ ? i18n.t('browse-page.loading')
+ : i18n.t('browse-page.load')
)
return {
@@ -57,7 +70,6 @@ export default defineComponent({
isFetching,
onLoadMore,
canLoadMore,
- endOfResults,
}
},
})
diff --git a/src/composables/use-load-more.ts b/src/composables/use-load-more.ts
deleted file mode 100644
index 71c0db106..000000000
--- a/src/composables/use-load-more.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import { computed, ComputedRef } from '@nuxtjs/composition-api'
-
-import { useMediaStore } from '~/stores/media'
-
-/**
- * Fetches media on 'Load More' button click.
- */
-export const useLoadMore = (searchTerm: ComputedRef<string>) => {
- const canLoadMore = computed(() => searchTerm.value.trim() !== '')
-
- const onLoadMore = async () => {
- const mediaStore = useMediaStore()
- if (canLoadMore.value) {
- await mediaStore.fetchMedia({
- shouldPersistMedia: true,
- })
- }
- }
-
- return { canLoadMore, onLoadMore }
-}
diff --git a/src/pages/search/audio.vue b/src/pages/search/audio.vue
index ea7f5033a..980be4105 100644
--- a/src/pages/search/audio.vue
+++ b/src/pages/search/audio.vue
@@ -33,18 +33,11 @@
</template>
<script lang="ts">
-import {
- computed,
- defineComponent,
- toRef,
- useMeta,
-} from '@nuxtjs/composition-api'
+import { computed, defineComponent, useMeta } from '@nuxtjs/composition-api'
-import { useLoadMore } from '~/composables/use-load-more'
import { isMinScreen } from '~/composables/use-media-query'
import { useBrowserIsMobile } from '~/composables/use-browser-detection'
import { useFocusFilters } from '~/composables/use-focus-filters'
-import { useI18n } from '~/composables/use-i18n'
import { Focus } from '~/utils/focus-management'
import { useUiStore } from '~/stores/ui'
@@ -66,8 +59,6 @@ export default defineComponent({
},
props: propTypes,
setup(props) {
- const i18n = useI18n()
-
useMeta({ title: `${props.searchTerm} | Openverse` })
const results = computed(() => props.resultItems.audio)
@@ -84,20 +75,12 @@ export default defineComponent({
: 'm'
})
- const isError = computed(() => !!props.fetchState.fetchingError)
- const errorHeader = computed(() => {
- const type = i18n.t('browse-page.search-form.audio')
- return i18n.t('browse-page.fetching-error', { type })
- })
-
const focusFilters = useFocusFilters()
const handleShiftTab = (event: KeyboardEvent, i: number) => {
if (i === 0) {
focusFilters.focusFilterSidebar(event, Focus.Last)
}
}
- const searchTermRef = toRef(props, 'searchTerm')
- const { canLoadMore, onLoadMore } = useLoadMore(searchTermRef)
const uiStore = useUiStore()
const isSnackbarVisible = computed(() => uiStore.areInstructionsVisible)
@@ -111,12 +94,8 @@ export default defineComponent({
return {
results,
audioTrackSize,
- isError,
- errorHeader,
handleShiftTab,
- canLoadMore,
- onLoadMore,
isSnackbarVisible,
showSnackbar,
diff --git a/src/pages/search/image.vue b/src/pages/search/image.vue
index 9cd0e38a8..5e2dd5493 100644
--- a/src/pages/search/image.vue
+++ b/src/pages/search/image.vue
@@ -1,23 +1,16 @@
<template>
<VImageGrid
:images="results"
- :can-load-more="canLoadMore"
+ :is-single-page="false"
:fetch-state="fetchState"
- @load-more="onLoadMore"
@shift-tab="handleShiftTab"
/>
</template>
<script lang="ts">
-import {
- computed,
- defineComponent,
- toRef,
- useMeta,
-} from '@nuxtjs/composition-api'
+import { computed, defineComponent, useMeta } from '@nuxtjs/composition-api'
import { propTypes } from '~/pages/search/search-page.types'
-import { useLoadMore } from '~/composables/use-load-more'
import { useFocusFilters } from '~/composables/use-focus-filters'
import VImageGrid from '~/components/VImageGrid/VImageGrid.vue'
@@ -31,15 +24,12 @@ export default defineComponent({
const results = computed(() => props.resultItems.image)
- const searchTermRef = toRef(props, 'searchTerm')
- const { canLoadMore, onLoadMore } = useLoadMore(searchTermRef)
-
const focusFilters = useFocusFilters()
const handleShiftTab = () => {
focusFilters.focusFilterSidebar()
}
- return { canLoadMore, onLoadMore, handleShiftTab, results }
+ return { handleShiftTab, results }
},
head: {},
})
diff --git a/src/pages/search/index.vue b/src/pages/search/index.vue
index b0b4f7e70..9d6f8d1c2 100644
--- a/src/pages/search/index.vue
+++ b/src/pages/search/index.vue
@@ -1,11 +1,10 @@
<template>
- <VAllResultsGrid :can-load-more="canLoadMore" @load-more="onLoadMore" />
+ <VAllResultsGrid />
</template>
<script lang="ts">
-import { defineComponent, toRef, useMeta } from '@nuxtjs/composition-api'
+import { defineComponent, useMeta } from '@nuxtjs/composition-api'
-import { useLoadMore } from '~/composables/use-load-more'
import { propTypes } from '~/pages/search/search-page.types'
import VAllResultsGrid from '~/components/VAllResultsGrid/VAllResultsGrid.vue'
@@ -16,11 +15,6 @@ export default defineComponent({
props: propTypes,
setup(props) {
useMeta({ title: `${props.searchTerm} | Openverse` })
-
- const searchTermRef = toRef(props, 'searchTerm')
- const { canLoadMore, onLoadMore } = useLoadMore(searchTermRef)
-
- return { canLoadMore, onLoadMore }
},
head: {
meta: [
diff --git a/src/stores/media/index.ts b/src/stores/media/index.ts
index 2dc514a7b..8ab214ae8 100644
--- a/src/stores/media/index.ts
+++ b/src/stores/media/index.ts
@@ -135,24 +135,31 @@ export const useMediaStore = defineStore('media', {
fetchState(): FetchState {
if (this._searchType === ALL_MEDIA) {
/**
- * For all_media, we return the error for the first media type that has an error.
+ * For all_media, we return 'All media fetching error' if all types have some kind of error.
*/
- const findFirstError = () => {
- for (const type of supportedMediaTypes) {
- if (this.mediaFetchState[type].fetchingError) {
- return this.mediaFetchState[type].fetchingError
- }
- }
- return null
- }
const atLeastOne = (property: keyof FetchState) =>
supportedMediaTypes.some(
(type) => this.mediaFetchState[type][property]
)
+ const allMediaError = () => {
+ const errors = {} as Record<SupportedMediaType, string>
+ for (const mt of supportedMediaTypes) {
+ const error = this.mediaFetchState[mt].fetchingError
+ if (error) {
+ errors[mt] = error
+ }
+ }
+ return JSON.stringify(errors)
+ }
+
return {
isFetching: atLeastOne('isFetching'),
- fetchingError: findFirstError(),
+ fetchingError: supportedMediaTypes.every(
+ (type) => this.mediaFetchState[type].fetchingError !== null
+ )
+ ? allMediaError()
+ : null,
hasStarted: atLeastOne('hasStarted'),
isFinished: supportedMediaTypes.every(
(type) => this.mediaFetchState[type].isFinished
@@ -365,7 +372,8 @@ export const useMediaStore = defineStore('media', {
).filter(
(type) =>
!this.mediaFetchState[type].fetchingError &&
- !this.mediaFetchState[type].isFetching
+ !this.mediaFetchState[type].isFetching &&
+ !this.mediaFetchState[type].isFinished
)
await Promise.all(
mediaToFetch.map((type) =>
@@ -415,7 +423,6 @@ export const useMediaStore = defineStore('media', {
const mediaCount = data.result_count
let errorMessage
if (!mediaCount) {
- errorMessage = `No ${mediaType} found for this query`
page = undefined
}
this._updateFetchState(mediaType, 'end', errorMessage)
diff --git a/test/unit/specs/stores/media-store.spec.js b/test/unit/specs/stores/media-store.spec.js
index 5fd998423..06d69b45c 100644
--- a/test/unit/specs/stores/media-store.spec.js
+++ b/test/unit/specs/stores/media-store.spec.js
@@ -173,7 +173,7 @@ describe('Media Store', () => {
it.each`
searchType | fetchState
- ${ALL_MEDIA} | ${{ fetchingError: 'Error', hasStarted: true, isFetching: true, isFinished: false }}
+ ${ALL_MEDIA} | ${{ fetchingError: null, hasStarted: true, isFetching: true, isFinished: false }}
${AUDIO} | ${{ fetchingError: 'Error', hasStarted: true, isFetching: false, isFinished: true }}
${IMAGE} | ${{ fetchingError: null, hasStarted: true, isFetching: true, isFinished: false }}
`(
@@ -188,6 +188,20 @@ describe('Media Store', () => {
expect(mediaStore.fetchState).toEqual(fetchState)
}
)
+ it('fetchState for ALL_MEDIA returns compound error if all types have errors', () => {
+ const mediaStore = useMediaStore()
+ const searchStore = useSearchStore()
+ searchStore.setSearchType(ALL_MEDIA)
+ mediaStore._updateFetchState(AUDIO, 'end', 'Error')
+ mediaStore._updateFetchState(IMAGE, 'end', 'Error')
+
+ expect(mediaStore.fetchState).toEqual({
+ fetchingError: '{"image":"Error","audio":"Error"}',
+ hasStarted: true,
+ isFetching: false,
+ isFinished: true,
+ })
+ })
})
describe('actions', () => {
@@ -300,35 +314,6 @@ describe('Media Store', () => {
}
)
- it('fetchSingleMediaType throws an error if result count is 0', async () => {
- const mediaType = IMAGE
- mockSearchImage.mockImplementationOnce(() =>
- Promise.resolve({
- results: {},
- result_count: 0,
- page_count: 0,
- })
- )
-
- const mediaStore = useMediaStore()
-
- const params = {
- q: 'foo',
- page: 1,
- shouldPersistMedia: false,
- mediaType,
- }
- await mediaStore.fetchSingleMediaType(params)
-
- expect(mediaStore.results[mediaType]).toEqual(initialResults)
- expect(mediaStore.fetchState).toEqual({
- isFetching: false,
- hasStarted: true,
- isFinished: false,
- fetchingError: `No ${mediaType} found for this query`,
- })
- })
-
it('fetchSingleMediaType resets images if shouldPersistMedia is false', async () => {
const mediaStore = useMediaStore()