flaschengeist-frontend/api/src/stores/user.ts

133 lines
3.7 KiB
TypeScript

import { defineStore } from 'pinia';
import { api } from '../internal';
import { isAxiosError, useMainStore } from '.';
export function fixUser(u?: FG.User) {
return !u ? u : Object.assign(u, { birthday: u.birthday ? new Date(u.birthday) : undefined });
}
export const useUserStore = defineStore({
id: 'users',
state: () => ({
roles: [] as FG.Role[],
users: [] as FG.User[],
permissions: [] as FG.Permission[],
_dirty_users: true,
_dirty_roles: true,
}),
getters: {},
actions: {
findUser(userid: string) {
return this.users.find((user) => user.userid === userid);
},
async getUser(userid: string, force = false) {
const idx = this.users.findIndex((user) => user.userid === userid);
if (force || this._dirty_users || idx === -1) {
try {
const { data } = await api.get<FG.User>(`/users/${userid}`);
fixUser(data);
if (idx === -1) this.users.push(data);
else this.users[idx] = data;
return data;
} catch (error) {
// Ignore 404, throw all other
if (!isAxiosError(error, 404)) throw error;
}
} else {
return this.users[idx];
}
},
async getUsers(force = false) {
if (force || this._dirty_users) {
const { data } = await api.get<FG.User[]>('/users');
data.forEach(fixUser);
this.users = data;
this._dirty_users = false;
}
return this.users;
},
async updateUser(user: FG.User) {
await api.put(`/users/${user.userid}`, user);
const mainStore = useMainStore();
if (user.userid === mainStore.user?.userid) mainStore.user = user;
this._dirty_users = true;
},
async createUser(user: FG.User) {
const { data } = await api.post<FG.User>('/users', user);
this.users.push(data);
return data;
},
async deleteUser(user: FG.User | string) {
if (typeof user === 'object') user = user.userid;
await api.delete(`/users/${user}`);
this.users = this.users.filter(u => u.userid != user);
},
async uploadAvatar(user: FG.User, file: string | File) {
const formData = new FormData();
formData.append('file', file);
await api.post(`/users/${user.userid}/avatar`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
},
async deleteAvatar(user: FG.User) {
await api.delete(`/users/${user.userid}/avatar`);
},
async getPermissions(force = false) {
if (force || this.permissions.length === 0) {
const { data } = await api.get<FG.Permission[]>('/roles/permissions');
this.permissions = data;
}
return this.permissions;
},
async getRoles(force = false) {
if (force || this._dirty_roles) {
const { data } = await api.get<FG.Role[]>('/roles');
this.roles = data;
this._dirty_roles = false;
}
return this.roles;
},
async updateRole(role: FG.Role) {
try {
await api.put(`/roles/${role.id}`, role);
} catch (error) {
console.warn(error);
}
this._updatePermission(role);
},
_updatePermission(role: FG.Role) {
const idx = this.roles.findIndex((r) => r.id === role.id);
if (idx != -1) this.roles[idx] = role;
this._dirty_roles = true;
},
async newRole(role: FG.Role) {
const { data } = await api.post<FG.Role>('/roles', role);
this.roles.push(data);
return data;
},
async deleteRole(role: FG.Role | number) {
await api.delete(`/roles/${typeof role === 'number' ? role : role.id}`);
this.roles = this.roles.filter((r) => r.id !== (typeof role == 'number' ? role : role.id));
},
},
});