57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
|
import axios from 'axios';
|
||
|
import { useArrayDataStore } from 'stores/useArrayDataStore';
|
||
|
|
||
|
const arrayDataStore = useArrayDataStore();
|
||
|
const options = {
|
||
|
limit: 10,
|
||
|
};
|
||
|
|
||
|
export function useArrayData(key, userOptions) {
|
||
|
if (!key) throw new Error('ArrayData: A key is required to use this composable');
|
||
|
|
||
|
arrayDataStore.set(key, []);
|
||
|
|
||
|
const data = arrayDataStore.get(key);
|
||
|
|
||
|
if (typeof userOptions === 'object') {
|
||
|
Object.assign(options, userOptions);
|
||
|
}
|
||
|
|
||
|
async function fetch() {
|
||
|
if (!options.url) return;
|
||
|
|
||
|
const filter = {
|
||
|
limit: options.limit,
|
||
|
};
|
||
|
|
||
|
const requestOptions = { params: { filter } };
|
||
|
|
||
|
const response = await axios.get(options.url, requestOptions);
|
||
|
for (const row of response.data) data.push(row);
|
||
|
|
||
|
return response.data;
|
||
|
}
|
||
|
|
||
|
async function apply(filter) {
|
||
|
if (!options.url) return;
|
||
|
|
||
|
const requestOptions = { params: { filter } };
|
||
|
|
||
|
const response = await axios.get(options.url, requestOptions);
|
||
|
for (const row of response.data) data.push(row);
|
||
|
|
||
|
return response.data;
|
||
|
}
|
||
|
|
||
|
async function refresh() {
|
||
|
// TODO
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
fetch,
|
||
|
apply,
|
||
|
refresh,
|
||
|
data,
|
||
|
};
|
||
|
}
|