<template>
  <div>
    <header>
      <PageNav />
    </header>
    <main>
      <span>Content</span>

      <VideosList :videos="videos" />
    </main>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
  name: 'IndexPage',

  data(): {
    hasError: boolean
    videos: []
  } {
    return {
      hasError: false,
      videos: [],
    }
  },

  async fetch() {
    try {
      const response = await fetch(
        `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId=${process.env.YOUTUBE_UPLOADS_PLAYLIST_ID}&key=${process.env.YOUTUBE_API_KEY}`
      )
      const json = await response.json()
      this.videos = json.items
    } catch (error: any) {
      this.hasError = true
      throw new Error(error)
    }
  },

  fetchOnServer: true,
})
</script>
