2023-11-04 04:17:36 +00:00
|
|
|
|
import { Credentials, DoorsSaved, SiteConfigModel } from "@/lib/api";
|
2023-11-04 15:58:31 +00:00
|
|
|
|
import { Advent22 } from "@/plugins/advent22";
|
2023-11-02 00:37:00 +00:00
|
|
|
|
import { AxiosBasicCredentials } from "axios";
|
2023-11-01 23:58:09 +00:00
|
|
|
|
import { acceptHMRUpdate, defineStore } from "pinia";
|
|
|
|
|
|
2023-11-05 21:57:50 +00:00
|
|
|
|
declare global {
|
|
|
|
|
interface Navigator {
|
|
|
|
|
readonly msMaxTouchPoints: number;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-01 23:58:09 +00:00
|
|
|
|
|
|
|
|
|
export const advent22Store = defineStore({
|
|
|
|
|
id: "advent22",
|
|
|
|
|
|
2023-11-03 14:40:44 +00:00
|
|
|
|
state: () => ({
|
2023-11-04 15:58:31 +00:00
|
|
|
|
advent22: {} as Advent22,
|
2023-11-05 21:57:50 +00:00
|
|
|
|
api_creds: ["", ""] as Credentials,
|
|
|
|
|
is_touch_device:
|
|
|
|
|
window.matchMedia("(any-hover: none)").matches ||
|
|
|
|
|
"ontouchstart" in window ||
|
|
|
|
|
navigator.maxTouchPoints > 0 ||
|
|
|
|
|
navigator.msMaxTouchPoints > 0,
|
2023-11-03 14:40:44 +00:00
|
|
|
|
is_admin: false,
|
|
|
|
|
site_config: {
|
2023-11-04 16:01:58 +00:00
|
|
|
|
title: document.title,
|
|
|
|
|
subtitle: "",
|
2023-11-03 14:40:44 +00:00
|
|
|
|
content: "",
|
|
|
|
|
footer: "",
|
|
|
|
|
} as SiteConfigModel,
|
2023-11-04 04:17:36 +00:00
|
|
|
|
user_doors: [] as DoorsSaved,
|
2023-11-03 17:07:25 +00:00
|
|
|
|
next_door_target: null as number | null,
|
2023-11-03 14:40:44 +00:00
|
|
|
|
}),
|
2023-11-01 23:58:09 +00:00
|
|
|
|
|
2023-11-02 00:37:00 +00:00
|
|
|
|
getters: {
|
|
|
|
|
axios_creds: (state): AxiosBasicCredentials => {
|
|
|
|
|
const [username, password] = state.api_creds;
|
|
|
|
|
return { username: username, password: password };
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-11-01 23:58:09 +00:00
|
|
|
|
|
|
|
|
|
actions: {
|
2023-11-04 15:58:31 +00:00
|
|
|
|
init(advent22: Advent22): void {
|
|
|
|
|
this.advent22 = advent22;
|
|
|
|
|
|
|
|
|
|
advent22
|
|
|
|
|
.api_get_blob("user/favicon")
|
2023-11-03 14:40:44 +00:00
|
|
|
|
.then((favicon_src) => {
|
|
|
|
|
const link: HTMLLinkElement =
|
|
|
|
|
document.querySelector("link[rel*='icon']") ||
|
|
|
|
|
document.createElement("link");
|
|
|
|
|
link.rel = "shortcut icon";
|
|
|
|
|
link.type = "image/x-icon";
|
|
|
|
|
link.href = favicon_src;
|
|
|
|
|
|
|
|
|
|
if (link.parentElement === null)
|
|
|
|
|
document.getElementsByTagName("head")[0].appendChild(link);
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {});
|
|
|
|
|
|
2023-11-03 17:07:25 +00:00
|
|
|
|
Promise.all([
|
2023-11-04 15:58:31 +00:00
|
|
|
|
advent22.api_get<SiteConfigModel>("user/site_config"),
|
|
|
|
|
advent22.api_get<DoorsSaved>("user/doors"),
|
|
|
|
|
advent22.api_get<number | null>("user/next_door"),
|
2023-11-03 17:07:25 +00:00
|
|
|
|
])
|
2023-11-04 04:17:36 +00:00
|
|
|
|
.then(([site_config, user_doors, next_door]) => {
|
2023-11-03 17:07:25 +00:00
|
|
|
|
document.title = site_config.title;
|
|
|
|
|
|
|
|
|
|
if (site_config.subtitle !== "")
|
|
|
|
|
document.title += " – " + site_config.subtitle;
|
|
|
|
|
|
2023-11-03 14:40:44 +00:00
|
|
|
|
this.site_config = site_config;
|
2023-11-04 04:17:36 +00:00
|
|
|
|
this.user_doors = user_doors;
|
|
|
|
|
|
2023-11-03 17:07:25 +00:00
|
|
|
|
if (next_door !== null)
|
|
|
|
|
this.next_door_target = Date.now() + next_door;
|
2023-11-03 14:40:44 +00:00
|
|
|
|
})
|
2023-11-04 15:58:31 +00:00
|
|
|
|
.catch(advent22.alert_user_error);
|
2023-11-03 14:40:44 +00:00
|
|
|
|
},
|
|
|
|
|
|
2023-11-05 21:57:50 +00:00
|
|
|
|
login(creds: Credentials): Promise<boolean> {
|
2023-11-02 00:37:00 +00:00
|
|
|
|
this.api_creds = creds;
|
|
|
|
|
|
|
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
2023-11-04 15:58:31 +00:00
|
|
|
|
this.advent22
|
|
|
|
|
.api_get<boolean>("admin/is_admin")
|
2023-11-02 00:37:00 +00:00
|
|
|
|
.then((is_admin) => {
|
|
|
|
|
this.is_admin = is_admin;
|
|
|
|
|
resolve(is_admin);
|
|
|
|
|
})
|
|
|
|
|
.catch(reject);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
logout(): Promise<boolean> {
|
2023-11-05 21:57:50 +00:00
|
|
|
|
return this.login(["", ""]);
|
2023-11-01 23:58:09 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
toggle_touch_device(): void {
|
|
|
|
|
this.is_touch_device = !this.is_touch_device;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (import.meta.webpackHot) {
|
|
|
|
|
import.meta.webpackHot.accept(
|
|
|
|
|
acceptHMRUpdate(advent22Store, import.meta.webpackHot),
|
|
|
|
|
);
|
|
|
|
|
}
|