[stores] Minor cleanup

This commit is contained in:
Ferdinand Thiessen 2021-11-26 18:10:53 +01:00
parent d516839ad4
commit 4887bc261b
3 changed files with 16 additions and 14 deletions

View File

@ -1,6 +1,6 @@
import { LocalStorage, SessionStorage } from 'quasar';
import { FG_Plugin } from '@flaschengeist/types';
import { useSessionStore, useUserStore } from '.';
import { fixSession, useSessionStore, useUserStore } from '.';
import { AxiosResponse } from 'axios';
import { api } from '../internal';
import { defineStore } from 'pinia';
@ -63,9 +63,7 @@ export const useMainStore = defineStore({
async login(userid: string, password: string) {
try {
const { data } = await api.post<FG.Session>('/auth', { userid, password });
this.session = data;
this.session.expires = new Date(this.session.expires);
LocalStorage.set('session', this.session);
this.session = fixSession(data);
return true;
} catch ({ response }) {
return (<AxiosResponse | undefined>response)?.status || false;

View File

@ -3,6 +3,10 @@ import { defineStore } from 'pinia';
import { api } from '../internal';
import { isAxiosError, useMainStore } from '.';
export function fixSession(s?: FG.Session) {
return !s ? s : Object.assign(s, { expires: new Date(s.expires) });
}
export const useSessionStore = defineStore({
id: 'sessions',
@ -15,15 +19,13 @@ export const useSessionStore = defineStore({
return await api
.get(`/auth/${token}`)
.then(({ data }: AxiosResponse<FG.Session>) => data)
.catch(() => null);
.catch(() => undefined);
},
async getSessions() {
try {
const { data } = await api.get<FG.Session[]>('/auth');
data.forEach((session) => {
session.expires = new Date(session.expires);
});
data.forEach(fixSession);
const mainStore = useMainStore();
const currentSession = data.find((session) => {
@ -55,7 +57,7 @@ export const useSessionStore = defineStore({
async updateSession(lifetime: number, token: string) {
try {
const { data } = await api.put<FG.Session>(`auth/${token}`, { value: lifetime });
data.expires = new Date(data.expires);
fixSession(data);
const mainStore = useMainStore();
if (mainStore.session?.token == data.token) mainStore.session = data;

View File

@ -2,6 +2,10 @@ 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',
@ -24,7 +28,7 @@ export const useUserStore = defineStore({
if (force || this._dirty_users || idx === -1) {
try {
const { data } = await api.get<FG.User>(`/users/${userid}`);
if (data.birthday) data.birthday = new Date(data.birthday);
fixUser(data);
if (idx === -1) this.users.push(data);
else this.users[idx] = data;
return data;
@ -40,9 +44,7 @@ export const useUserStore = defineStore({
async getUsers(force = false) {
if (force || this._dirty_users) {
const { data } = await api.get<FG.User[]>('/users');
data.forEach((user) => {
if (user.birthday) user.birthday = new Date(user.birthday);
});
data.forEach(fixUser);
this.users = data;
this._dirty_users = false;
}
@ -74,7 +76,7 @@ export const useUserStore = defineStore({
},
async deleteAvatar(user: FG.User) {
await api.delete(`/users/${user.userid}/avatar`)
await api.delete(`/users/${user.userid}/avatar`);
},
async getPermissions(force = false) {