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

93 lines
2.3 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">
<a
v-for="video in videos"
2022-04-15 14:24:30 +00:00
:key="video.id.videoId"
:href="`https://youtu.be/${video.id.videoId}`"
2022-04-15 14:24:30 +00:00
class="video"
data-testid="youtube-search-result"
role="button"
@click.prevent="play(video)"
2022-04-15 14:24:30 +00:00
>
<div class="thumb">
<img :alt="video.snippet.title" :src="video.snippet.thumbnails.default.url" width="90">
2022-04-15 14:24:30 +00:00
</div>
<div class="meta">
<h3 class="title">{{ video.snippet.title }}</h3>
<p class="desc">{{ video.snippet.description }}</p>
</div>
</a>
<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-else>Play a song to retrieve related YouTube videos.</p>
<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-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
watchEffect(() => (videos.value = song.value.youtube?.items || []))
2022-04-15 14:24:30 +00:00
2022-04-24 08:50:45 +00:00
const play = (video: YouTubeVideo) => youTubeService.play(video)
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
} finally {
loading.value = false
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
}
2022-04-15 14:24:30 +00:00
</script>
<style lang="scss" scoped>
.youtube-extra-wrapper {
overflow-x: hidden;
.video {
display: flex;
padding: 12px 0;
.thumb {
margin-right: 10px;
}
.title {
font-size: 1.1rem;
margin-bottom: .4rem;
}
.desc {
font-size: .9rem;
}
&:hover, &:active, &:focus {
color: var(--color-text-primary);
}
&:last-of-type {
margin-bottom: 16px;
}
}
}
</style>