role and user management

This commit is contained in:
Julian Krauser 2024-09-02 15:57:03 +02:00
parent 6247c385c3
commit 5ffdfcd6f2
22 changed files with 798 additions and 92 deletions

View file

@ -1,6 +1,7 @@
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: () => {
@ -10,9 +11,16 @@ export const useRoleStore = defineStore("role", {
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
@ -38,9 +46,6 @@ export const useRoleStore = defineStore("role", {
this.loadingSingle = "failed";
});
},
resetCreateStatus() {
this.createStatus = null;
},
createRole(role: string) {
this.createStatus = "loading";
http
@ -55,5 +60,47 @@ export const useRoleStore = defineStore("role", {
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 };
});
},
},
});

View file

@ -1,6 +1,7 @@
import { defineStore } from "pinia";
import type { UserViewModel } from "../../viewmodels/admin/user.models";
import type { CreateOrUpdateUserViewModel, UserViewModel } from "../../viewmodels/admin/user.models";
import { http } from "../../serverCom";
import type { PermissionObject } from "../../types/permissionTypes";
export const useUserStore = defineStore("user", {
state: () => {
@ -9,9 +10,17 @@ export const useUserStore = defineStore("user", {
user: null as null | UserViewModel,
loadingAll: "loading" as "loading" | "fetched" | "failed",
loadingSingle: "loading" as "loading" | "fetched" | "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;
},
fetchUsers() {
this.loadingAll = "loading";
http
@ -37,5 +46,65 @@ export const useUserStore = defineStore("user", {
this.loadingSingle = "failed";
});
},
updateActiveUser(user: CreateOrUpdateUserViewModel) {
if (this.user == null) return;
this.updateStatus = "loading";
http
.patch(`/admin/user/${this.user.id}`, {
username: user.username,
firstname: user.firstname,
lastname: user.lastname,
mail: user.mail,
})
.then((result) => {
this.updateStatus = { status: "success" };
this.fetchUsers();
})
.catch((err) => {
this.updateStatus = { status: "failed" };
});
},
updateActiveUserPermissions(permission: PermissionObject) {
if (this.user == null) return;
this.updateStatus = "loading";
http
.patch(`/admin/user/${this.user.id}/permissions`, {
permissions: permission,
})
.then((result) => {
this.updateStatus = { status: "success" };
this.fetchUsers();
})
.catch((err) => {
this.updateStatus = { status: "failed" };
});
},
updateActiveUserRoles(roles: Array<number>) {
if (this.user == null) return;
this.updateStatus = "loading";
http
.patch(`/admin/user/${this.user.id}/roles`, {
roleIds: roles,
})
.then((result) => {
this.updateStatus = { status: "success" };
this.fetchUsers();
})
.catch((err) => {
this.updateStatus = { status: "failed" };
});
},
deleteUser(user: number) {
this.deleteStatus = "loading";
http
.delete(`/admin/user/${user}`)
.then((res) => {
this.deleteStatus = { status: "success" };
this.fetchUsers();
})
.catch((err) => {
this.deleteStatus = { status: "failed", reason: err.data };
});
},
},
});