audiobookshelf/client/components/modals/edit-tabs/Match.vue

144 lines
3.9 KiB
Vue
Raw Normal View History

2021-08-18 00:01:11 +02:00
<template>
<div class="w-full h-full overflow-hidden">
<form @submit.prevent="submitSearch">
<div class="flex items-center justify-start -mx-1 h-20">
<div class="w-72 px-1">
<ui-text-input-with-label v-model="searchTitle" label="Search Title" placeholder="Search" :disabled="processing" />
</div>
<div class="w-72 px-1">
<ui-text-input-with-label v-model="searchAuthor" label="Author" :disabled="processing" />
</div>
<ui-btn class="mt-5 ml-1" type="submit">Search</ui-btn>
<div class="flex-grow" />
2021-08-18 00:01:11 +02:00
</div>
</form>
2021-08-18 00:01:11 +02:00
<div v-show="processing" class="flex h-full items-center justify-center">
<p>Loading...</p>
</div>
<div v-show="!processing && !searchResults.length && hasSearched" class="flex h-full items-center justify-center">
<p>No Results</p>
</div>
<div v-show="!processing" class="w-full max-h-full overflow-y-auto overflow-x-hidden matchListWrapper">
2021-08-18 00:01:11 +02:00
<template v-for="(res, index) in searchResults">
<cards-book-match-card :key="index" :book="res" @select="selectMatch" />
2021-08-18 00:01:11 +02:00
</template>
</div>
</div>
</template>
<script>
export default {
props: {
processing: Boolean,
2021-08-18 00:01:11 +02:00
audiobook: {
type: Object,
default: () => {}
}
},
data() {
return {
audiobookId: null,
searchTitle: null,
searchAuthor: null,
2021-08-18 00:01:11 +02:00
lastSearch: null,
provider: 'best',
searchResults: [],
hasSearched: false
2021-08-18 00:01:11 +02:00
}
},
watch: {
audiobook: {
immediate: true,
handler(newVal) {
if (newVal) this.init()
}
}
},
computed: {
isProcessing: {
get() {
return this.processing
},
set(val) {
this.$emit('update:processing', val)
}
}
},
2021-08-18 00:01:11 +02:00
methods: {
getSearchQuery() {
var searchQuery = `provider=${this.provider}&fallbackTitleOnly=1&title=${this.searchTitle}`
if (this.searchAuthor) searchQuery += `&author=${this.searchAuthor}`
return searchQuery
},
2021-08-18 00:01:11 +02:00
submitSearch() {
if (!this.searchTitle) {
this.$toast.warning('Search title is required')
return
}
2021-08-18 00:01:11 +02:00
this.runSearch()
},
async runSearch() {
var searchQuery = this.getSearchQuery()
if (this.lastSearch === searchQuery) return
2021-08-18 00:01:11 +02:00
this.searchResults = []
this.isProcessing = true
this.lastSearch = searchQuery
var results = await this.$axios.$get(`/api/find/search?${searchQuery}`).catch((error) => {
console.error('Failed', error)
return []
})
results = results.filter((res) => {
return !!res.title
})
2021-08-18 00:01:11 +02:00
this.searchResults = results
this.isProcessing = false
this.hasSearched = true
2021-08-18 00:01:11 +02:00
},
init() {
if (this.audiobook.id !== this.audiobookId) {
this.searchResults = []
this.hasSearched = false
this.audiobookId = this.audiobook.id
}
2021-08-18 00:01:11 +02:00
if (!this.audiobook.book || !this.audiobook.book.title) {
this.searchTitle = null
this.searchAuthor = null
2021-08-18 00:01:11 +02:00
return
}
this.searchTitle = this.audiobook.book.title
this.searchAuthor = this.audiobook.book.author || ''
2021-08-18 00:01:11 +02:00
},
async selectMatch(match) {
this.isProcessing = true
const updatePayload = {
book: {}
}
if (match.cover) {
updatePayload.book.cover = match.cover
}
if (match.title) {
updatePayload.book.title = match.title
}
if (match.description) {
updatePayload.book.description = match.description
}
var updatedAudiobook = await this.$axios.$patch(`/api/audiobook/${this.audiobook.id}`, updatePayload).catch((error) => {
console.error('Failed to update', error)
return false
})
this.isProcessing = false
if (updatedAudiobook) {
this.$toast.success('Update Successful')
this.$emit('close')
}
}
2021-08-18 00:01:11 +02:00
}
}
</script>
<style>
.matchListWrapper {
height: calc(100% - 80px);
}
</style>