import type { Account } from "~/shared/types/account"; export const useAccountStore = defineStore("account", () => { const runtimeConfig = useRuntimeConfig(); const sessionStore = useSessionStore(); const state = { valid: ref(false), id: ref(), name: ref(), timezone: ref(), type: ref(), interestedIds: ref>(), }; 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.type.value = account?.type; state.interestedIds.value = account?.interestedIds ? new Set(account.interestedIds) : undefined; }); const getters = { 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), }; const actions = { async toggleInterestedId(id: string, slotIds?: string[]) { if (!state.interestedIds.value) { throw Error("accountStore.toggleInterestedId: Invalid state") } let newIds = [...state.interestedIds.value ?? []]; if (state.interestedIds.value.has(id)) { newIds = newIds.filter(newId => newId !== id); } else { newIds.push(id); if (slotIds) { const filterIds = new Set(slotIds); newIds = newIds.filter(newId => !filterIds.has(newId)); } } await $fetch("/api/auth/account", { method: "PATCH", body: { interestedIds: newIds }, }) await sessionStore.fetch(); }, }; return { ...state, ...getters, ...actions, }; });