fix ts
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { api } from 'boot/axios';
|
||||
import { IRoles, IRolesDisplay, IUser, IUserDisplay, IUserRolesDisplay } from 'src/types/UserTypes';
|
||||
import { IRoles, IRolesDisplay, IUser, IUserDisplay, IUserRolesDisplay, IUserSubmit } from 'src/types/UserTypes';
|
||||
import { Notify } from 'quasar'
|
||||
import { IResponse } from 'src/types/BaseTypes';
|
||||
|
||||
@@ -12,6 +12,7 @@ export const useUserStore = defineStore('user', {
|
||||
roles: [] as IRolesDisplay[],
|
||||
}),
|
||||
actions: {
|
||||
// -------------------------- users --------------------------
|
||||
async loadUsers() {
|
||||
this.reset('users');
|
||||
try {
|
||||
@@ -29,6 +30,39 @@ export const useUserStore = defineStore('user', {
|
||||
}
|
||||
},
|
||||
|
||||
getUserById(id: number) {
|
||||
if (!this.userIds.has(id)) {
|
||||
return null;
|
||||
}
|
||||
return this.users.find((item: IUserDisplay) => item.id === id);
|
||||
},
|
||||
|
||||
async addUser(user: IUserSubmit) {
|
||||
return await api.post('api/v1/users', user);
|
||||
},
|
||||
|
||||
async editUser(user: IUserSubmit) {
|
||||
const id = user.id;
|
||||
delete user['id']
|
||||
return await api.put(`api/v1/users/${id}`, user);
|
||||
},
|
||||
|
||||
async deleteUser(userId: number) {
|
||||
try {
|
||||
await api.delete(`api/v1/users/${userId}`)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
Notify.create({
|
||||
icon: 'error',
|
||||
color: 'negative',
|
||||
message: 'ユーザーの削除に失敗しました'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
// -------------------------- roles --------------------------
|
||||
async loadRoles() {
|
||||
this.reset('roles');
|
||||
try {
|
||||
@@ -45,26 +79,19 @@ export const useUserStore = defineStore('user', {
|
||||
}
|
||||
},
|
||||
|
||||
getUserById(id: number) {
|
||||
if (!this.userIds.has(id)) {
|
||||
return null;
|
||||
}
|
||||
return this.users.find((item: IUserDisplay) => item.id === id);
|
||||
async addRole(user: IUserRolesDisplay, role: IRolesDisplay) {
|
||||
return await this.updateUserRole(user, user.roleIds.concat(role.id));
|
||||
},
|
||||
|
||||
async deleteUser(userId: number) {
|
||||
try {
|
||||
await api.delete(`api/v1/users/${userId}`)
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
Notify.create({
|
||||
icon: 'error',
|
||||
color: 'negative',
|
||||
message: 'ユーザーの削除に失敗しました'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
async removeRole(user: IUserRolesDisplay, role: IRolesDisplay) {
|
||||
return await this.updateUserRole(user, user.roleIds.filter(e => e !== role.id));
|
||||
},
|
||||
|
||||
async updateUserRole(user: IUserRolesDisplay, roleIds: number[]) {
|
||||
return await api.post('api/v1/userrole', {
|
||||
userid: user.id,
|
||||
roleIds
|
||||
});
|
||||
},
|
||||
|
||||
reset(target?: 'users'|'roles') {
|
||||
@@ -98,7 +125,14 @@ export function userToUserDisplay(user: IUser): IUserDisplay {
|
||||
|
||||
export function userToUserRolesDisplay(user: IUser): IUserRolesDisplay {
|
||||
const userRolesDisplay = userToUserDisplay(user) as IUserRolesDisplay;
|
||||
userRolesDisplay.roles = user.roles.map((role) => roleToRoleDisplay(role)).sort((a, b) => a.level - b.level);
|
||||
const roles: IRolesDisplay[] = [];
|
||||
const roleIds: number[] = [];
|
||||
user.roles.sort((a, b) => a.level - b.level).forEach((role) => {
|
||||
roles.push(roleToRoleDisplay(role));
|
||||
roleIds.push(role.id);
|
||||
});
|
||||
userRolesDisplay.roles = roles;
|
||||
userRolesDisplay.roleIds = roleIds;
|
||||
return userRolesDisplay;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user