feat: permissions store

This commit is contained in:
2025-06-24 19:05:07 +02:00
parent 18664dbd8b
commit 221ef192bc

View File

@ -0,0 +1,32 @@
import { getPermissionsApi } from "@/api/admin/permissions";
import type { AppPermission } from "@/types";
import { create } from "zustand";
export interface IAdminPermissionsState {
permissions: Record<string, AppPermission[]>;
fetching: boolean;
fetch: () => Promise<void>;
}
export const usePermissions = create<IAdminPermissionsState>((set) => ({
permissions: {},
fetching: false,
fetch: async () => {
set({ fetching: true });
try {
const response = await getPermissionsApi();
set({
permissions: Object.fromEntries(
response.map(({ scope, permissions }) => [scope, permissions]),
),
});
} catch (err) {
console.log("ERR: Failed to fetch admin permissions:", err);
} finally {
set({ fetching: false });
}
},
}));