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

60 lines
1.9 KiB
TypeScript

import { defineStore } from "pinia";
import type { UpdateUserViewModel, UserViewModel } from "@/viewmodels/admin/user/user.models";
import { http } from "@/serverCom";
import type { PermissionObject } from "@/types/permissionTypes";
import type { AxiosResponse } from "axios";
export const useUserStore = defineStore("user", {
state: () => {
return {
users: [] as Array<UserViewModel>,
loading: "loading" as "loading" | "fetched" | "failed",
};
},
actions: {
fetchUsers() {
this.loading = "loading";
http
.get("/admin/user")
.then((result) => {
this.users = result.data;
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
fetchUserById(id: string): Promise<AxiosResponse<any, any>> {
return http.get(`/admin/user/${id}`);
},
async updateActiveUser(user: UpdateUserViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/user/${user.id}`, {
username: user.username,
firstname: user.firstname,
lastname: user.lastname,
mail: user.mail,
});
this.fetchUsers();
return result;
},
async updateActiveUserPermissions(userId: string, permission: PermissionObject): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/user/${userId}/permissions`, {
permissions: permission,
});
this.fetchUsers();
return result;
},
async updateActiveUserRoles(userId: string, roles: Array<number>): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/user/${userId}/roles`, {
roleIds: roles,
});
this.fetchUsers();
return result;
},
async deleteUser(userId: string): Promise<AxiosResponse<any, any>> {
const result = await http.delete(`/admin/user/${userId}`);
this.fetchUsers();
return result;
},
},
});