From 8c93a974e68207f2e17ba9630f7f7adf1f6dafef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sat, 11 Nov 2023 01:47:18 +0000 Subject: [PATCH] add store.on_initialized hooks - start Calendar.toast_timeout when initialized --- ui/src/components/Calendar.vue | 10 ++++++---- ui/src/plugins/store.ts | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/ui/src/components/Calendar.vue b/ui/src/components/Calendar.vue index b3006ff..02cf133 100644 --- a/ui/src/components/Calendar.vue +++ b/ui/src/components/Calendar.vue @@ -85,11 +85,13 @@ export default class extends Vue { if (this.store.is_touch_device) return; - this.toast_timeout = setTimeout(() => { - if (this.store.user_doors.length === 0) return; + this.store.when_initialized(() => { + this.toast_timeout = setTimeout(() => { + if (this.store.user_doors.length === 0) return; - this.toast!.show({ duration: 600000, type: "is-warning" }); - }, 10000); + this.toast!.show({ duration: 600000, type: "is-warning" }); + }, 10000); + }); } public door_click(day: number) { diff --git a/ui/src/plugins/store.ts b/ui/src/plugins/store.ts index 8460713..681a09e 100644 --- a/ui/src/plugins/store.ts +++ b/ui/src/plugins/store.ts @@ -14,6 +14,8 @@ declare global { type State = { advent22: Advent22; api_creds: RemovableRef; + is_initialized: boolean; + on_initialized: (() => void)[]; is_touch_device: boolean; is_admin: boolean; site_config: SiteConfigModel; @@ -29,6 +31,8 @@ export const advent22Store = defineStore({ state: (): State => ({ advent22: new Advent22(), api_creds: useLocalStorage("advent22/auth", ["", ""]), + is_initialized: false, + on_initialized: [], is_touch_device: window.matchMedia("(any-hover: none)").matches || "ontouchstart" in window || @@ -96,10 +100,21 @@ export const advent22Store = defineStore({ if (next_door !== null) this.next_door_target = Date.now() + next_door; + + this.is_initialized = true; + for (const callback of this.on_initialized) callback(); }) .catch(this.advent22.alert_user_error); }, + when_initialized(callback: () => void): void { + if (this.is_initialized) { + callback(); + } else { + this.on_initialized.push(callback); + } + }, + update_is_admin(): Promise { return new Promise((resolve, reject) => { this.advent22