ff-admin/src/stores/admin/user.ts

111 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-09-01 14:54:49 +02:00
import { defineStore } from "pinia";
2024-09-02 15:57:03 +02:00
import type { CreateOrUpdateUserViewModel, UserViewModel } from "../../viewmodels/admin/user.models";
2024-09-01 14:54:49 +02:00
import { http } from "../../serverCom";
2024-09-02 15:57:03 +02:00
import type { PermissionObject } from "../../types/permissionTypes";
2024-09-01 14:54:49 +02:00
export const useUserStore = defineStore("user", {
state: () => {
return {
users: [] as Array<UserViewModel>,
user: null as null | UserViewModel,
loadingAll: "loading" as "loading" | "fetched" | "failed",
loadingSingle: "loading" as "loading" | "fetched" | "failed",
2024-09-02 15:57:03 +02:00
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 },
2024-09-01 14:54:49 +02:00
};
},
actions: {
2024-09-02 15:57:03 +02:00
resetStatus() {
this.createStatus = null;
this.updateStatus = null;
this.deleteStatus = null;
},
2024-09-01 14:54:49 +02:00
fetchUsers() {
this.loadingAll = "loading";
http
.get("/admin/user")
.then((result) => {
this.users = result.data;
this.loadingAll = "fetched";
})
.catch((err) => {
this.loadingAll = "failed";
});
},
2024-09-01 19:19:48 +02:00
fetchUserById(id: number) {
2024-09-01 14:54:49 +02:00
this.user = null;
this.loadingSingle = "loading";
http
.get(`/admin/user/${id}`)
.then((result) => {
this.user = result.data;
this.loadingSingle = "fetched";
})
.catch((err) => {
this.loadingSingle = "failed";
});
},
2024-09-02 15:57:03 +02:00
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 };
});
},
2024-09-01 14:54:49 +02:00
},
});