From 07088d591dab91fed384156731c945e3c3f94922 Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Fri, 15 Mar 2024 22:36:17 +0300 Subject: [PATCH] Fix bugs discovered during code review Signed-off-by: Olga Bulat --- frontend/src/composables/use-collection.ts | 66 ------------------ frontend/src/composables/use-search-type.ts | 9 ++- frontend/src/pages/audio/collection.vue | 74 +++++++++++++++------ frontend/src/pages/image/collection.vue | 68 ++++++++++++++----- frontend/src/pages/search.vue | 14 +--- frontend/src/stores/media/index.ts | 3 - 6 files changed, 115 insertions(+), 119 deletions(-) delete mode 100644 frontend/src/composables/use-collection.ts diff --git a/frontend/src/composables/use-collection.ts b/frontend/src/composables/use-collection.ts deleted file mode 100644 index 4c832626144..00000000000 --- a/frontend/src/composables/use-collection.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { computed, ref } from "vue" - -import { useMediaStore } from "~/stores/media" -import { useSearchStore } from "~/stores/search" -import { useI18n } from "~/composables/use-i18n" -import type { DetailFromMediaType } from "~/types/media" -import type { SupportedMediaType } from "~/constants/media" - -export const useCollection = ({ - mediaType, -}: { - mediaType: T -}) => { - const mediaStore = useMediaStore() - const searchStore = useSearchStore() - - const collectionParams = computed(() => searchStore.collectionParams) - const isFetching = computed(() => mediaStore.fetchState.isFetching) - - const media = ref[]>([]) - const creatorUrl = ref() - - const i18n = useI18n() - - const collectionLabel = computed(() => { - if (!collectionParams.value) { - return "" - } - const { collection, ...params } = collectionParams.value - return i18n - .t(`collection.label.${collection}.image`, { ...params }) - .toString() - }) - - const fetchMedia = async ( - { shouldPersistMedia }: { shouldPersistMedia: boolean } = { - shouldPersistMedia: false, - } - ) => { - if (mediaStore._searchType !== mediaType) { - throw new Error( - `Search type is incorrectly set in the store to ${mediaStore._searchType} when it should be "${mediaType}"` - ) - } - media.value = (await mediaStore.fetchMedia({ - shouldPersistMedia, - })) as (typeof media)["value"] - creatorUrl.value = - media.value.length > 0 ? media.value[0].creator_url : undefined - } - - const handleLoadMore = async () => { - await fetchMedia({ shouldPersistMedia: true }) - } - - const results = computed(() => ({ type: mediaType, items: media.value })) - - return { - results, - creatorUrl, - fetchMedia, - handleLoadMore, - collectionLabel, - isFetching, - } -} diff --git a/frontend/src/composables/use-search-type.ts b/frontend/src/composables/use-search-type.ts index 970b888a0c5..4e0d4f218e2 100644 --- a/frontend/src/composables/use-search-type.ts +++ b/frontend/src/composables/use-search-type.ts @@ -11,7 +11,6 @@ import { SearchType, } from "~/constants/media" -import { useMediaStore } from "~/stores/media" import { useSearchStore } from "~/stores/search" import { useFeatureFlagStore } from "~/stores/feature-flag" @@ -65,8 +64,14 @@ export default function useSearchType() { next: searchType, component: componentName, }) + + // `setActiveType` is called after the search middleware + // ran and updated the search store state + // TODO: Figure out why + if (activeType.value === searchType) { + return + } useSearchStore().setSearchType(searchType) - useMediaStore().clearMedia() previousSearchType.value = searchType } diff --git a/frontend/src/pages/audio/collection.vue b/frontend/src/pages/audio/collection.vue index e5d6aed02f7..9c01fd0a59c 100644 --- a/frontend/src/pages/audio/collection.vue +++ b/frontend/src/pages/audio/collection.vue @@ -4,22 +4,29 @@ v-if="collectionParams" search-term="" :is-fetching="isFetching" - :results="results" + :results="{ type: 'audio', items: media }" :collection-label="collectionLabel" :collection-params="collectionParams" - @load-more="handleLoadMore" + @load-more="loadMore" />