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

View File

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

View File

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