index.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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="true" />
  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-if="authors.length === 0" class="text-center pa-4">
  13. <div>No authors found</div>
  14. </v-card>
  15. <v-card v-else class="pa-4">
  16. <v-expansion-panels class="w-80 flex-column" multiple>
  17. <AuthorPanel
  18. v-for="author in authors"
  19. :key="author.id"
  20. :author="author"
  21. />
  22. </v-expansion-panels>
  23. </v-card>
  24. </div>
  25. </template>
  26. <script setup lang="ts">
  27. import { ref, onMounted } from 'vue'
  28. import AuthorPanel from '~/components/AuthorPanel.vue'
  29. // Set page title
  30. useHead({
  31. title: 'Authors'
  32. })
  33. // Define types
  34. interface Author {
  35. id: number
  36. name: string
  37. }
  38. // Fetch authors from API
  39. const authors = ref<Author[]>([])
  40. const error = ref<string | null>(null)
  41. const pending = ref(true)
  42. onMounted(async () => {
  43. try {
  44. const response = await fetch('https://local.api.snc-demo.fr/api/authors')
  45. if (!response.ok) {
  46. throw new Error(`HTTP error. Status: ${response.status}`)
  47. }
  48. const data = await response.json()
  49. authors.value = data['member'] || []
  50. } catch (err) {
  51. error.value = err instanceof Error ? err.message : 'Unknown error'
  52. console.error('Error fetching authors:', err)
  53. } finally {
  54. pending.value = false
  55. }
  56. })
  57. </script>