koel/resources/assets/js/components/ui/YouTubeVideoList.vue

61 lines
1.6 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<div class="youtube-extra-wrapper">
<template v-if="videos.length">
<YouTubeVideo v-for="video in videos" :key="video.id.videoId" :video="video"/>
<Btn v-if="!loading" class="more" data-testid="youtube-search-more-btn" @click.prevent="loadMore">Load More</Btn>
2022-04-15 14:24:30 +00:00
</template>
<p class="nope" v-show="loading">Loading</p>
</div>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { defineAsyncComponent, ref, toRefs, watchEffect } from 'vue'
2022-04-24 08:50:45 +00:00
import { youTubeService } from '@/services'
2022-04-15 14:24:30 +00:00
2022-04-21 18:39:18 +00:00
const Btn = defineAsyncComponent(() => import('@/components/ui/Btn.vue'))
const YouTubeVideo = defineAsyncComponent(() => import('@/components/ui/YouTubeVideoItem.vue'))
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const props = defineProps<{ song: Song }>()
const { song } = toRefs(props)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const loading = ref(false)
const videos = ref<YouTubeVideo[]>([])
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const loadMore = async () => {
loading.value = true
try {
song.value.youtube = song.value.youtube || { nextPageToken: '', items: [] }
2022-04-24 08:50:45 +00:00
const result = await youTubeService.searchVideosRelatedToSong(song.value, song.value.youtube.nextPageToken!)
2022-04-15 17:00:08 +00:00
song.value.youtube.nextPageToken = result.nextPageToken
song.value.youtube.items.push(...result.items as YouTubeVideo[])
videos.value = song.value.youtube.items
2022-06-10 10:47:46 +00:00
} catch (err) {
console.error(err)
2022-04-15 17:00:08 +00:00
} finally {
loading.value = false
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
}
2022-06-10 10:47:46 +00:00
watchEffect(() => {
videos.value = song.value.youtube?.items || []
if (videos.value.length === 0) {
loadMore()
}
})
2022-04-15 14:24:30 +00:00
</script>
<style lang="scss" scoped>
.youtube-extra-wrapper {
overflow-x: hidden;
a:last-of-type {
margin-bottom: 16px;
2022-04-15 14:24:30 +00:00
}
}
</style>