2021-08-18 00:01:11 +02:00
|
|
|
<template>
|
|
|
|
<div class="w-full h-full overflow-hidden">
|
2021-08-21 16:15:44 +02:00
|
|
|
<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>
|
2021-08-21 16:15:44 +02:00
|
|
|
</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>
|
2021-08-21 23:23:35 +02:00
|
|
|
<div v-show="!processing && !searchResults.length && hasSearched" class="flex h-full items-center justify-center">
|
2021-08-18 13:50:24 +02:00
|
|
|
<p>No Results</p>
|
|
|
|
</div>
|
2021-08-21 16:15:44 +02:00
|
|
|
<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">
|
2021-08-21 16:15:44 +02:00
|
|
|
<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: {
|
2021-08-18 13:50:24 +02:00
|
|
|
processing: Boolean,
|
2021-08-18 00:01:11 +02:00
|
|
|
audiobook: {
|
|
|
|
type: Object,
|
|
|
|
default: () => {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2021-08-21 23:23:35 +02:00
|
|
|
audiobookId: null,
|
2021-08-21 16:15:44 +02:00
|
|
|
searchTitle: null,
|
|
|
|
searchAuthor: null,
|
2021-08-18 00:01:11 +02:00
|
|
|
lastSearch: null,
|
2021-08-21 16:15:44 +02:00
|
|
|
provider: 'best',
|
2021-08-21 23:23:35 +02:00
|
|
|
searchResults: [],
|
|
|
|
hasSearched: false
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
audiobook: {
|
|
|
|
immediate: true,
|
|
|
|
handler(newVal) {
|
|
|
|
if (newVal) this.init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-08-18 13:50:24 +02:00
|
|
|
computed: {
|
|
|
|
isProcessing: {
|
|
|
|
get() {
|
|
|
|
return this.processing
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
this.$emit('update:processing', val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-08-18 00:01:11 +02:00
|
|
|
methods: {
|
2021-08-21 16:15:44 +02:00
|
|
|
getSearchQuery() {
|
2021-08-26 02:15:00 +02:00
|
|
|
var searchQuery = `provider=${this.provider}&fallbackTitleOnly=1&title=${this.searchTitle}`
|
2021-08-21 16:15:44 +02:00
|
|
|
if (this.searchAuthor) searchQuery += `&author=${this.searchAuthor}`
|
|
|
|
return searchQuery
|
|
|
|
},
|
2021-08-18 00:01:11 +02:00
|
|
|
submitSearch() {
|
2021-08-21 16:15:44 +02:00
|
|
|
if (!this.searchTitle) {
|
|
|
|
this.$toast.warning('Search title is required')
|
|
|
|
return
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
this.runSearch()
|
|
|
|
},
|
|
|
|
async runSearch() {
|
2021-08-21 16:15:44 +02:00
|
|
|
var searchQuery = this.getSearchQuery()
|
|
|
|
if (this.lastSearch === searchQuery) return
|
2021-08-18 00:01:11 +02:00
|
|
|
this.searchResults = []
|
2021-08-21 01:29:10 +02:00
|
|
|
this.isProcessing = true
|
2021-08-21 16:15:44 +02:00
|
|
|
this.lastSearch = searchQuery
|
|
|
|
var results = await this.$axios.$get(`/api/find/search?${searchQuery}`).catch((error) => {
|
2021-08-18 13:50:24 +02:00
|
|
|
console.error('Failed', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
results = results.filter((res) => {
|
|
|
|
return !!res.title
|
|
|
|
})
|
2021-08-18 00:01:11 +02:00
|
|
|
this.searchResults = results
|
2021-08-21 01:29:10 +02:00
|
|
|
this.isProcessing = false
|
2021-08-21 23:23:35 +02:00
|
|
|
this.hasSearched = true
|
2021-08-18 00:01:11 +02:00
|
|
|
},
|
|
|
|
init() {
|
2021-08-21 23:23:35 +02:00
|
|
|
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) {
|
2021-08-21 16:15:44 +02:00
|
|
|
this.searchTitle = null
|
2021-08-21 23:23:35 +02:00
|
|
|
this.searchAuthor = null
|
2021-08-18 00:01:11 +02:00
|
|
|
return
|
|
|
|
}
|
2021-08-21 16:15:44 +02:00
|
|
|
this.searchTitle = this.audiobook.book.title
|
|
|
|
this.searchAuthor = this.audiobook.book.author || ''
|
2021-08-18 00:01:11 +02:00
|
|
|
},
|
2021-08-18 13:50:24 +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
|
|
|
}
|
|
|
|
}
|
2021-08-21 16:15:44 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.matchListWrapper {
|
|
|
|
height: calc(100% - 80px);
|
|
|
|
}
|
|
|
|
</style>
|