index.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <main>
  3. <div>
  4. <div v-if="pending">
  5. Pending...
  6. </div>
  7. <div v-else>
  8. <ul>
  9. <li v-for="file in files">
  10. <nuxt-link :to="'/poc/' + file.id" class="mr-3">{{ file.name }}</nuxt-link>
  11. </li>
  12. </ul>
  13. <div class="ma-3">{{ totalItems }} results</div>
  14. <div class="ma-3">
  15. <button @click="goToPreviousPage" class="mr-3">Previous page ({{ pagination.previous }})</button>
  16. <span class="mx-3">Page : {{ page }}</span>
  17. <button @click="goToNextPage">Next page ({{ pagination.next }})</button>
  18. <span class="mx-2"> (Last page : {{ pagination.last }})</span>
  19. </div>
  20. <div class="ma-3">
  21. <nuxt-link to="/poc/new">Create</nuxt-link>
  22. </div>
  23. </div>
  24. </div>
  25. </main>
  26. </template>
  27. <script setup lang="ts">
  28. import {useEntityManager} from "~/composables/data/useEntityManager";
  29. import {computed, ComputedRef, Ref} from "@vue/reactivity";
  30. import ApiResource from "~/models/ApiResource";
  31. import {File} from "~/models/Core/File";
  32. import {Collection, Pagination} from "~/types/data";
  33. import {useLazyAsyncData} from "#app";
  34. const em = useEntityManager()
  35. const page: Ref<number> = ref(1)
  36. const totalItems: ComputedRef<number | undefined> = computed(() => collection.value?.totalItems)
  37. const pagination: ComputedRef<Pagination | undefined> = computed(() => {
  38. return collection.value?.pagination
  39. })
  40. let files: ComputedRef<Array<ApiResource> | undefined> = computed(() => {
  41. return collection.value?.items
  42. })
  43. let { data: collection, pending, refresh } = useLazyAsyncData(
  44. 'files',
  45. () => em.fetchAll(File, page.value)
  46. )
  47. const goToPreviousPage = async function () {
  48. if (page.value > 1) {
  49. page.value--
  50. await refresh()
  51. }
  52. }
  53. const goToNextPage = async function () {
  54. page.value++
  55. await refresh()
  56. }
  57. </script>
  58. <style>
  59. a {
  60. color: blue;
  61. cursor: pointer;
  62. }
  63. a:hover {
  64. text-decoration: underline;
  65. }
  66. button {
  67. border: grey solid 1px;
  68. padding: 5px;
  69. margin: 5px;
  70. cursor: pointer;
  71. }
  72. button:hover {
  73. text-decoration: underline;
  74. }
  75. button:focus {
  76. background-color: lightgrey;
  77. }
  78. </style>