import { onMounted, ref } from 'vue'; import { useRouter, useRoute } from 'vue-router'; import axios from 'axios'; import { useArrayDataStore } from 'stores/useArrayDataStore'; const arrayDataStore = useArrayDataStore(); export function useArrayData(key, userOptions) { if (!key) throw new Error('ArrayData: A key is required to use this composable'); if (!arrayDataStore.get(key)) { arrayDataStore.set(key); } const store = arrayDataStore.get(key); const hasMoreData = ref(false); const router = useRouter(); const route = useRoute(); let canceller = null; const page = ref(1); if (typeof userOptions === 'object') { if (userOptions.filter) store.filter = userOptions.filter; if (userOptions.url) store.url = userOptions.url; if (userOptions.limit) store.limit = userOptions.limit; if (userOptions.order) store.order = userOptions.order; } onMounted(() => { const query = route.query; if (query.params) { store.userParams = JSON.parse(query.params); } }); async function fetch({ append = false }) { if (!store.url) return; cancelRequest(); canceller = new AbortController(); const filter = { order: store.order, limit: store.limit, skip: store.skip, }; Object.assign(filter, store.userFilter); Object.assign(filter, store.filter); store.filter = filter; const params = { filter: JSON.stringify(filter), }; Object.assign(params, store.userParams); const response = await axios.get(store.url, { signal: canceller.signal, params, }); const { limit } = filter; hasMoreData.value = response.data.length === limit; if (append === true) { for (const row of response.data) store.data.push(row); } if (append === false) { store.data = response.data; updateStateParams(); } canceller = null; } function clear() { if (arrayDataStore.get(key)) { arrayDataStore.clear(key); } } function cancelRequest() { if (canceller) { canceller.abort(); canceller = null; } } async function applyFilter({ filter, params }) { if (filter) store.userFilter = filter; if (params) store.userParams = Object.assign({}, params); await fetch({ append: false }); } async function addFilter({ filter, params }) { if (filter) store.userFilter = Object.assign(store.userFilter, filter); if (params) store.userParams = Object.assign(store.userParams, params); await fetch({ append: false }); } async function loadMore() { if (!hasMoreData.value) return; store.skip = store.limit * (page.value - 1); page.value += 1; await fetch({ append: true }); } async function refresh() { await fetch({ append: false }); } function updateStateParams() { const query = {}; if (store.order) query.order = store.order; if (store.limit) query.limit = store.limit; if (store.skip) query.skip = store.skip; if (store.userParams && Object.keys(store.userParams).length !== 0) query.params = JSON.stringify(store.userParams); router.replace({ path: route.path, query: query, }); } return { fetch, applyFilter, addFilter, refresh, clear, loadMore, store, hasMoreData, updateStateParams, }; }