From 9dbefe5a8ad3b85bd39114cda4061d9aefe5a344 Mon Sep 17 00:00:00 2001 From: James Collins Date: Fri, 19 May 2023 14:00:15 +1000 Subject: [PATCH] add page loading icon --- resources/js/components/SMLoadingIcon.vue | 1 + resources/js/router/index.js | 33 +++++++++++++++-------- resources/js/store/ApplicationStore.ts | 2 ++ resources/js/views/App.vue | 22 +++++++++++++++ 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/resources/js/components/SMLoadingIcon.vue b/resources/js/components/SMLoadingIcon.vue index 6e29fe3..1f7560c 100644 --- a/resources/js/components/SMLoadingIcon.vue +++ b/resources/js/components/SMLoadingIcon.vue @@ -48,6 +48,7 @@ const props = defineProps({ div:nth-child(4) { left: 2em; animation: loading-icon3 0.6s infinite; + z-index: -1; } @keyframes loading-icon1 { diff --git a/resources/js/router/index.js b/resources/js/router/index.js index f99c952..d37286a 100644 --- a/resources/js/router/index.js +++ b/resources/js/router/index.js @@ -429,19 +429,18 @@ router.beforeEach(async (to, from, next) => { applicationStore.clearDynamicTitle(); + if (applicationStore.pageLoaderTimeout !== 0) { + window.clearTimeout(applicationStore.pageLoaderTimeout); + applicationStore.pageLoaderTimeout = window.setTimeout(() => { + const pageLoadingElem = document.getElementById("sm-page-loading"); + if (pageLoadingElem !== null) { + pageLoadingElem.style.display = "flex"; + } + }, 0); + } + if (to.meta.middleware == "authenticated") { if (userStore.id) { - // try { - // let res = await api.get({ - // url: "/me", - // }); - // userStore.setUserDetails(res.json.user); - // } catch (err) { - // if (err.status == 401) { - // userStore.clearUser(); - // } - // } - api.get({ url: "/me", }) @@ -482,6 +481,8 @@ router.beforeEach(async (to, from, next) => { }); router.afterEach((to, from) => { + const applicationStore = useApplicationStore(); + if (from.name !== undefined) { document.body.classList.remove(`page-${from.name}`); } @@ -550,6 +551,16 @@ router.afterEach((to, from) => { } } }, 10); + + if (applicationStore.pageLoaderTimeout !== 0) { + window.clearTimeout(applicationStore.pageLoaderTimeout); + applicationStore.pageLoaderTimeout = 0; + } + + const pageLoadingElem = document.getElementById("sm-page-loading"); + if (pageLoadingElem !== null) { + pageLoadingElem.style.display = "none"; + } }); export default router; diff --git a/resources/js/store/ApplicationStore.ts b/resources/js/store/ApplicationStore.ts index 1e83a39..c1bc1ea 100644 --- a/resources/js/store/ApplicationStore.ts +++ b/resources/js/store/ApplicationStore.ts @@ -5,6 +5,7 @@ type ApplicationStoreEventKeyUpCallback = (event: KeyboardEvent) => boolean; export interface ApplicationStore { dynamicTitle: string; eventKeyUpStack: ApplicationStoreEventKeyUpCallback[]; + pageLoaderTimeout: number; _addedListener: boolean; } @@ -13,6 +14,7 @@ export const useApplicationStore = defineStore({ state: (): ApplicationStore => ({ dynamicTitle: "", eventKeyUpStack: [], + pageLoaderTimeout: 0, _addedListener: false, }), diff --git a/resources/js/views/App.vue b/resources/js/views/App.vue index 16d817f..94771c2 100644 --- a/resources/js/views/App.vue +++ b/resources/js/views/App.vue @@ -10,6 +10,9 @@ +
+ +
@@ -19,6 +22,7 @@ import SMNavbar from "../components/SMNavbar.vue"; import SMPageFooter from "../components/SMPageFooter.vue"; import SMToastList from "../components/SMToastList.vue"; import SMDialogList from "../components/SMDialog"; +import SMLoadingIcon from "../components/SMLoadingIcon.vue";