import { defineStore } from "pinia"; import { useAbilityStore } from "../ability"; import router from "../../router"; export interface navigationModel { club: navigationSplitModel; settings: navigationSplitModel; user: navigationSplitModel; } export interface navigationSplitModel { topTitle?: string; top?: Array; mainTitle: string; main: Array; } export type topLevelNavigationType = "club" | "settings" | "user"; export interface topLevelNavigationModel { key: topLevelNavigationType; title: string; levelDefault: string; } export interface navigationLinkModel { key: string; title: string; } export const useNavigationStore = defineStore("navigation", { state: () => { return { activeNavigation: "club" as topLevelNavigationType, activeLink: null as null | string, topLevel: [] as Array, navigation: {} as navigationModel, }; }, getters: { activeNavigationObject: (state) => (state.navigation[state.activeNavigation] ?? {}) as navigationSplitModel, activeTopLevelObject: (state) => (state.topLevel.find((elem) => elem.key == state.activeNavigation) ?? {}) as topLevelNavigationModel, }, actions: { resetNavigation() { this.$reset(); }, updateTopLevel(first: boolean = false) { const abilityStore = useAbilityStore(); this.topLevel = [ ...(abilityStore.canSection("read", "club") ? [ { key: "club", title: "Verein", levelDefault: "members", } as topLevelNavigationModel, ] : []), ...(abilityStore.canSection("read", "settings") ? [ { key: "settings", title: "Einstellungen", levelDefault: "qualification", } as topLevelNavigationModel, ] : []), ...(abilityStore.canSection("read", "user") ? [ { key: "user", title: "Benutzer", levelDefault: "user", } as topLevelNavigationModel, ] : []), ]; if (this.topLevel.findIndex((e) => e.key == this.activeNavigation) == -1 && !first) router.push({ name: `admin-${this.topLevel[0]?.key ?? "club"}-default` }); }, updateNavigation(first: boolean = false) { const abilityStore = useAbilityStore(); this.navigation = { club: { mainTitle: "Verein", main: [ ...(abilityStore.can("read", "club", "members") ? [{ key: "members", title: "Mitglieder" }] : []), ...(abilityStore.can("read", "club", "calendar") ? [{ key: "calendar", title: "Termine" }] : []), ...(abilityStore.can("read", "club", "newsletter") ? [{ key: "newsletter", title: "Newsletter" }] : []), ...(abilityStore.can("read", "club", "protocoll") ? [{ key: "protocol", title: "Protokolle" }] : []), ], }, settings: { mainTitle: "Einstellungen", main: [ ...(abilityStore.can("read", "settings", "qualification") ? [{ key: "qualification", title: "Qualifikationen" }] : []), ...(abilityStore.can("read", "settings", "award") ? [{ key: "award", title: "Auszeichnungen" }] : []), ...(abilityStore.can("read", "settings", "executive_position") ? [{ key: "executive_position", title: "Vereinsämter" }] : []), ...(abilityStore.can("read", "settings", "communication") ? [{ key: "communication", title: "Mitgliederdaten" }] : []), ], }, user: { mainTitle: "Benutzer", main: [ ...(abilityStore.can("read", "user", "user") ? [{ key: "user", title: "Benutzer" }] : []), ...(abilityStore.can("read", "user", "role") ? [{ key: "role", title: "Rollen" }] : []), ], }, } as navigationModel; if (this.topLevel.findIndex((e) => e.key == this.activeLink) == -1 && !first) router.push({ name: `admin-${this.activeNavigation}-default` }); }, }, });