Collection.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!-- Permet de requêter une subResource et de donner son contenu à un slot -->
  2. <template>
  3. <main>
  4. <v-skeleton-loader v-if="pending" :type="loaderType" />
  5. <div v-else>
  6. <!-- Content -->
  7. <slot name="list.item" v-bind="{ items }" />
  8. <!-- New button -->
  9. <v-btn v-if="newLink" class="theme-primary float-right">
  10. <NuxtLink :to="newLink" class="no-decoration">
  11. <v-icon>fa-plus-circle</v-icon>
  12. <span>{{ $t('add') }}</span>
  13. </NuxtLink>
  14. </v-btn>
  15. </div>
  16. <slot />
  17. </main>
  18. </template>
  19. <script setup lang="ts">
  20. import { computed, toRefs } from '@vue/reactivity'
  21. import type { ComputedRef, ToRefs } from '@vue/reactivity'
  22. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  23. import type { Collection } from '~/types/data'
  24. const props = defineProps({
  25. model: {
  26. type: Object,
  27. required: true,
  28. },
  29. parent: {
  30. type: Object,
  31. required: false,
  32. },
  33. loaderType: {
  34. type: String,
  35. required: false,
  36. default: 'text',
  37. },
  38. newLink: {
  39. type: String,
  40. required: false,
  41. },
  42. })
  43. const { model, parent }: ToRefs = toRefs(props)
  44. const { fetchCollection } = useEntityFetch()
  45. const { data: collection, pending } = await fetchCollection(
  46. model.value,
  47. parent.value,
  48. )
  49. const items: ComputedRef<Collection> = computed(
  50. () => collection.value ?? { items: [], pagination: {}, totalItems: 0 },
  51. )
  52. </script>