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.canAccessSection("club") ? [ { key: "club", title: "Verein", levelDefault: "member", } as topLevelNavigationModel, ] : []), ...(abilityStore.canAccessSection("unit") ? [ { key: "unit", title: "Wehr", levelDefault: "equipment", } as topLevelNavigationModel, ] : []), ...(abilityStore.canAccessSection("configuration") ? [ { key: "configuration", title: "Konfiguration", levelDefault: "salutation", } as topLevelNavigationModel, ] : []), ...(abilityStore.canAccessSection("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" }] : []), ], }, unit: { mainTitle: "Wehr", main: [ ...(abilityStore.can("read", "unit", "equipment") ? [{ key: "equipment", title: "Gerätschaften" }] : []), ...(abilityStore.can("read", "unit", "vehicle") ? [{ key: "vehicle", title: "Fahrzeuge" }] : []), ...(abilityStore.can("read", "unit", "wearable") ? [{ key: "wearable", title: "Kleidung" }] : []), ...(abilityStore.can("read", "unit", "respiratory_gear") ? [{ key: "respiratory_gear", title: "Atemschutz-Geräte" }] : []), ...(abilityStore.can("read", "unit", "respiratory_wearer") ? [{ key: "respiratory_wearer", title: "Atemschutz-Träger" }] : []), ...(abilityStore.can("read", "unit", "respiratory_mission") ? [{ key: "respiratory_mission", title: "Atemschutz-Einsätze" }] : []), ...(abilityStore.can("create", "unit", "inspection") ? [{ key: "inspection", title: "Prüfungen" }] : []), ...(abilityStore.can("read", "unit", "damage_report") ? [{ key: "damage_report", title: "Schadensmeldungen" }] : []), { key: "divider1", title: "Basisdaten" }, ...(abilityStore.can("read", "unit", "equipment_type") ? [{ key: "equipment_type", title: "Geräte-Typen" }] : []), ...(abilityStore.can("read", "unit", "vehicle_type") ? [{ key: "vehicle_type", title: "Fahrzeug-Arten" }] : []), ...(abilityStore.can("read", "unit", "wearable_type") ? [{ key: "wearable_type", title: "Kleidungs-Arten" }] : []), ...(abilityStore.can("read", "unit", "inspection_plan") ? [{ key: "inspection_plan", title: "Prüfpläne" }] : []), ], }, configuration: { mainTitle: "Konfiguration", 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", "education") ? [{ key: "education", title: "Aus-/Fortbildungen" }] : []), ...(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.filter((m) => !m.key.startsWith("divider"))[0].key; this.activeLink = link; router.push({ name: `admin-${this.activeNavigation}-${link}` }); } }, }, });