|
|
@@ -0,0 +1,55 @@
|
|
|
+import { defineStore } from "pinia";
|
|
|
+
|
|
|
+export type Message = {
|
|
|
+ pk: number;
|
|
|
+ display: boolean;
|
|
|
+ title: string;
|
|
|
+ message: string;
|
|
|
+};
|
|
|
+
|
|
|
+const READ_ADMIN_MESSAGES_KEY = "read-admin-messages";
|
|
|
+
|
|
|
+export const useAdminMessagesStore = defineStore("adminMessages", {
|
|
|
+ state: () => {
|
|
|
+ return {
|
|
|
+ currentMessage: null as Message | null,
|
|
|
+ messages: [] as Message[],
|
|
|
+ readMessageIds: [] as number[],
|
|
|
+ ready: null as Promise<Response> | null,
|
|
|
+ };
|
|
|
+ },
|
|
|
+
|
|
|
+ actions: {
|
|
|
+ fetchData() {
|
|
|
+ this.readMessageIds = JSON.parse(
|
|
|
+ localStorage[READ_ADMIN_MESSAGES_KEY] ?? "[]",
|
|
|
+ );
|
|
|
+
|
|
|
+ this.ready = fetch(`/data/messages.json`);
|
|
|
+
|
|
|
+ this.ready
|
|
|
+ .then((response) => response.json())
|
|
|
+ .then((data: Message[]) => {
|
|
|
+ this.messages = data.filter((m) => m.display);
|
|
|
+
|
|
|
+ this.determineNextMessage();
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ console.error(error);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ markAsRead(messageId: number) {
|
|
|
+ const read_ids = this.readMessageIds;
|
|
|
+ read_ids.push(messageId);
|
|
|
+ localStorage[READ_ADMIN_MESSAGES_KEY] = JSON.stringify(read_ids);
|
|
|
+
|
|
|
+ this.determineNextMessage();
|
|
|
+ },
|
|
|
+ determineNextMessage() {
|
|
|
+ this.currentMessage =
|
|
|
+ this.messages.find((m) => {
|
|
|
+ return m.display && !this.readMessageIds.includes(m.pk);
|
|
|
+ }) ?? null;
|
|
|
+ },
|
|
|
+ },
|
|
|
+});
|