feat: permissions store
This commit is contained in:
32
web/src/store/admin/permissions.ts
Normal file
32
web/src/store/admin/permissions.ts
Normal 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 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}));
|
Reference in New Issue
Block a user