index.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <div>
  3. <h1 class="text-center mb-6">Authors</h1>
  4. <v-card v-if="pending" class="text-center pa-4">
  5. <v-progress-circular indeterminate />
  6. <div class="mt-2">Loading authors...</div>
  7. </v-card>
  8. <v-card v-else-if="error" class="text-center pa-4 error--text">
  9. <v-icon color="error" large>mdi-alert-circle</v-icon>
  10. <div class="mt-2">Error loading authors: {{ error }}</div>
  11. </v-card>
  12. <v-card v-else>
  13. <v-list>
  14. <v-list-item v-for="author in authors" :key="author.id">
  15. <v-list-item-title>{{ author.name }}</v-list-item-title>
  16. </v-list-item>
  17. <v-list-item v-if="authors.length === 0">
  18. <v-list-item-title class="text-center">No authors found</v-list-item-title>
  19. </v-list-item>
  20. </v-list>
  21. </v-card>
  22. </div>
  23. </template>
  24. <script setup lang="ts">
  25. import { ref, onMounted } from 'vue'
  26. // Set page title
  27. useHead({
  28. title: 'Authors'
  29. })
  30. // Define types
  31. interface Author {
  32. id: number
  33. name: string
  34. }
  35. // Fetch authors from API
  36. const authors = ref<Author[]>([])
  37. const error = ref<string | null>(null)
  38. const pending = ref(true)
  39. onMounted(async () => {
  40. try {
  41. const response = await fetch('https://local.api.snc-demo.fr/api/authors')
  42. if (!response.ok) {
  43. throw new Error(`HTTP error! Status: ${response.status}`)
  44. }
  45. const data = await response.json()
  46. authors.value = data['hydra:member'] || []
  47. } catch (err) {
  48. error.value = err instanceof Error ? err.message : 'Unknown error'
  49. console.error('Error fetching authors:', err)
  50. } finally {
  51. pending.value = false
  52. }
  53. })
  54. </script>