fix: better handling of YouTube video loading failure

This commit is contained in:
Phan An 2024-05-19 13:56:39 +08:00
parent 31154abbea
commit 27ef78098f

View file

@ -9,12 +9,17 @@
<Btn v-if="!loading" small @click.prevent="loadMore">Load More</Btn>
</template>
<p v-if="loading" class="nope">Loading</p>
<p v-if="loading">Loading</p>
<p v-if="somethingWrong">
Failed to load videos.
<a @click.prevent="loadMore" href="#">Try again</a>
</p>
</div>
</template>
<script lang="ts" setup>
import { defineAsyncComponent, ref, toRefs, watch } from 'vue'
import { computed, defineAsyncComponent, ref, toRefs, watch } from 'vue'
import { youTubeService } from '@/services'
import { useErrorHandler } from '@/composables'
@ -35,7 +40,7 @@ const loadMore = async () => {
try {
const result = await youTubeService.searchVideosBySong(song.value, nextPageToken)
nextPageToken = result.nextPageToken
videos.value.push(...result.items)
videos.value.push(...(result.items || []))
} catch (error: unknown) {
useErrorHandler().handleHttpError(error)
} finally {
@ -43,6 +48,8 @@ const loadMore = async () => {
}
}
const somethingWrong = computed(() => !loading.value && videos.value.length === 0)
watch(song, () => {
videos.value = []
nextPageToken = ''