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, 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> { return http.get(`/admin/user/${id}`); }, async updateActiveUser(user: UpdateUserViewModel): Promise> { 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> { const result = await http.patch(`/admin/user/${userId}/permissions`, { permissions: permission, }); this.fetchUsers(); return result; }, async updateActiveUserRoles(userId: string, roles: Array): Promise> { const result = await http.patch(`/admin/user/${userId}/roles`, { roleIds: roles, }); this.fetchUsers(); return result; }, async deleteUser(userId: string): Promise> { const result = await http.delete(`/admin/user/${userId}`); this.fetchUsers(); return result; }, }, });