ff-admin/src/stores/admin/navigation.ts

123 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-08-23 14:42:32 +02:00
import { defineStore } from "pinia";
import { useAbilityStore } from "../ability";
2024-09-01 19:19:48 +02:00
import router from "../../router";
2024-08-23 14:42:32 +02:00
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,
2024-09-01 19:19:48 +02:00
activeLink: null as null | string,
topLevel: [] as Array<topLevelNavigationModel>,
navigation: {} as navigationModel,
2024-08-23 14:42:32 +02:00
};
},
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();
},
2024-09-01 19:19:48 +02:00
updateTopLevel(first: boolean = false) {
const abilityStore = useAbilityStore();
this.topLevel = [
2024-08-27 17:55:10 +02:00
...(abilityStore.canSection("read", "club")
? [
{
key: "club",
title: "Verein",
2024-09-01 19:19:48 +02:00
levelDefault: "members",
} as topLevelNavigationModel,
]
: []),
2024-08-27 17:55:10 +02:00
...(abilityStore.canSection("read", "settings")
? [
{
key: "settings",
title: "Einstellungen",
2024-09-01 19:19:48 +02:00
levelDefault: "qualification",
} as topLevelNavigationModel,
]
: []),
2024-08-27 17:55:10 +02:00
...(abilityStore.canSection("read", "user")
? [
{
key: "user",
title: "Benutzer",
2024-09-01 19:19:48 +02:00
levelDefault: "user",
} as topLevelNavigationModel,
]
: []),
];
2024-09-01 19:19:48 +02:00
if (this.topLevel.findIndex((e) => e.key == this.activeNavigation) == -1 && !first)
router.push({ name: `admin-${this.topLevel[0]?.key ?? "club"}-default` });
},
2024-09-01 19:19:48 +02:00
updateNavigation(first: boolean = false) {
const abilityStore = useAbilityStore();
this.navigation = {
club: {
mainTitle: "Verein",
main: [
2024-09-01 19:19:48 +02:00
...(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")
2024-09-01 19:19:48 +02:00
? [{ key: "qualification", title: "Qualifikationen" }]
: []),
2024-09-01 19:19:48 +02:00
...(abilityStore.can("read", "settings", "award") ? [{ key: "award", title: "Auszeichnungen" }] : []),
...(abilityStore.can("read", "settings", "executive_position")
2024-09-01 19:19:48 +02:00
? [{ key: "executive_position", title: "Vereinsämter" }]
: []),
...(abilityStore.can("read", "settings", "communication")
2024-09-01 19:19:48 +02:00
? [{ key: "communication", title: "Mitgliederdaten" }]
: []),
],
},
user: {
mainTitle: "Benutzer",
main: [
2024-09-01 19:19:48 +02:00
...(abilityStore.can("read", "user", "user") ? [{ key: "user", title: "Benutzer" }] : []),
...(abilityStore.can("read", "user", "role") ? [{ key: "role", title: "Rollen" }] : []),
],
},
} as navigationModel;
2024-09-01 19:19:48 +02:00
if (this.topLevel.findIndex((e) => e.key == this.activeLink) == -1 && !first)
router.push({ name: `admin-${this.activeNavigation}-default` });
},
2024-08-23 14:42:32 +02:00
},
});