import { defineStore } from "pinia"; import { useAbilityStore } from "@/stores/ability"; import router from "@/router"; import type { PermissionSection } from "../../types/permissionTypes"; export type navigationModel = { [key in topLevelNavigationType]: navigationSplitModel; }; export interface navigationSplitModel { topTitle?: string; top?: Array; mainTitle: string; main: Array; } export type topLevelNavigationType = PermissionSection; 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() { const abilityStore = useAbilityStore(); this.topLevel = [ ...(abilityStore.canSection("read", "club") ? [ { key: "club", title: "Verein", levelDefault: "member", } as topLevelNavigationModel, ] : []), ...(abilityStore.canSection("read", "configuration") ? [ { key: "configuration", title: "Konfiguration", levelDefault: "salutation", } as topLevelNavigationModel, ] : []), ...(abilityStore.canSection("read", "management") ? [ { key: "management", title: "Verwaltung", levelDefault: "user", } as topLevelNavigationModel, ] : []), ]; if (this.topLevel.findIndex((e) => e.key == this.activeNavigation) == -1) { this.activeNavigation = this.topLevel[0]?.key ?? "club"; router.push({ name: `admin-${this.topLevel[0]?.key ?? "club"}-default` }); } }, updateNavigation() { const abilityStore = useAbilityStore(); this.navigation = { club: { mainTitle: "Verein", main: [ ...(abilityStore.can("read", "club", "member") ? [{ key: "member", title: "Mitglieder" }] : []), ...(abilityStore.can("read", "club", "calendar") ? [{ key: "calendar", title: "Kalender" }] : []), ...(abilityStore.can("read", "club", "protocol") ? [{ key: "protocol", title: "Protokolle" }] : []), ...(abilityStore.can("read", "club", "newsletter") ? [{ key: "newsletter", title: "Newsletter" }] : []), ...(abilityStore.can("read", "club", "query") ? [{ key: "query_builder", title: "Query Builder" }] : []), ...(abilityStore.can("read", "club", "listprint") ? [{ key: "listprint", title: "Liste Drucken" }] : []), ], }, configuration: { mainTitle: "Einstellungen", main: [ { key: "divider1", title: "Mitgliederdaten" }, ...(abilityStore.can("read", "configuration", "salutation") ? [{ key: "salutation", title: "Anrede" }] : []), ...(abilityStore.can("read", "configuration", "award") ? [{ key: "award", title: "Auszeichnungen" }] : []), ...(abilityStore.can("read", "configuration", "communication_type") ? [{ key: "communication_type", title: "Kommunikationsarten" }] : []), ...(abilityStore.can("read", "configuration", "membership_status") ? [{ key: "membership_status", title: "Mitgliedsstatus" }] : []), ...(abilityStore.can("read", "configuration", "qualification") ? [{ key: "qualification", title: "Qualifikationen" }] : []), ...(abilityStore.can("read", "configuration", "executive_position") ? [{ key: "executive_position", title: "Vereinsämter" }] : []), { key: "divider2", title: "Einstellungen" }, ...(abilityStore.can("read", "configuration", "newsletter_config") ? [{ key: "newsletter_config", title: "Newsletter Konfiguration" }] : []), ...(abilityStore.can("read", "configuration", "template") ? [{ key: "template", title: "Templates" }] : []), ...(abilityStore.can("read", "configuration", "template_usage") ? [{ key: "template_usage", title: "Template-Verwendung" }] : []), ...(abilityStore.can("read", "configuration", "calendar_type") ? [{ key: "calendar_type", title: "Terminarten" }] : []), ...(abilityStore.can("read", "configuration", "query") ? [{ key: "query_store", title: "Query Store" }] : []), ], }, management: { mainTitle: "Benutzer", main: [ ...(abilityStore.can("read", "management", "user") ? [{ key: "user", title: "Benutzer" }] : []), ...(abilityStore.can("read", "management", "role") ? [{ key: "role", title: "Rollen" }] : []), ...(abilityStore.can("read", "management", "webapi") ? [{ key: "webapi", title: "Webapi-Token" }] : []), ...(abilityStore.can("read", "management", "setting") ? [{ key: "setting", title: "Einstellungen" }] : []), ...(abilityStore.can("read", "management", "backup") ? [{ key: "backup", title: "Backups" }] : []), ...(abilityStore.isAdmin() ? [{ key: "version", title: "Version" }] : []), ], }, } as navigationModel; if ( this.activeNavigationObject.main.findIndex((e) => e.key == this.activeLink) == -1 || this.activeLink == "default" ) { let link = this.activeNavigationObject.main[0].key; router.push({ name: `admin-${this.activeNavigation}-${link}` }); } }, }, });