salix-front/src/composables/useArrayData.js

137 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-01-10 13:58:24 +00:00
import { ref, onUnmounted } from 'vue';
2023-01-14 12:48:57 +00:00
import { useRouter, useRoute } from 'vue-router';
2023-01-05 13:57:47 +00:00
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');
2023-01-10 13:58:24 +00:00
if (!arrayDataStore.get(key)) {
arrayDataStore.set(key);
}
2023-01-05 13:57:47 +00:00
2023-01-10 13:58:24 +00:00
const store = arrayDataStore.get(key);
const hasMoreData = ref(true);
2023-01-14 12:48:57 +00:00
const router = useRouter();
const route = useRoute();
2023-01-10 13:58:24 +00:00
const page = ref(1);
2023-01-05 13:57:47 +00:00
if (typeof userOptions === 'object') {
2023-01-14 12:48:57 +00:00
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);
2023-01-05 13:57:47 +00:00
}
2023-01-10 13:58:24 +00:00
// if (typeof userOptions === 'object' && userOptions.filter) {
// Object.assign(filter.value, userOptions.filter);
// }
onUnmounted(() => {
if (arrayDataStore.get(key)) {
arrayDataStore.clear(key);
}
});
async function fetch({ append = false }) {
2023-01-14 12:48:57 +00:00
if (!store.url) return;
2023-01-10 13:58:24 +00:00
2023-01-14 12:48:57 +00:00
const filter = {
order: store.order,
limit: store.limit,
skip: store.skip,
2023-01-05 13:57:47 +00:00
};
2023-01-14 12:48:57 +00:00
Object.assign(filter, store.userFilter);
2023-01-10 13:58:24 +00:00
2023-01-14 12:48:57 +00:00
store.filter = filter;
2023-01-10 13:58:24 +00:00
2023-01-17 13:57:12 +00:00
const params = {
filter: filter,
};
Object.assign(params, store.userParams);
const response = await axios.get(store.url, { params });
2023-01-14 12:48:57 +00:00
const { limit } = filter;
2023-01-05 13:57:47 +00:00
2023-01-10 13:58:24 +00:00
hasMoreData.value = response.data.length === limit;
2023-01-05 13:57:47 +00:00
2023-01-10 13:58:24 +00:00
if (append === true) {
for (const row of response.data) store.data.push(row);
}
if (append === false) {
store.data = response.data;
2023-01-14 12:48:57 +00:00
2023-01-27 08:43:10 +00:00
updateStateParams();
}
2023-01-14 12:48:57 +00:00
}
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;
2023-01-05 13:57:47 +00:00
}
2023-01-10 13:58:24 +00:00
async function apply({ filter, params }) {
2023-01-14 12:48:57 +00:00
if (filter) store.userFilter = filter;
if (params) store.userParams = params;
2023-01-10 13:58:24 +00:00
await fetch({ append: false });
}
2023-01-05 13:57:47 +00:00
2023-01-10 13:58:24 +00:00
async function loadMore() {
if (!hasMoreData.value) return;
2023-01-05 13:57:47 +00:00
2023-01-14 12:48:57 +00:00
store.skip = store.limit * (page.value - 1);
2023-01-10 13:58:24 +00:00
page.value += 1;
2023-01-05 13:57:47 +00:00
2023-01-10 13:58:24 +00:00
await fetch({ append: true });
2023-01-05 13:57:47 +00:00
}
async function refresh() {
2023-01-14 12:48:57 +00:00
// TODO: Refresh
}
function updateStateParams() {
router.replace({
path: route.path,
query: {
order: store.order,
limit: store.limit,
skip: store.skip,
2023-01-27 08:43:10 +00:00
params: [
'a'
],
2023-01-14 12:48:57 +00:00
},
});
2023-01-05 13:57:47 +00:00
}
return {
fetch,
apply,
refresh,
2023-01-14 12:48:57 +00:00
request,
2023-01-10 13:58:24 +00:00
loadMore,
store,
hasMoreData,
2023-01-14 12:48:57 +00:00
updateStateParams,
2023-01-05 13:57:47 +00:00
};
}