2023-08-16 00:43:28 +02:00
|
|
|
import {ref, computed, onBeforeUnmount} from 'vue';
|
2023-07-27 17:22:06 +02:00
|
|
|
import {router} from '@inertiajs/vue3';
|
2023-08-25 00:23:38 +02:00
|
|
|
import useQueueEvents from './useQueueEvents.js';
|
2023-07-27 17:22:06 +02:00
|
|
|
|
2023-08-16 00:43:28 +02:00
|
|
|
export function useIndex(props, siteName) {
|
2023-08-25 00:23:38 +02:00
|
|
|
const {startListener, stopListener} = useQueueEvents(siteName, () => reload(false));
|
2023-07-27 17:22:06 +02:00
|
|
|
const rawProps = JSON.parse(JSON.stringify(props));
|
|
|
|
const inner = {
|
|
|
|
data: ref(rawProps.data),
|
|
|
|
meta: ref(rawProps.meta),
|
|
|
|
};
|
|
|
|
|
|
|
|
function toFilterString(data) {
|
|
|
|
return btoa(encodeURIComponent(JSON.stringify(data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
const filterString = computed(() => toFilterString(inner.meta.value.filter));
|
|
|
|
|
2023-10-30 23:46:41 +01:00
|
|
|
function reload(resetPage = true, withMeta = true) {
|
2023-07-27 17:22:06 +02:00
|
|
|
var data = {
|
|
|
|
filter: filterString.value,
|
|
|
|
page: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
data['page'] = resetPage ? 1 : inner.meta.value.current_page;
|
|
|
|
|
|
|
|
router.visit(window.location.pathname, {
|
|
|
|
data,
|
|
|
|
preserveState: true,
|
|
|
|
onSuccess: (page) => {
|
|
|
|
inner.data.value = page.props.data.data;
|
2023-10-30 23:46:41 +01:00
|
|
|
if (withMeta) {
|
|
|
|
inner.meta.value = page.props.data.meta;
|
|
|
|
}
|
2023-07-27 17:22:06 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function can(permission) {
|
|
|
|
return inner.meta.value.can[permission];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilter(value) {
|
|
|
|
return inner.meta.value.filter[value];
|
|
|
|
}
|
|
|
|
|
|
|
|
function setFilter(key, value) {
|
|
|
|
inner.meta.value.filter[key] = value;
|
2023-10-30 23:46:41 +01:00
|
|
|
reload(true, false);
|
2023-07-27 17:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function requestCallback(successMessage, failureMessage) {
|
|
|
|
return {
|
|
|
|
onSuccess: () => {
|
|
|
|
this.$success(successMessage);
|
|
|
|
reload(false);
|
|
|
|
},
|
|
|
|
onFailure: () => {
|
|
|
|
this.$error(failureMessage);
|
|
|
|
reload(false);
|
|
|
|
},
|
|
|
|
preserveState: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-25 00:26:21 +02:00
|
|
|
startListener();
|
2023-08-25 00:23:38 +02:00
|
|
|
onBeforeUnmount(() => stopListener());
|
2023-08-14 23:57:15 +02:00
|
|
|
|
2023-07-27 17:22:06 +02:00
|
|
|
return {
|
|
|
|
data: inner.data,
|
|
|
|
reload,
|
|
|
|
can,
|
|
|
|
getFilter,
|
|
|
|
setFilter,
|
|
|
|
requestCallback,
|
|
|
|
meta: inner.meta,
|
|
|
|
filterString,
|
|
|
|
router,
|
|
|
|
toFilterString,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const indexProps = {
|
|
|
|
data: {
|
|
|
|
default: () => {
|
|
|
|
return {data: [], meta: {}};
|
|
|
|
},
|
|
|
|
type: Object,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export {indexProps};
|