From 36b5f74414413e7270d47f52df387697698aeeaf Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Oct 2022 22:31:35 +0200 Subject: Add pagination --- components/ModelForm.vue | 2 ++ components/Pagination.vue | 35 +++++++++++++++++++++++++++++++++++ pages/index.vue | 16 ++++++++++++++-- pages/search.vue | 14 ++++++++++++-- pages/user/_id.vue | 19 ++++++++++++++++--- store/users.js | 6 ++++-- 6 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 components/Pagination.vue diff --git a/components/ModelForm.vue b/components/ModelForm.vue index d59a5ae..f7b622c 100644 --- a/components/ModelForm.vue +++ b/components/ModelForm.vue @@ -248,6 +248,8 @@ export default { this.$toast.error(response.data.error); } }); + } else { + this.$toast.error("Fill all the required fields"); } event.preventDefault(); }, diff --git a/components/Pagination.vue b/components/Pagination.vue new file mode 100644 index 0000000..053d7cf --- /dev/null +++ b/components/Pagination.vue @@ -0,0 +1,35 @@ + + + diff --git a/pages/index.vue b/pages/index.vue index 27c157a..5884e7c 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -19,11 +19,13 @@ :key="model.id" :model="model" ) + pagination(:page="page" :pages="pages" v-if="count") diff --git a/pages/search.vue b/pages/search.vue index 1c09e3b..6f448af 100644 --- a/pages/search.vue +++ b/pages/search.vue @@ -21,11 +21,13 @@ :key="model.id" :model="model" ) + pagination(:page="page" :pages="pages" v-if="count") diff --git a/pages/user/_id.vue b/pages/user/_id.vue index a43a7c8..797f2c0 100644 --- a/pages/user/_id.vue +++ b/pages/user/_id.vue @@ -19,6 +19,7 @@ :key="model.id" :model="model" ) + pagination(:page="page" :pages="pages" v-if="count") h4.text-lg(v-else class="dark:text-white") No models found @@ -28,6 +29,7 @@ import UserAvatar from "@/components/UserAvatar.vue"; import FilePreview from "@/components/FilePreview.vue"; import ModelLoading from "@/components/ModelLoading.vue"; import ModelBoxCard from "@/components/ModelBoxCard.vue"; +import Pagination from "@/components/Pagination.vue"; import { mapGetters } from "vuex"; @@ -39,6 +41,9 @@ export default { id: 0, user: {}, models: [], + count: 0, + page: 0, + pages: 0, }; }, head() { @@ -51,12 +56,14 @@ export default { "file-preview": FilePreview, "model-loading": ModelLoading, "model-box-card": ModelBoxCard, + pagination: Pagination, }, computed: { ...mapGetters(["isLoading"]), }, created() { this.id = this.$route.params.id; + this.page = this.$route.query.page ?? 0; this.$store.dispatch("users/findById", this.id).then((response) => { if (response.status != 200) { @@ -66,9 +73,15 @@ export default { } }); - this.$store.dispatch("users/findModels", this.id).then((response) => { - if (response.status == 200) this.models = response.data.results; - }); + this.$store + .dispatch("users/findModels", { id: this.id, page: this.page }) + .then((response) => { + if (response.status == 200) { + this.models = response.data.results; + this.count = response.data.count; + this.pages = Math.ceil(this.count / 20); + } + }); }, }; diff --git a/store/users.js b/store/users.js index cf1ca86..90e4dad 100644 --- a/store/users.js +++ b/store/users.js @@ -39,12 +39,14 @@ export const actions = { return res; }, // Search user models - async findModels({ commit }, id) { + async findModels({ commit }, data) { commit("loadingStatus", true, { root: true }); let res = { status: 0, data: null }; let api = this.$config.api; + const id = data.id; + const page = data.page ? data.page : 0; - await fetch(`${api}/v1/users/${id}/models`, { + await fetch(`${api}/v1/users/${id}/models?page=${page}`, { headers: { "Content-Type": "application/json", }, -- cgit v1.2.3-18-g5258