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

65 lines
1.5 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>
2022-07-22 15:03:45 +00:00
<li v-for="video in videos" :key="video.id.videoId" data-testid="youtube-video">
2022-07-07 22:57:25 +00:00
<YouTubeVideo :video="video"/>
</li>
</ul>
2022-07-22 15:03:45 +00:00
<Btn v-if="!loading" class="more" @click.prevent="loadMore">Load More</Btn>
2022-04-15 14:24:30 +00:00
</template>
2022-07-22 15:03:45 +00:00
<p class="nope" v-if="loading">Loading</p>
2022-04-15 14:24:30 +00:00
</div>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-07-07 23:15:38 +00:00
import { defineAsyncComponent, ref, toRefs, watch } from 'vue'
2022-04-24 08:50:45 +00:00
import { youTubeService } from '@/services'
2022-07-20 08:00:02 +00:00
import { logger } from '@/utils'
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-07-07 23:15:38 +00:00
let nextPageToken = ''
2022-04-15 17:00:08 +00:00
const loadMore = async () => {
loading.value = true
try {
2022-07-07 23:15:38 +00:00
const result = await youTubeService.searchVideosBySong(song.value, nextPageToken)
nextPageToken = result.nextPageToken
videos.value.push(...result.items)
2022-06-10 10:47:46 +00:00
} catch (err) {
2022-07-20 08:00:02 +00:00
logger.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
2022-07-07 23:15:38 +00:00
watch(song, () => {
videos.value = []
nextPageToken = ''
loadMore()
}, { immediate: true })
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>