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

71 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-09-01 14:54:49 +02:00
import { defineStore } from "pinia";
2025-01-02 18:28:13 +01:00
import type { UpdateUserViewModel, UserViewModel } from "@/viewmodels/admin/user/user.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 useUserStore = defineStore("user", {
state: () => {
return {
users: [] as Array<UserViewModel>,
2024-09-15 13:52:54 +02:00
loading: "loading" as "loading" | "fetched" | "failed",
2024-09-01 14:54:49 +02:00
};
},
actions: {
fetchUsers() {
2024-09-15 13:52:54 +02:00
this.loading = "loading";
2024-09-01 14:54:49 +02:00
http
.get("/admin/user")
.then((result) => {
this.users = result.data;
2024-09-15 13:52:54 +02:00
this.loading = "fetched";
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
fetchUserById(id: number): Promise<AxiosResponse<any, any>> {
return http.get(`/admin/user/${id}`);
2024-09-01 14:54:49 +02:00
},
2024-09-15 13:52:54 +02:00
updateActiveUser(user: UpdateUserViewModel): Promise<AxiosResponse<any, any>> {
return http
.patch(`/admin/user/${user.id}`, {
2024-09-02 15:57:03 +02:00
username: user.username,
firstname: user.firstname,
lastname: user.lastname,
mail: user.mail,
})
.then((result) => {
this.fetchUsers();
2024-09-15 13:52:54 +02:00
return result;
2024-09-02 15:57:03 +02:00
});
},
2024-09-15 13:52:54 +02:00
updateActiveUserPermissions(user: number, permission: PermissionObject): Promise<AxiosResponse<any, any>> {
return http
.patch(`/admin/user/${user}/permissions`, {
2024-09-02 15:57:03 +02:00
permissions: permission,
})
.then((result) => {
this.fetchUsers();
2024-09-15 13:52:54 +02:00
return result;
2024-09-02 15:57:03 +02:00
});
},
2024-09-15 13:52:54 +02:00
updateActiveUserRoles(user: number, roles: Array<number>): Promise<AxiosResponse<any, any>> {
return http
.patch(`/admin/user/${user}/roles`, {
2024-09-02 15:57:03 +02:00
roleIds: roles,
})
.then((result) => {
this.fetchUsers();
2024-09-15 13:52:54 +02:00
return result;
2024-09-02 15:57:03 +02:00
});
},
2024-09-15 13:52:54 +02:00
deleteUser(user: number): Promise<AxiosResponse<any, any>> {
return http.delete(`/admin/user/${user}`).then((result) => {
this.fetchUsers();
return result;
});
2024-09-02 15:57:03 +02:00
},
2024-09-01 14:54:49 +02:00
},
});