diff --git a/src/__tests__/App.spec.js b/src/__tests__/App.spec.js index c5be66823..8f87dbfb6 100644 --- a/src/__tests__/App.spec.js +++ b/src/__tests__/App.spec.js @@ -52,8 +52,7 @@ describe('App', () => { } }; - await vm.responseError(response); - + expect(vm.responseError(response)).rejects.toEqual(expect.objectContaining(response)); expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining( { type: 'negative', @@ -73,8 +72,7 @@ describe('App', () => { } }; - await vm.responseError(response); - + expect(vm.responseError(response)).rejects.toEqual(expect.objectContaining(response)); expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining( { type: 'negative', diff --git a/src/components/FetchData.vue b/src/components/FetchData.vue index 0eee59f90..0110e5576 100644 --- a/src/components/FetchData.vue +++ b/src/components/FetchData.vue @@ -15,6 +15,18 @@ const $props = defineProps({ type: Object, default: null, }, + where: { + type: Object, + default: null, + }, + sortBy: { + type: String, + default: '', + }, + limit: { + type: String, + default: '', + }, }); defineExpose({ fetch }); @@ -27,8 +39,13 @@ onMounted(async () => { }); async function fetch() { + const filter = Object.assign({}, $props.filter); + if ($props.where) filter.where = $props.where; + if ($props.sortBy) filter.order = $props.sortBy; + if ($props.limit) filter.limit = $props.limit; + const { data } = await axios.get($props.url, { - params: { filter: $props.filter }, + params: { filter }, }); emit('onFetch', data); diff --git a/src/components/SmartCard.vue b/src/components/Paginate.vue similarity index 70% rename from src/components/SmartCard.vue rename to src/components/Paginate.vue index 1e6038450..cd47ed331 100644 --- a/src/components/SmartCard.vue +++ b/src/components/Paginate.vue @@ -6,6 +6,10 @@ import { useI18n } from 'vue-i18n'; const { t } = useI18n(); const $props = defineProps({ + autoLoad: { + type: Boolean, + default: false, + }, url: { type: String, default: '', @@ -18,14 +22,18 @@ const $props = defineProps({ type: Object, default: null, }, - autoLoad: { - type: Boolean, - default: false, + where: { + type: Object, + default: null, }, sortBy: { type: String, default: '', }, + limit: { + type: String, + default: '', + }, rowsPerPage: { type: Number, default: 10, @@ -37,10 +45,10 @@ const $props = defineProps({ }); defineEmits(['onNavigate']); -defineExpose({ fetch }); +defineExpose({ refresh }); onMounted(() => { - if ($props.autoLoad) fetch(); + if ($props.autoLoad) paginate(); }); watch( @@ -60,12 +68,10 @@ const pagination = ref({ const rows = ref(null); async function fetch() { - const { page, rowsPerPage, sortBy, descending } = pagination.value; + const { page, rowsPerPage, sortBy } = pagination.value; if (!$props.url) return; - isLoading.value = true; - const filter = { limit: rowsPerPage, skip: rowsPerPage * (page - 1), @@ -73,12 +79,26 @@ async function fetch() { Object.assign(filter, $props.filter); + if ($props.where) filter.where = $props.where; + if ($props.sortBy) filter.order = $props.sortBy; + if ($props.limit) filter.limit = $props.limit; + if (sortBy) filter.order = sortBy; const { data } = await axios.get($props.url, { params: { filter }, }); + isLoading.value = false; + + return data; +} + +async function paginate() { + const { page, rowsPerPage, sortBy, descending } = pagination.value; + + const data = await fetch(); + if (!data) { isLoading.value = false; return; @@ -98,13 +118,31 @@ async function fetch() { isLoading.value = false; } +async function refresh() { + const { rowsPerPage } = pagination.value; + + const data = await fetch(); + + if (!data) { + isLoading.value = false; + return; + } + + hasMoreData.value = data.length === rowsPerPage; + + if (!rows.value) rows.value = []; + rows.value = data; + + isLoading.value = false; +} + async function onLoad(...params) { const done = params[1]; if (!rows.value || rows.value.length === 0 || !$props.url) return done(false); pagination.value.page = pagination.value.page + 1; - await fetch(); + await paginate(); const endOfPages = !hasMoreData.value; done(endOfPages); @@ -114,34 +152,7 @@ async function onLoad(...params) {