From bfc4663ba4be0dd9821a4f2f44a86c961005f545 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Fri, 13 Oct 2023 13:07:16 +0200 Subject: [PATCH] Add loading for memberships sidebar --- resources/css/app.css | 1 - resources/css/sidebar.css | 3 - resources/js/components/page/Header.vue | 17 ++- resources/js/components/ui/Sidebar.vue | 20 +++ resources/js/composables/useApiIndex.js | 77 +++++++++++ .../js/views/member/MemberMemberships.vue | 126 +++++++----------- resources/js/views/member/VIndex.vue | 101 ++++++++------ resources/js/views/member/index/Actions.vue | 15 ++- 8 files changed, 217 insertions(+), 143 deletions(-) delete mode 100644 resources/css/sidebar.css create mode 100644 resources/js/components/ui/Sidebar.vue create mode 100644 resources/js/composables/useApiIndex.js diff --git a/resources/css/app.css b/resources/css/app.css index 868d7e18..b6440267 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -7,6 +7,5 @@ @import 'layout'; @import 'buttons'; @import 'table'; -@import 'sidebar'; @import 'bool'; @import 'form'; diff --git a/resources/css/sidebar.css b/resources/css/sidebar.css deleted file mode 100644 index 40ff451c..00000000 --- a/resources/css/sidebar.css +++ /dev/null @@ -1,3 +0,0 @@ -.sidebar { - @apply fixed w-max shadow-2xl bg-gray-600 right-0 top-0 h-full; -} diff --git a/resources/js/components/page/Header.vue b/resources/js/components/page/Header.vue index a0654323..a12e87e1 100644 --- a/resources/js/components/page/Header.vue +++ b/resources/js/components/page/Header.vue @@ -6,7 +6,7 @@
- + @@ -14,14 +14,13 @@
- diff --git a/resources/js/components/ui/Sidebar.vue b/resources/js/components/ui/Sidebar.vue new file mode 100644 index 00000000..0614d8d9 --- /dev/null +++ b/resources/js/components/ui/Sidebar.vue @@ -0,0 +1,20 @@ + + + diff --git a/resources/js/composables/useApiIndex.js b/resources/js/composables/useApiIndex.js new file mode 100644 index 00000000..684f2cf1 --- /dev/null +++ b/resources/js/composables/useApiIndex.js @@ -0,0 +1,77 @@ +import {ref, inject, onBeforeUnmount} from 'vue'; +import {router} from '@inertiajs/vue3'; +import useQueueEvents from './useQueueEvents.js'; + +export function useApiIndex(url, siteName) { + const axios = inject('axios'); + const {startListener, stopListener} = useQueueEvents(siteName, () => reload()); + const single = ref(null); + const inner = { + data: ref([]), + meta: ref({}), + }; + + async function reload(resetPage = true) { + var params = { + page: resetPage ? 1 : inner.meta.value.current_page, + }; + + var response = (await axios.post(url, params)).data; + inner.data.value = response.data; + inner.meta.value = response.meta; + } + + function create() { + single.value = JSON.parse(JSON.stringify(inner.meta.value.default)); + } + + function edit(model) { + single.value = JSON.parse(JSON.stringify(model)); + } + + async function submit() { + single.value.id ? await axios.patch(single.value.links.update, single.value) : await axios.post(inner.meta.value.links.store, single.value); + await reload(); + single.value = null; + } + + async function remove(model) { + await axios.delete(model.links.destroy); + await reload(); + } + + function can(permission) { + return inner.meta.value.can[permission]; + } + + function requestCallback(successMessage, failureMessage) { + return { + onSuccess: () => { + this.$success(successMessage); + reload(false); + }, + onFailure: () => { + this.$error(failureMessage); + reload(false); + }, + preserveState: true, + }; + } + + startListener(); + onBeforeUnmount(() => stopListener()); + + return { + data: inner.data, + meta: inner.meta, + single, + create, + edit, + reload, + can, + requestCallback, + router, + submit, + remove, + }; +} diff --git a/resources/js/views/member/MemberMemberships.vue b/resources/js/views/member/MemberMemberships.vue index cbae2762..b1cac382 100644 --- a/resources/js/views/member/MemberMemberships.vue +++ b/resources/js/views/member/MemberMemberships.vue @@ -1,94 +1,62 @@ diff --git a/resources/js/views/member/VIndex.vue b/resources/js/views/member/VIndex.vue index 95f10966..0ea169c5 100644 --- a/resources/js/views/member/VIndex.vue +++ b/resources/js/views/member/VIndex.vue @@ -1,23 +1,17 @@ @@ -121,14 +134,14 @@ import MemberMemberships from './MemberMemberships.vue'; import MemberCourses from './MemberCourses.vue'; import Tags from './Tags.vue'; import Actions from './index/Actions.vue'; -import { indexProps, useIndex } from '../../composables/useIndex.js'; -import { ref, defineProps } from 'vue'; +import {indexProps, useIndex} from '../../composables/useIndex.js'; +import {ref, defineProps} from 'vue'; const single = ref(null); const deleting = ref(null); const props = defineProps(indexProps); -var { router, data, meta, getFilter, setFilter, filterString } = useIndex(props.data, 'member'); +var {router, data, meta, getFilter, setFilter, filterString} = useIndex(props.data, 'member'); function exportMembers() { window.open(`/member-export?filter=${filterString.value}`); @@ -136,7 +149,7 @@ function exportMembers() { async function remove(member) { new Promise((resolve, reject) => { - deleting.value = { resolve, reject, member }; + deleting.value = {resolve, reject, member}; }) .then(() => { router.delete(`/member/${member.id}`); diff --git a/resources/js/views/member/index/Actions.vue b/resources/js/views/member/index/Actions.vue index fcaf9da3..4201880f 100644 --- a/resources/js/views/member/index/Actions.vue +++ b/resources/js/views/member/index/Actions.vue @@ -1,7 +1,7 @@ -