/* SPDX-FileCopyrightText: © 2025 Hornwitser SPDX-License-Identifier: AGPL-3.0-or-later */ import type { ApiAccount, ApiAccountPatch } from "~/shared/types/api"; export const useAccountStore = defineStore("account", () => { const runtimeConfig = useRuntimeConfig(); const sessionStore = useSessionStore(); const state = { valid: ref(false), id: ref(), name: ref(), timezone: ref(), locale: ref(), type: ref(), interestedEventIds: ref>(new Set()), interestedEventSlotIds: ref>(new Set()), }; watchEffect(() => { const account = sessionStore.account; state.valid.value = Boolean(account); state.id.value = account?.id; state.name.value = account?.name; state.timezone.value = account?.timezone; state.locale.value = account?.locale; state.type.value = account?.type; state.interestedEventIds.value = new Set(account?.interestedEventIds ?? []); state.interestedEventSlotIds.value = new Set(account?.interestedEventSlotIds ?? []); }); const getters = { isAdmin: computed(() => state.type.value === "admin"), isCrew: computed(() => state.type.value === "crew" || state.type.value === "admin"), canEdit: computed(() => state.type.value === "admin" || state.type.value === "crew" ), canEditPublic: computed(() => state.type.value === "admin"), activeTimezone: computed(() => state.timezone.value || runtimeConfig.public.defaultTimezone), defaultTimezone: computed(() => runtimeConfig.public.defaultTimezone), activeLocale: computed(() => state.locale.value || runtimeConfig.public.defaultLocale), defaultLocale: computed(() => runtimeConfig.public.defaultLocale), }; const actions = { async toggleInterestedId(type: "event" | "slot", id: number, slotIds?: number[]) { let newEventIds = new Set(state.interestedEventIds.value); let newSlotIds = new Set(state.interestedEventSlotIds.value); if (type === "event") { if (newEventIds.has(id)) { newEventIds.delete(id) } else { newEventIds.add(id); if (slotIds) { for (const slotId of slotIds) { newSlotIds.delete(slotId) } } } } else if (type === "slot") { if (newSlotIds.has(id)) { newSlotIds.delete(id); } else { newSlotIds.add(id); } } const patch: ApiAccountPatch = { interestedEventIds: [...newEventIds], interestedEventSlotIds: [...newSlotIds], } await $fetch("/api/auth/account", { method: "PATCH", body: patch, }) await sessionStore.fetch(); }, }; return { ...state, ...getters, ...actions, }; });