106 lines
3.1 KiB
TypeScript
106 lines
3.1 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import type { RoleViewModel } from "../../viewmodels/admin/role.models";
|
|
import { http } from "../../serverCom";
|
|
import type { PermissionObject } from "../../types/permissionTypes";
|
|
|
|
export const useRoleStore = defineStore("role", {
|
|
state: () => {
|
|
return {
|
|
roles: [] as Array<RoleViewModel>,
|
|
role: null as null | RoleViewModel,
|
|
loadingAll: null as null | "loading" | "success" | "failed",
|
|
loadingSingle: null as null | "loading" | "success" | "failed",
|
|
createStatus: null as null | "loading" | { status: "success" | "failed"; reason?: string },
|
|
updateStatus: null as null | "loading" | { status: "success" | "failed"; reason?: string },
|
|
deleteStatus: null as null | "loading" | { status: "success" | "failed"; reason?: string },
|
|
};
|
|
},
|
|
actions: {
|
|
resetStatus() {
|
|
this.createStatus = null;
|
|
this.updateStatus = null;
|
|
this.deleteStatus = null;
|
|
},
|
|
fetchRoles() {
|
|
this.loadingAll = "loading";
|
|
http
|
|
.get("/admin/role")
|
|
.then((result) => {
|
|
this.roles = result.data;
|
|
this.loadingAll = "success";
|
|
})
|
|
.catch((err) => {
|
|
this.loadingAll = "failed";
|
|
});
|
|
},
|
|
fetchRoleById(id: number) {
|
|
this.role = null;
|
|
this.loadingSingle = "loading";
|
|
http
|
|
.get(`/admin/role/${id}`)
|
|
.then((result) => {
|
|
this.role = result.data;
|
|
this.loadingSingle = "success";
|
|
})
|
|
.catch((err) => {
|
|
this.loadingSingle = "failed";
|
|
});
|
|
},
|
|
createRole(role: string) {
|
|
this.createStatus = "loading";
|
|
http
|
|
.post("/admin/role", {
|
|
role: role,
|
|
})
|
|
.then((res) => {
|
|
this.createStatus = { status: "success" };
|
|
this.fetchRoles();
|
|
})
|
|
.catch((err) => {
|
|
this.createStatus = { status: "failed", reason: err.data };
|
|
});
|
|
},
|
|
updateActiveRole(role: string) {
|
|
if (this.role == null) return;
|
|
this.updateStatus = "loading";
|
|
http
|
|
.patch(`/admin/role/${this.role.id}`, {
|
|
role: role,
|
|
})
|
|
.then((result) => {
|
|
this.updateStatus = { status: "success" };
|
|
this.fetchRoles();
|
|
})
|
|
.catch((err) => {
|
|
this.updateStatus = { status: "failed" };
|
|
});
|
|
},
|
|
updateActiveRolePermissions(permission: PermissionObject) {
|
|
if (this.role == null) return;
|
|
this.updateStatus = "loading";
|
|
http
|
|
.patch(`/admin/role/${this.role.id}/permissions`, {
|
|
permissions: permission,
|
|
})
|
|
.then((result) => {
|
|
this.updateStatus = { status: "success" };
|
|
this.fetchRoles();
|
|
})
|
|
.catch((err) => {
|
|
this.updateStatus = { status: "failed" };
|
|
});
|
|
},
|
|
deleteRole(role: number) {
|
|
this.deleteStatus = "loading";
|
|
http
|
|
.delete(`/admin/role/${role}`)
|
|
.then((res) => {
|
|
this.deleteStatus = { status: "success" };
|
|
this.fetchRoles();
|
|
})
|
|
.catch((err) => {
|
|
this.deleteStatus = { status: "failed", reason: err.data };
|
|
});
|
|
},
|
|
},
|
|
});
|