|
1 | 1 | <template>
|
2 |
| - <ContentLoader v-slot :loading="result == null"> |
3 |
| - <div v-if="result.artists.length > 0" class="mb-5"> |
4 |
| - <h1>Artists</h1> |
5 |
| - <ArtistList :items="result.artists" /> |
6 |
| - </div> |
| 2 | + <div> |
7 | 3 | <div v-if="result.albums.length > 0" class="mb-5">
|
8 |
| - <h1>Albums</h1> |
| 4 | + <h1 v-if="!type"> |
| 5 | + Albums |
| 6 | + <router-link v-if="!type" :to="{params: {type: 'album'}, query: $route.query}" class="text-muted"> |
| 7 | + <Icon icon="chevron-right" /> |
| 8 | + </router-link> |
| 9 | + </h1> |
9 | 10 | <AlbumList :items="result.albums" />
|
10 | 11 | </div>
|
| 12 | + |
| 13 | + <div v-if="result.artists.length > 0" class="mb-5"> |
| 14 | + <h1 v-if="!type"> |
| 15 | + Artists |
| 16 | + <router-link v-if="!type" :to="{params: {type: 'artist'}, query: $route.query}" class="text-muted"> |
| 17 | + <Icon icon="chevron-right" /> |
| 18 | + </router-link> |
| 19 | + </h1> |
| 20 | + <ArtistList :items="result.artists" /> |
| 21 | + </div> |
| 22 | + |
11 | 23 | <div v-if="result.tracks.length > 0">
|
12 |
| - <h1>Tracks</h1> |
| 24 | + <h1 v-if="!type"> |
| 25 | + Tracks |
| 26 | + <router-link :to="{params: {type: 'track'}, query: $route.query}" class="text-muted"> |
| 27 | + <Icon icon="chevron-right" /> |
| 28 | + </router-link> |
| 29 | + </h1> |
13 | 30 | <TrackList :tracks="result.tracks" />
|
14 | 31 | </div>
|
15 |
| - <EmptyIndicator v-if="!hasResult" label="No results" /> |
16 |
| - </ContentLoader> |
| 32 | + |
| 33 | + <EmptyIndicator v-if="!loading && !hasResult" label="No results" /> |
| 34 | + |
| 35 | + <InfiniteLoader :key="key" :loading="loading" :has-more="type ? hasMore : false" @load-more="loadMore" /> |
| 36 | + </div> |
17 | 37 | </template>
|
18 | 38 | <script lang="ts">
|
19 | 39 | import { defineComponent } from 'vue'
|
|
28 | 48 | TrackList,
|
29 | 49 | },
|
30 | 50 | props: {
|
31 |
| - query: { type: String, required: true } |
| 51 | + query: { type: String, required: true }, |
| 52 | + type: { type: String, default: null }, |
32 | 53 | },
|
33 | 54 | data() {
|
34 | 55 | return {
|
35 |
| - result: null as any, |
| 56 | + result: { |
| 57 | + albums: [] as any[], |
| 58 | + artists: [] as any[], |
| 59 | + tracks: [] as any[], |
| 60 | + }, |
| 61 | + loading: true, |
| 62 | + offset: 0 as number, |
| 63 | + hasMore: true, |
36 | 64 | }
|
37 | 65 | },
|
38 | 66 | computed: {
|
| 67 | + key(): string { |
| 68 | + return '' + this.type + this.query |
| 69 | + }, |
39 | 70 | hasResult(): boolean {
|
40 |
| - return this.result && ( |
| 71 | + return this.result.albums.length > 0 || |
41 | 72 | this.result.artists.length > 0 ||
|
42 |
| - this.result.albums.length > 0 || |
43 |
| - this.result.tracks.length > 0) |
44 |
| - } |
| 73 | + this.result.tracks.length > 0 |
| 74 | + }, |
45 | 75 | },
|
46 | 76 | watch: {
|
47 |
| - query: { |
| 77 | + key: { |
48 | 78 | immediate: true,
|
49 |
| - handler(value: string) { |
50 |
| - this.$api.search(value).then(result => { |
51 |
| - this.result = result |
52 |
| - }) |
| 79 | + handler() { |
| 80 | + this.result.artists = [] |
| 81 | + this.result.albums = [] |
| 82 | + this.result.tracks = [] |
| 83 | + this.offset = 0 |
| 84 | + this.hasMore = true |
| 85 | + this.loading = true |
53 | 86 | }
|
54 |
| - } |
| 87 | + }, |
55 | 88 | },
|
| 89 | + methods: { |
| 90 | + async loadMore() { |
| 91 | + this.loading = true |
| 92 | + const result = await this.$api.search(this.query, this.type, this.offset) |
| 93 | + const size = result.albums.length + result.artists.length + result.tracks.length |
| 94 | +
|
| 95 | + this.result.albums.push(...result.albums) |
| 96 | + this.result.artists.push(...result.artists) |
| 97 | + this.result.tracks.push(...result.tracks) |
| 98 | +
|
| 99 | + this.offset += size |
| 100 | + this.hasMore = size > 0 |
| 101 | + this.loading = false |
| 102 | + } |
| 103 | + } |
56 | 104 | })
|
57 | 105 | </script>
|
0 commit comments