2022-04-15 14:24:30 +00:00
|
|
|
<template>
|
|
|
|
<div class="youtube-extra-wrapper">
|
|
|
|
<template v-if="videos.length">
|
2022-05-12 17:29:53 +00:00
|
|
|
<YouTubeVideo v-for="video in videos" :key="video.id.videoId" :video="video"/>
|
2022-04-23 22:31:40 +00:00
|
|
|
<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'))
|
2022-05-12 17:29:53 +00:00
|
|
|
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-05-12 17:29:53 +00:00
|
|
|
a:last-of-type {
|
|
|
|
margin-bottom: 16px;
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|