advplyr.audiobookshelf-app/pages/bookshelf/index.vue

256 lines
8 KiB
Vue
Raw Normal View History

<template>
2021-12-31 16:57:53 -06:00
<div class="w-full h-full min-h-full relative">
<template v-for="(shelf, index) in shelves">
2021-12-04 19:56:29 -06:00
<bookshelf-shelf :key="shelf.id" :label="shelf.label" :entities="shelf.entities" :type="shelf.type" :style="{ zIndex: shelves.length - index }" />
</template>
2021-12-04 19:56:29 -06:00
<div v-if="!shelves.length" class="absolute top-0 left-0 w-full h-full flex items-center justify-center">
<div>
<p class="mb-4 text-center text-xl">
2021-12-31 16:57:53 -06:00
Bookshelf empty
<span v-show="isSocketConnected">
for library
<strong>{{ currentLibraryName }}</strong>
</span>
2021-12-04 19:56:29 -06:00
</p>
<div class="w-full" v-if="!isSocketConnected">
<div class="flex justify-center items-center mb-3">
<span class="material-icons text-error text-lg">cloud_off</span>
<p class="pl-2 text-error text-sm">Audiobookshelf server not connected.</p>
</div>
2022-01-16 14:14:02 -06:00
<p class="px-4 text-center text-error absolute bottom-12 left-0 right-0 mx-auto"><strong>Important!</strong> This app requires that you are running <u>your own server</u> and does not provide any content.</p>
2021-12-04 19:56:29 -06:00
</div>
<div class="flex justify-center">
2021-12-31 16:57:53 -06:00
<ui-btn v-if="!isSocketConnected" small @click="$router.push('/connect')" class="w-32">Connect</ui-btn>
2021-12-04 19:56:29 -06:00
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
2021-12-04 19:56:29 -06:00
shelves: [],
loading: true
}
},
computed: {
books() {
return this.$store.getters['downloads/getDownloads'].map((dl) => {
var download = { ...dl }
var ab = { ...download.audiobook }
delete download.audiobook
ab.download = download
return ab
})
2021-12-04 19:56:29 -06:00
},
isSocketConnected() {
return this.$store.state.socketConnected
},
currentLibraryName() {
return this.$store.getters['libraries/getCurrentLibraryName']
},
booksWithUserAbData() {
var books = this.books.map((b) => {
var userAbData = this.$store.getters['user/getUserAudiobookData'](b.id)
return { ...b, userAbData }
})
return books
},
booksCurrentlyReading() {
var books = this.booksWithUserAbData
.map((b) => ({ ...b }))
.filter((b) => b.userAbData && !b.userAbData.isRead && b.userAbData.progress > 0)
.sort((a, b) => {
return b.userAbData.lastUpdate - a.userAbData.lastUpdate
})
return books
},
booksRecentlyAdded() {
var books = this.books
.map((b) => {
return { ...b }
})
.sort((a, b) => b.addedAt - a.addedAt)
return books.slice(0, 10)
},
booksRead() {
var books = this.booksWithUserAbData
.filter((b) => b.userAbData && b.userAbData.isRead)
.sort((a, b) => {
return b.userAbData.lastUpdate - a.userAbData.lastUpdate
})
return books.slice(0, 10)
},
2021-12-04 19:56:29 -06:00
downloadOnlyShelves() {
var shelves = []
if (this.booksCurrentlyReading.length) {
shelves.push({
id: 'recent',
label: 'Continue Reading',
2021-12-04 19:56:29 -06:00
type: 'books',
entities: this.booksCurrentlyReading
})
}
if (this.booksRecentlyAdded.length) {
shelves.push({
id: 'added',
label: 'Recently Added',
2021-12-04 19:56:29 -06:00
type: 'books',
entities: this.booksRecentlyAdded
})
}
if (this.booksRead.length) {
shelves.push({
id: 'read',
label: 'Read Again',
2021-12-04 19:56:29 -06:00
type: 'books',
entities: this.booksRead
})
}
return shelves
2021-12-04 19:56:29 -06:00
},
currentLibraryId() {
return this.$store.state.libraries.currentLibraryId
}
},
methods: {
2021-12-04 19:56:29 -06:00
async fetchCategories() {
var categories = await this.$axios
.$get(`/api/libraries/${this.currentLibraryId}/personalized?minified=1`)
2021-12-04 19:56:29 -06:00
.then((data) => {
return data
})
.catch((error) => {
console.error('Failed to fetch categories', error)
return []
})
this.shelves = categories
console.log('Shelves', this.shelves)
2021-12-04 19:56:29 -06:00
},
async socketInit(isConnected) {
2022-01-16 14:14:02 -06:00
if (isConnected && this.currentLibraryId) {
2021-12-04 19:56:29 -06:00
console.log('Connected - Load from server')
await this.fetchCategories()
} else {
console.log('Disconnected - Reset to local storage')
this.shelves = this.downloadOnlyShelves
}
this.loading = false
},
async libraryChanged(libid) {
2022-01-16 14:14:02 -06:00
if (this.isSocketConnected && this.currentLibraryId) {
2021-12-04 19:56:29 -06:00
await this.fetchCategories()
} else {
this.shelves = this.downloadOnlyShelves
}
},
downloadsLoaded() {
if (!this.isSocketConnected) {
this.shelves = this.downloadOnlyShelves
}
},
audiobookAdded(audiobook) {
console.log('Audiobook added', audiobook)
// TODO: Check if audiobook would be on this shelf
if (!this.search) {
this.fetchCategories()
}
},
audiobookUpdated(audiobook) {
console.log('Audiobook updated', audiobook)
this.shelves.forEach((shelf) => {
if (shelf.type === 'books') {
shelf.entities = shelf.entities.map((ent) => {
if (ent.id === audiobook.id) {
return audiobook
}
return ent
})
} else if (shelf.type === 'series') {
shelf.entities.forEach((ent) => {
ent.books = ent.books.map((book) => {
if (book.id === audiobook.id) return audiobook
return book
})
})
}
})
},
removeBookFromShelf(audiobook) {
this.shelves.forEach((shelf) => {
if (shelf.type === 'books') {
shelf.entities = shelf.entities.filter((ent) => {
return ent.id !== audiobook.id
})
} else if (shelf.type === 'series') {
shelf.entities.forEach((ent) => {
ent.books = ent.books.filter((book) => {
return book.id !== audiobook.id
})
})
}
})
},
audiobookRemoved(audiobook) {
this.removeBookFromShelf(audiobook)
},
audiobooksAdded(audiobooks) {
console.log('audiobooks added', audiobooks)
// TODO: Check if audiobook would be on this shelf
this.fetchCategories()
},
audiobooksUpdated(audiobooks) {
audiobooks.forEach((ab) => {
this.audiobookUpdated(ab)
})
},
initListeners() {
this.$server.on('initialized', this.socketInit)
this.$eventBus.$on('library-changed', this.libraryChanged)
this.$eventBus.$on('downloads-loaded', this.downloadsLoaded)
if (this.$server.socket) {
this.$server.socket.on('audiobook_updated', this.audiobookUpdated)
this.$server.socket.on('audiobook_added', this.audiobookAdded)
this.$server.socket.on('audiobook_removed', this.audiobookRemoved)
this.$server.socket.on('audiobooks_updated', this.audiobooksUpdated)
this.$server.socket.on('audiobooks_added', this.audiobooksAdded)
} else {
console.error('Error socket not initialized')
}
},
removeListeners() {
this.$server.off('initialized', this.socketInit)
this.$eventBus.$off('library-changed', this.libraryChanged)
this.$eventBus.$off('downloads-loaded', this.downloadsLoaded)
if (this.$server.socket) {
this.$server.socket.off('audiobook_updated', this.audiobookUpdated)
this.$server.socket.off('audiobook_added', this.audiobookAdded)
this.$server.socket.off('audiobook_removed', this.audiobookRemoved)
this.$server.socket.off('audiobooks_updated', this.audiobooksUpdated)
this.$server.socket.off('audiobooks_added', this.audiobooksAdded)
} else {
console.error('Error socket not initialized')
}
2021-12-04 19:56:29 -06:00
}
},
mounted() {
this.initListeners()
2022-01-16 14:14:02 -06:00
if (this.$server.initialized && this.currentLibraryId) {
2021-12-04 19:56:29 -06:00
this.fetchCategories()
} else {
this.shelves = this.downloadOnlyShelves
}
},
2021-12-04 19:56:29 -06:00
beforeDestroy() {
this.removeListeners()
2021-12-04 19:56:29 -06:00
}
}
</script>