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

68 lines
1.7 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">
2022-07-07 22:57:25 +00:00
<ul>
<li v-for="video in videos" :key="video.id.videoId">
<YouTubeVideo :video="video"/>
</li>
</ul>
<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;
2022-07-07 22:57:25 +00:00
ul {
display: flex;
flex-direction: column;
gap: 16px;
margin-bottom: 24px;
2022-04-15 14:24:30 +00:00
}
}
</style>