Skip to content

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

Refactor search.vue and add it to tsconfig (#2866)

Olga Bulat29a65ee61acc
diff --git a/frontend/src/components/VSearchGrid.vue b/frontend/src/components/VSearchGrid.vue
deleted file mode 100644
index 0f1d42f39..000000000
--- a/frontend/src/components/VSearchGrid.vue
+++ /dev/null
@@ -1,109 +0,0 @@
-<template>
- <section v-if="!showError">
- <header v-if="query.q && supported" class="my-0 md:mb-8 md:mt-4">
- <VSearchResultsTitle :size="isAllView ? 'large' : 'default'">
- {{ searchTerm }}
- </VSearchResultsTitle>
- </header>
-
- <slot name="media" />
-
- <VExternalSearchForm
- v-if="!isAllView"
- :has-no-results="hasNoResults"
- :search-term="searchTerm"
- :is-supported="supported"
- />
- </section>
- <VErrorSection v-else class="w-full py-10">
- <template #image>
- <VErrorImage error-code="NO_RESULT" />
- </template>
- <VNoResults :search-term="searchTerm" />
- </VErrorSection>
-</template>
-
-<script lang="ts">
-import { computed, defineComponent, PropType } from "vue"
-
-import { ALL_MEDIA, SearchType } from "~/constants/media"
-import { NO_RESULT } from "~/constants/errors"
-import { defineEvent } from "~/types/emits"
-import type { FetchState } from "~/types/fetch-state"
-import type { ApiQueryParams } from "~/utils/search-query-transform"
-
-import VExternalSearchForm from "~/components/VExternalSearch/VExternalSearchForm.vue"
-import VErrorSection from "~/components/VErrorSection/VErrorSection.vue"
-import VErrorImage from "~/components/VErrorSection/VErrorImage.vue"
-import VNoResults from "~/components/VErrorSection/VNoResults.vue"
-import VSearchResultsTitle from "~/components/VSearchResultsTitle.vue"
-
-export default defineComponent({
- name: "VSearchGrid",
- components: {
- VErrorSection,
- VExternalSearchForm,
- VErrorImage,
- VNoResults,
- VSearchResultsTitle,
- },
- props: {
- supported: {
- type: Boolean,
- required: true,
- },
- query: {
- type: Object as PropType<ApiQueryParams>,
- required: true,
- },
- searchType: {
- type: String as PropType<SearchType>,
- required: true,
- },
- fetchState: {
- type: Object as PropType<FetchState>,
- required: true,
- },
- resultsCount: {
- type: Number,
- required: true,
- },
- },
- emits: {
- tab: defineEvent<[KeyboardEvent]>(),
- },
- setup(props) {
- const hasNoResults = computed(() => {
- // noResult is hard-coded for search types that are not currently
- // supported by Openverse built-in search
- return props.supported
- ? Boolean(
- props.query.q !== "" &&
- props.fetchState.hasStarted &&
- props.resultsCount === 0
- )
- : false
- })
-
- const isAllView = computed(() => props.searchType === ALL_MEDIA)
-
- const searchTerm = computed(() => props.query.q || "")
-
- const showError = computed(() => {
- return (
- props.fetchState.hasStarted &&
- !props.fetchState.isFetching &&
- props.resultsCount === 0
- )
- })
-
- return {
- hasNoResults,
- isAllView,
- NO_RESULT,
- searchTerm,
- showError,
- }
- },
-})
-</script>
diff --git a/frontend/src/components/VSearchResultsGrid/VImageGrid.vue b/frontend/src/components/VSearchResultsGrid/VImageGrid.vue
index 03e3cc7f0..9f8797366 100644
--- a/frontend/src/components/VSearchResultsGrid/VImageGrid.vue
+++ b/frontend/src/components/VSearchResultsGrid/VImageGrid.vue
@@ -64,7 +64,7 @@ export default defineComponent({
required: true,
},
fetchState: {
- type: Object as PropType<FetchState<NuxtError>>,
+ type: Object as PropType<FetchState | FetchState<NuxtError>>,
required: true,
},
imageGridLabel: {
diff --git a/frontend/src/pages/search.vue b/frontend/src/pages/search.vue
index 46c7f6615..daed192ba 100644
--- a/frontend/src/pages/search.vue
+++ b/frontend/src/pages/search.vue
@@ -4,30 +4,40 @@
tabindex="-1"
class="browse-page flex w-full flex-col px-6 lg:px-10"
>
- <VSearchGrid
- :fetch-state="fetchState"
- :query="query"
- :supported="supported"
- :search-type="searchType"
- :results-count="resultCount"
- data-testid="search-grid"
- >
- <template #media>
- <NuxtChild
- :key="$route.path"
- :results="resultItems[searchType]"
- :fetch-state="fetchState"
- :search-term="query.q"
- :supported="supported"
- data-testid="search-results"
- />
+ <VErrorSection v-if="fetchingError" class="w-full py-10">
+ <template #image>
+ <VErrorImage error-code="NO_RESULT" />
</template>
- </VSearchGrid>
- <VScrollButton
- v-show="showScrollButton"
- :is-filter-sidebar-visible="isSidebarVisible"
- data-testid="scroll-button"
- />
+ <VNoResults :search-term="searchTerm" />
+ </VErrorSection>
+ <section v-else>
+ <header v-if="query.q && supported" class="my-0 md:mb-8 md:mt-4">
+ <VSearchResultsTitle :size="isAllView ? 'large' : 'default'">{{
+ searchTerm
+ }}</VSearchResultsTitle>
+ </header>
+ <NuxtChild
+ :key="$route.path"
+ :results="
+ isSupportedMediaType(searchType) ? resultItems[searchType] : null
+ "
+ :fetch-state="fetchState"
+ :search-term="query.q"
+ :supported="supported"
+ data-testid="search-results"
+ />
+ <VExternalSearchForm
+ v-if="!isAllView"
+ :search-term="searchTerm"
+ :is-supported="supported"
+ :has-no-results="false"
+ />
+ <VScrollButton
+ v-show="showScrollButton"
+ :is-filter-sidebar-visible="isSidebarVisible"
+ data-testid="scroll-button"
+ />
+ </section>
</div>
</template>
@@ -47,17 +57,27 @@ import { searchMiddleware } from "~/middleware/search"
import { useMediaStore } from "~/stores/media"
import { useSearchStore } from "~/stores/search"
import { NO_RESULT } from "~/constants/errors"
+import { ALL_MEDIA, isSupportedMediaType } from "~/constants/media"
+
import { skipToContentTargetId } from "~/constants/window"
import { IsSidebarVisibleKey, ShowScrollButtonKey } from "~/types/provides"
-import VSearchGrid from "~/components/VSearchGrid.vue"
import VScrollButton from "~/components/VScrollButton.vue"
+import VExternalSearchForm from "~/components/VExternalSearch/VExternalSearchForm.vue"
+import VSearchResultsTitle from "~/components/VSearchResultsTitle.vue"
+import VNoResults from "~/components/VErrorSection/VNoResults.vue"
+import VErrorSection from "~/components/VErrorSection/VErrorSection.vue"
+import VErrorImage from "~/components/VErrorSection/VErrorImage.vue"
export default defineComponent({
name: "BrowsePage",
components: {
+ VErrorImage,
+ VErrorSection,
+ VNoResults,
+ VSearchResultsTitle,
+ VExternalSearchForm,
VScrollButton,
- VSearchGrid,
},
layout: "search-layout",
middleware: searchMiddleware,
@@ -86,6 +106,8 @@ export default defineComponent({
const { resultCount, fetchState, resultItems } = storeToRefs(mediaStore)
+ const isAllView = computed(() => searchType.value === ALL_MEDIA)
+
const needsFetching = computed(() =>
Boolean(supported.value && !resultCount.value && searchTerm.value !== "")
)
@@ -116,11 +138,19 @@ export default defineComponent({
* NO_RESULT error is handled by the VErrorSection component in the child pages.
* For all other errors, show the Nuxt error page.
*/
- if (error?.statusCode && !error?.message.includes(NO_RESULT))
- return nuxtError(mediaStore.fetchState.fetchingError)
+ if (
+ error !== null &&
+ !error?.message?.toLowerCase().includes(NO_RESULT) &&
+ !error?.message?.toLowerCase().includes("timeout")
+ ) {
+ return nuxtError(error)
+ }
}
+ return null
}
+ const fetchingError = computed(() => mediaStore.fetchState.fetchingError)
+
/**
* Search middleware runs when the path changes. This watcher
* is necessary to handle the query changes.
@@ -157,12 +187,16 @@ export default defineComponent({
searchType,
supported,
query,
+ isSupportedMediaType,
resultCount,
fetchState,
resultItems,
needsFetching,
isSidebarVisible,
+ fetchingError,
+
+ isAllView,
skipToContentTargetId,
}
diff --git a/tsconfig.json b/tsconfig.json
index 4446c3cfb..ce871387d 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -45,6 +45,8 @@
"frontend/src/pages/audio/_id/index.vue",
"frontend/src/pages/image/_id/index.vue",
"frontend/src/pages/image/_id/report.vue",
+ "frontend/src/pages/search.vue",
+ "frontend/src/pages/search/**/*.vue",
/**
* Some files from this section cannot be converted to TypeScript because they are
* used in non-TS contexts (like Tailwind configuration). Therefore they're left as