add infinite loading to album view

This commit is contained in:
Thomas Amland
2020-09-20 17:33:21 +02:00
parent 6c290689c4
commit 4be81d0e35
6 changed files with 83 additions and 16 deletions
+21 -10
View File
@@ -7,9 +7,8 @@
</router-link>
</li>
</ul>
<ContentLoader #default :loading="albums == null">
<AlbumList :items="albums" />
</ContentLoader>
<AlbumList :items="albums" />
<InfiniteLoader :key="sort" :loading="loading" :has-more="hasMore" @loadMore="loadMore" />
</div>
</template>
<script lang="ts">
@@ -26,7 +25,10 @@
},
data() {
return {
albums: null as null | Album[],
albums: [] as | Album[],
loading: false,
offset: 0 as number,
hasMore: true,
}
},
computed: {
@@ -42,14 +44,23 @@
},
watch: {
sort: {
immediate: true,
handler(value: AlbumSort) {
this.albums = null
this.$api.getAlbums(value).then(albums => {
this.albums = albums
})
handler() {
this.albums = []
this.offset = 0
this.hasMore = true
}
}
},
methods: {
loadMore() {
this.loading = true
this.$api.getAlbums(this.sort as AlbumSort, 50, this.offset).then(albums => {
this.albums.push(...albums)
this.offset += albums.length
this.hasMore = albums.length > 0
this.loading = false
})
}
}
})
</script>
+2 -6
View File
@@ -142,7 +142,7 @@ export class API {
.map(this.normalizeArtist, this)
}
async getAlbums(sort: AlbumSort, size = 500): Promise<Album[]> {
async getAlbums(sort: AlbumSort, size: number, offset = 0): Promise<Album[]> {
const type = {
'a-z': 'alphabeticalByName',
'recently-added': 'newest',
@@ -151,11 +151,7 @@ export class API {
random: 'random',
}[sort]
const params = {
type,
offset: '0',
size: size,
}
const params = { type, offset, size }
const response = await this.get('rest/getAlbumList2', params)
const albums = response.albumList2?.album || []
return albums.map(this.normalizeAlbum, this)
+52
View File
@@ -0,0 +1,52 @@
<template>
<infinite-loading ref="inf" force-use-infinite-wrapper @infinite="loadMore">
<template slot="spinner">
<div class="d-flex justify-content-center my-4">
<span class="spinner-border" />
</div>
</template>
<template slot="no-more">
<span />
</template>
<template slot="no-results">
<span />
</template>
</infinite-loading>
</template>
<script lang="ts">
import Vue from 'vue'
import InfiniteLoading from 'vue-infinite-loading'
export default Vue.extend({
components: {
InfiniteLoading,
},
props: {
loading: { type: Boolean, required: true },
hasMore: { type: Boolean, required: true },
},
watch: {
loading: {
handler(value: boolean) {
if (!this.hasMore) {
(this.$refs.inf as any).stateChanger.complete()
} else if (!value) {
(this.$refs.inf as any).stateChanger.loaded()
}
}
},
hasMore: {
handler(value: boolean) {
if (!value) {
(this.$refs.inf as any).stateChanger.complete()
}
}
}
},
methods: {
loadMore() {
this.$emit('loadMore')
},
}
})
</script>
+2
View File
@@ -2,6 +2,7 @@ import Vue from 'vue'
import ContentLoader from './ContentLoader.vue'
import ExternalLink from './ExternalLink.vue'
import Icon from './Icon.vue'
import InfiniteLoader from './InfiniteLoader.vue'
import OverflowMenu from './OverflowMenu.vue'
import Tiles from './Tiles.vue'
import Tile from './Tile.vue'
@@ -10,6 +11,7 @@ const components = {
ContentLoader,
ExternalLink,
Icon,
InfiniteLoader,
OverflowMenu,
Tiles,
Tile,