ff-admin/src/stores/admin/navigation.ts
2024-10-27 15:51:15 +01:00

128 lines
4.5 KiB
TypeScript

import { defineStore } from "pinia";
import { useAbilityStore } from "@/stores/ability";
import router from "@/router";
export interface navigationModel {
club: navigationSplitModel;
settings: navigationSplitModel;
user: navigationSplitModel;
}
export interface navigationSplitModel {
topTitle?: string;
top?: Array<navigationLinkModel>;
mainTitle: string;
main: Array<navigationLinkModel>;
}
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<topLevelNavigationModel>,
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: "member",
} 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", "member") ? [{ key: "member", title: "Mitglieder" }] : []),
...(abilityStore.can("read", "club", "calendar") ? [{ key: "calendar", title: "Kalender" }] : []),
...(abilityStore.can("read", "club", "protocoll") ? [{ key: "protocol", title: "Protokolle" }] : []),
...(abilityStore.can("read", "club", "newsletter") ? [{ key: "newsletter", title: "Newsletter" }] : []),
],
},
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: "Kommunikationsarten" }]
: []),
...(abilityStore.can("read", "settings", "membership_status")
? [{ key: "membership_status", title: "Mitgliedsstatus" }]
: []),
...(abilityStore.can("read", "settings", "calendar_type")
? [{ key: "calendar_type", title: "Terminarten" }]
: []),
],
},
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.activeNavigationObject.main.findIndex((e) => e.key == this.activeLink) == -1 && !first)
router.push({ name: `admin-${this.activeNavigation}-default` });
},
},
});