From a89856c5e0acebbdf375db205317118bed5dd9cb Mon Sep 17 00:00:00 2001 From: joan Date: Sat, 14 Jan 2023 13:48:57 +0100 Subject: [PATCH] Searchbar --- src/components/PaginateData.vue | 16 +++--- src/composables/useArrayData.js | 78 +++++++++++++++++++++-------- src/pages/Customer/CustomerList.vue | 28 +---------- src/pages/Customer/CustomerMain.vue | 70 ++++++++++++++++++++++++-- src/router/modules/customer.js | 2 +- src/stores/useArrayDataStore.js | 11 +++- 6 files changed, 139 insertions(+), 66 deletions(-) diff --git a/src/components/PaginateData.vue b/src/components/PaginateData.vue index 5c2562d60..c9b67e9e3 100644 --- a/src/components/PaginateData.vue +++ b/src/components/PaginateData.vue @@ -26,15 +26,11 @@ const $props = defineProps({ type: Object, default: null, }, - sortBy: { + order: { type: String, default: '', }, limit: { - type: String, - default: '', - }, - rowsPerPage: { type: Number, default: 10, }, @@ -47,8 +43,8 @@ const $props = defineProps({ const emit = defineEmits(['onFetch', 'onPaginate']); const isLoading = ref(false); const pagination = ref({ - sortBy: $props.sortBy, - rowsPerPage: $props.rowsPerPage, + sortBy: $props.order, + rowsPerPage: $props.limit, page: 1, }); @@ -56,8 +52,8 @@ const arrayData = useArrayData('customers', { url: $props.url, filter: $props.filter, where: $props.where, - limit: $props.rowsPerPage, - order: $props.sortBy, + limit: $props.limit, + order: $props.order, }); const store = arrayData.store; @@ -136,7 +132,7 @@ async function onLoad(...params) {
- + diff --git a/src/composables/useArrayData.js b/src/composables/useArrayData.js index 00e02a32a..5ce4f03fb 100644 --- a/src/composables/useArrayData.js +++ b/src/composables/useArrayData.js @@ -1,4 +1,5 @@ import { ref, onUnmounted } from 'vue'; +import { useRouter, useRoute } from 'vue-router'; import axios from 'axios'; import { useArrayDataStore } from 'stores/useArrayDataStore'; @@ -12,18 +13,18 @@ export function useArrayData(key, userOptions) { } const store = arrayDataStore.get(key); - const filter = ref({}); - const userFilter = ref({}); - const userParams = ref({}); const hasMoreData = ref(true); - const options = ref({ - limit: 10, - skip: 0, - }); + const router = useRouter(); + const route = useRoute(); + const page = ref(1); if (typeof userOptions === 'object') { - Object.assign(options.value, userOptions); + if (userOptions.url) store.url = userOptions.url; + if (userOptions.limit) store.limit = userOptions.limit; + if (userOptions.order) store.order = userOptions.order; + //Object.assign(store.value, userOptions); + //console.log(options.value); } // if (typeof userOptions === 'object' && userOptions.filter) { @@ -36,21 +37,22 @@ export function useArrayData(key, userOptions) { }); async function fetch({ append = false }) { - if (!options.value.url) return; + if (!store.url) return; - const fetchFilter = { - order: options.value.order, - limit: options.value.limit, - skip: options.value.skip, + const filter = { + order: store.order, + limit: store.limit, + skip: store.skip, }; - Object.assign(fetchFilter, userFilter.value); - filter.value = fetchFilter; + Object.assign(filter, store.userFilter); - const requestOptions = { params: { filter: filter.value } }; - const response = await axios.get(options.value.url, requestOptions); + store.filter = filter; - const { limit } = filter.value; + const requestOptions = { params: { filter: filter } }; + const response = await axios.get(store.url, requestOptions); + + const { limit } = filter; hasMoreData.value = response.data.length === limit; @@ -61,11 +63,30 @@ export function useArrayData(key, userOptions) { if (append === false) { store.data = response.data; } + + updateStateParams(); + } + + async function request({ userFilter }) { + if (!store.url) return; + + const filter = { + order: store.order, + limit: store.limit, + skip: store.skip, + }; + + Object.assign(filter, userFilter); + + const requestOptions = { params: { filter: filter } }; + const response = await axios.get(store.url, requestOptions); + + return response.data; } async function apply({ filter, params }) { - if (filter) userFilter.value = filter; - if (params) userParams.value = params; + if (filter) store.userFilter = filter; + if (params) store.userParams = params; await fetch({ append: false }); } @@ -73,22 +94,35 @@ export function useArrayData(key, userOptions) { async function loadMore() { if (!hasMoreData.value) return; - options.value.skip = options.value.limit * (page.value - 1); + store.skip = store.limit * (page.value - 1); page.value += 1; await fetch({ append: true }); } async function refresh() { - // TODO + // TODO: Refresh + } + + function updateStateParams() { + router.replace({ + path: route.path, + query: { + order: store.order, + limit: store.limit, + skip: store.skip, + }, + }); } return { fetch, apply, refresh, + request, loadMore, store, hasMoreData, + updateStateParams, }; } diff --git a/src/pages/Customer/CustomerList.vue b/src/pages/Customer/CustomerList.vue index d7012a920..af0b3ce32 100644 --- a/src/pages/Customer/CustomerList.vue +++ b/src/pages/Customer/CustomerList.vue @@ -1,22 +1,14 @@