audiobookshelf/client/pages/library/_library/search.vue

81 lines
2.3 KiB
Vue
Raw Normal View History

<template>
<div class="page" :class="streamLibraryItem ? 'streaming' : ''">
<app-book-shelf-toolbar is-home page="search" :search-query="query" />
<app-book-shelf-categorized v-if="hasResults" ref="bookshelf" search :results="results" />
<div v-else class="w-full py-16">
2022-11-09 00:10:08 +01:00
<p class="text-xl text-center">{{ $getString('MessageNoSearchResultsFor', [query]) }}</p>
</div>
</div>
</template>
<script>
export default {
async asyncData({ store, params, redirect, query, app }) {
2023-04-25 01:25:30 +02:00
const libraryId = params.library
const library = await store.dispatch('libraries/fetch', libraryId)
2021-12-02 02:07:03 +01:00
if (!library) {
return redirect('/oops?message=Library not found')
}
2023-04-25 01:25:30 +02:00
let results = await app.$axios.$get(`/api/libraries/${libraryId}/search?q=${query.q}`).catch((error) => {
console.error('Failed to search library', error)
return null
})
results = {
2023-04-25 01:25:30 +02:00
podcasts: results?.podcast || [],
books: results?.book || [],
authors: results?.authors || [],
series: results?.series || [],
tags: results?.tags || [],
narrators: results?.narrators || []
}
return {
2021-12-02 02:07:03 +01:00
libraryId,
results,
2023-04-25 01:25:30 +02:00
query: query.q
}
},
data() {
return {}
},
2021-12-02 02:07:03 +01:00
watch: {
'$route.query'(newVal, oldVal) {
if (newVal && newVal.q && newVal.q !== this.query) {
this.query = newVal.q
this.search()
}
}
},
computed: {
streamLibraryItem() {
return this.$store.state.streamLibraryItem
},
hasResults() {
2022-04-12 23:54:52 +02:00
return Object.values(this.results).find((r) => !!r && r.length)
}
},
methods: {
2021-12-02 02:07:03 +01:00
async search() {
2023-04-25 01:25:30 +02:00
const results = await this.$axios.$get(`/api/libraries/${this.libraryId}/search?q=${this.query}`).catch((error) => {
2021-12-02 02:07:03 +01:00
console.error('Failed to search library', error)
return null
})
this.results = {
2023-04-25 01:25:30 +02:00
podcasts: results?.podcast || [],
books: results?.book || [],
authors: results?.authors || [],
series: results?.series || [],
tags: results?.tags || [],
narrators: results?.narrators || []
2021-12-02 02:07:03 +01:00
}
this.$nextTick(() => {
if (this.$refs.bookshelf) {
this.$refs.bookshelf.setShelvesFromSearch()
}
})
}
},
mounted() {},
beforeDestroy() {}
}
</script>