From 1933603692b636910d04c22e18f64830725a86ae Mon Sep 17 00:00:00 2001 From: Alexandro Uc Santos Date: Sat, 3 Feb 2024 19:30:17 -0600 Subject: [PATCH] add: pagination to locations --- src/components/Pagination.vue | 89 +++++++++++++++++++++++++++++++++++ src/services/company.js | 3 +- src/stores/company.js | 8 +++- src/views/LocationsView.vue | 45 ++++++++++++++++-- 4 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 src/components/Pagination.vue diff --git a/src/components/Pagination.vue b/src/components/Pagination.vue new file mode 100644 index 0000000..65077f4 --- /dev/null +++ b/src/components/Pagination.vue @@ -0,0 +1,89 @@ + + + + + \ No newline at end of file diff --git a/src/services/company.js b/src/services/company.js index dd4cc6e..0f19d8c 100644 --- a/src/services/company.js +++ b/src/services/company.js @@ -115,7 +115,8 @@ export const deleteBudget = async(id) => { export const getLocations = async(filter) => { try { - const endpoint = `/branches/${filter}`; + const endpoint = `/branches/${filter}&$limit=10&$sort%5BcreatedAt%5D=-1`; + console.log(endpoint); const {data} = await api.get(endpoint); return data; } catch (error) { diff --git a/src/stores/company.js b/src/stores/company.js index 22b22e9..91b31a2 100644 --- a/src/stores/company.js +++ b/src/stores/company.js @@ -10,6 +10,8 @@ export const useCompanyStore = defineStore('company', () => { const users = ref([]); const budgets = ref([]); const locations = ref([]); + const locationsTotal = ref(0); + const locationsCurrentPage = ref(1); const proposals = ref([]) const loading = ref(false); @@ -239,6 +241,7 @@ export const useCompanyStore = defineStore('company', () => { console.log(resp); if(resp !== null && resp.total > 0) { locations.value = resp.data; + locationsTotal.value = resp.total; } else { locations.value = []; } @@ -248,10 +251,11 @@ export const useCompanyStore = defineStore('company', () => { const createLocationCompany = async(formData, localData) => { const data = await createLocation(formData); if(data) { - locations.value.push({ + locations.value.unshift({ ...data, ...localData }); + locationsTotal.value++; return 'success'; } else { return 'Algo salio mal, intente más tarde'; @@ -306,6 +310,8 @@ export const useCompanyStore = defineStore('company', () => { budgets, users, locations, + locationsTotal, + locationsCurrentPage, clear, $reset, loading, diff --git a/src/views/LocationsView.vue b/src/views/LocationsView.vue index 9bd5194..9aabaef 100644 --- a/src/views/LocationsView.vue +++ b/src/views/LocationsView.vue @@ -5,6 +5,7 @@ import CardEmpty from '../components/CardEmpty.vue'; import CreateLocationModal from '../components/CreateLocationModal.vue'; import CardLocation from '../components/CardLocation.vue'; +import Pagination from '../components/Pagination.vue'; const companyStore = useCompanyStore(); @@ -18,13 +19,25 @@ getInitData(); }) + const limit = 10; + const getInitData = async() => { loading.value = true; + companyStore.locationsCurrentPage = companyStore.locationsCurrentPage; filterQuery.value.company = "company="+ localStorage.getItem('id'); await companyStore.getLocationsCompany(filterQuery.value, false) loading.value = false; } + const getLocationsByPage = async(data) => { + loading.value = true; + filterQuery.value.company = "company="+ localStorage.getItem('id'); + filterQuery.value.skip = "$skip="+ data.skip; + companyStore.locationsCurrentPage = data.page + await companyStore.getLocationsCompany(filterQuery.value, true) + loading.value = false; + } + const getLocationsWithFilters = async(filter) => { loading.value = true; await companyStore.getLocationsCompany(filter, true); @@ -32,8 +45,12 @@ } watch(query, () => { + filterQuery.value.skip = "$skip="+ 0; + filterQuery.value.limit = "$limit="+ 100; if(query.value.length === 0){ + clearRequest(); filterQuery.value.search = ""; + // filterQuery.value.page = 1 getLocationsWithFilters(filterQuery.value); } }); @@ -48,9 +65,10 @@ } const clearFilter = () => { + clearRequest(); + filterQuery.value.search = ""; filterQuery.value.company = "company="+ localStorage.getItem('id'); - if(query.value == ''){ getInitData(); } else { @@ -58,6 +76,12 @@ } } + const clearRequest = () => { + filterQuery.value.skip = "$skip="+ 0; + filterQuery.value.limit = "$limit="+ limit; + companyStore.locationsCurrentPage = 1; + } + const handleSetCurrentLocation = (location) => { openModal.value = true; locationCurrent.value = location; @@ -66,7 +90,6 @@ const handleResetCurrentBudget = () => { openModal.value = false; locationCurrent.value = null; - console.log('clear location'); } @@ -94,7 +117,9 @@ @click="handleSetCurrentLocation(null)" > Agregar locación - +
+ +
+
-