index.vue 2.1 KB

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