From 6b87911897d983590070fb624fad1445aaff2913 Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Thu, 16 Jan 2025 18:53:48 +0300 Subject: [PATCH] Simplify media `FetchState` and fix fetching errors (#5323) * Refactor FetchState to use discriminated union Fixes #5322 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/WordPress/openverse/issues/5322?shareId=XXXX-XXXX-XXXX-XXXX). * Fix loading labels, results, no result on later pages * Make `canLoadMore` external prop * Fix collection header stories * Refactor i18n search result count labels * Improve asyncData error in search.vue --- frontend/nuxt.config.ts | 1 + frontend/shared/types/fetch-state.ts | 9 +- .../VCollectionHeader/VCollectionHeader.vue | 10 +- .../meta/VCollectionHeader.stories.ts | 4 + .../components/VContentLink/VContentLink.vue | 28 +- .../src/components/VHeader/VHeaderDesktop.vue | 2 +- .../VHeader/VHeaderMobile/VHeaderMobile.vue | 2 +- frontend/src/components/VLoadMore.vue | 18 +- .../components/VMediaInfo/VRelatedMedia.vue | 2 +- .../VSearchResultsGrid/VCollectionResults.vue | 2 + .../VSearchResultsGrid/VSearchResults.vue | 18 +- .../meta/CustomButtonComponents.stories.ts | 6 +- frontend/src/composables/use-collection.ts | 9 +- .../src/composables/use-i18n-utilities.ts | 58 ++- frontend/src/composables/use-search.ts | 7 +- frontend/src/middleware/search.ts | 8 +- frontend/src/middleware/single-result.ts | 2 +- frontend/src/pages/audio/[id]/index.vue | 2 +- frontend/src/pages/image/[id]/index.vue | 2 +- frontend/src/pages/image/collection.vue | 2 + frontend/src/pages/search.vue | 35 +- frontend/src/stores/media/index.ts | 245 ++++++------ frontend/src/stores/media/related-media.ts | 19 +- frontend/src/stores/media/single-result.ts | 16 +- frontend/src/stores/provider.ts | 9 +- .../specs/components/v-content-link.spec.js | 6 +- .../composables/use-i18n-utilities.spec.js | 28 +- .../specs/stores/media-store-fetching.spec.js | 222 ----------- .../unit/specs/stores/media-store.spec.ts | 360 ++++++++++++++---- .../test/unit/specs/stores/provider.spec.ts | 5 +- .../specs/stores/single-result-store.spec.ts | 6 +- 31 files changed, 578 insertions(+), 565 deletions(-) delete mode 100644 frontend/test/unit/specs/stores/media-store-fetching.spec.js diff --git a/frontend/nuxt.config.ts b/frontend/nuxt.config.ts index bf28acf2d57..d45ccb5b7a6 100644 --- a/frontend/nuxt.config.ts +++ b/frontend/nuxt.config.ts @@ -12,6 +12,7 @@ export default defineNuxtConfig({ port: 8443, host: "0.0.0.0", }, + devtools: { enabled: true }, imports: { autoImport: false, }, diff --git a/frontend/shared/types/fetch-state.ts b/frontend/shared/types/fetch-state.ts index dd80cdc9e00..d12ad27f00c 100644 --- a/frontend/shared/types/fetch-state.ts +++ b/frontend/shared/types/fetch-state.ts @@ -27,9 +27,6 @@ export interface FetchingError { details?: Record } -export interface FetchState { - isFetching: boolean - hasStarted?: boolean - isFinished?: boolean - fetchingError: FetchingError | null -} +export type FetchState = + | { status: "idle" | "fetching" | "success"; error: null } + | { status: "error"; error: FetchingError } diff --git a/frontend/src/components/VCollectionHeader/VCollectionHeader.vue b/frontend/src/components/VCollectionHeader/VCollectionHeader.vue index 637a3c0e81f..2cad1aae7b0 100644 --- a/frontend/src/components/VCollectionHeader/VCollectionHeader.vue +++ b/frontend/src/components/VCollectionHeader/VCollectionHeader.vue @@ -89,16 +89,12 @@ const showCollectionExternalLink = computed(() => { return Boolean(props.collectionParams.collection !== "tag" && url.value) }) -const { getI18nCollectionResultCountLabel } = useI18nResultsCount() +const showLoading = computed(() => mediaStore.showLoading) +const { getI18nCollectionResultCountLabel } = useI18nResultsCount(showLoading) const resultsLabel = computed(() => { - if (mediaStore.resultCount === 0 && mediaStore.fetchState.isFetching) { - return "" - } - const resultsCount = mediaStore.results[props.mediaType].count - return getI18nCollectionResultCountLabel( - resultsCount, + mediaStore.results[props.mediaType].count, props.mediaType, props.collectionParams.collection ) diff --git a/frontend/src/components/VCollectionHeader/meta/VCollectionHeader.stories.ts b/frontend/src/components/VCollectionHeader/meta/VCollectionHeader.stories.ts index dd29a749a96..664c39abf0a 100644 --- a/frontend/src/components/VCollectionHeader/meta/VCollectionHeader.stories.ts +++ b/frontend/src/components/VCollectionHeader/meta/VCollectionHeader.stories.ts @@ -95,6 +95,10 @@ export const AllCollections: Omit = { const mediaStore = useMediaStore() mediaStore.$patch({ results: { image: { count: 240 } }, + mediaFetchState: { + image: { status: "success", error: null }, + audio: { status: "success", error: null }, + }, }) return () => h( diff --git a/frontend/src/components/VContentLink/VContentLink.vue b/frontend/src/components/VContentLink/VContentLink.vue index 89cb9c117f2..ad9e21827d9 100644 --- a/frontend/src/components/VContentLink/VContentLink.vue +++ b/frontend/src/components/VContentLink/VContentLink.vue @@ -1,43 +1,29 @@