ff-admin/src/stores/admin/management/role.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-09-01 14:54:49 +02:00
import { defineStore } from "pinia";
2025-02-15 11:08:09 +01:00
import type { RoleViewModel } from "@/viewmodels/admin/management/role.models";
2024-09-17 16:44:02 +02:00
import { http } from "@/serverCom";
import type { PermissionObject } from "@/types/permissionTypes";
2024-09-15 13:52:54 +02:00
import type { AxiosResponse } from "axios";
2024-09-01 14:54:49 +02:00
export const useRoleStore = defineStore("role", {
state: () => {
return {
roles: [] as Array<RoleViewModel>,
2024-09-15 13:52:54 +02:00
loading: null as null | "loading" | "success" | "failed",
2024-09-01 14:54:49 +02:00
};
},
actions: {
fetchRoles() {
2024-09-15 13:52:54 +02:00
this.loading = "loading";
2024-09-01 14:54:49 +02:00
http
.get("/admin/role")
.then((result) => {
this.roles = result.data;
2024-09-15 13:52:54 +02:00
this.loading = "success";
2024-09-01 14:54:49 +02:00
})
.catch((err) => {
2024-09-15 13:52:54 +02:00
this.loading = "failed";
2024-09-01 14:54:49 +02:00
});
},
2024-09-15 13:52:54 +02:00
fetchRoleById(id: number): Promise<AxiosResponse<any, any>> {
return http.get(`/admin/role/${id}`);
2024-09-01 14:54:49 +02:00
},
2024-09-15 13:52:54 +02:00
async createRole(role: string): Promise<AxiosResponse<any, any>> {
const result = await http.post("/admin/role", {
role: role,
});
this.fetchRoles();
return result;
2024-09-01 14:54:49 +02:00
},
2024-09-15 13:52:54 +02:00
async updateActiveRole(id: number, role: string): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/role/${id}`, {
role: role,
});
this.fetchRoles();
return result;
2024-09-02 15:57:03 +02:00
},
2024-09-15 13:52:54 +02:00
async updateActiveRolePermissions(role: number, permission: PermissionObject): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/role/${role}/permissions`, {
permissions: permission,
});
this.fetchRoles();
return result;
2024-09-02 15:57:03 +02:00
},
2024-09-15 13:52:54 +02:00
async deleteRole(role: number): Promise<AxiosResponse<any, any>> {
const result = await http.delete(`/admin/role/${role}`);
this.fetchRoles();
return result;
2024-09-02 15:57:03 +02:00
},
2024-09-01 14:54:49 +02:00
},
});