From e7c7a44a9df3514e80332fa24f02a90d41573fe7 Mon Sep 17 00:00:00 2001 From: Brian DeRocher Date: Sun, 1 Dec 2024 23:59:26 -0500 Subject: [PATCH] cleanups --- server.js | 1 - src/stores/sessionStore.ts | 9 +++++---- src/stores/stickerStore.ts | 13 ++++++++----- src/stores/stickersStore.ts | 4 ++-- src/stores/userStore.ts | 8 ++++---- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/server.js b/server.js index 31db149..28fd13b 100644 --- a/server.js +++ b/server.js @@ -47,7 +47,6 @@ const requestListener = function (req, res) { switch (urlParts[1]) { case "login": res.writeHead(200); - res.end(JSON.stringify(users[urlParts[2]])); break; case "sticker": if (!(urlParts[2] in stickers)) { diff --git a/src/stores/sessionStore.ts b/src/stores/sessionStore.ts index 2128df7..129bdc1 100644 --- a/src/stores/sessionStore.ts +++ b/src/stores/sessionStore.ts @@ -5,12 +5,12 @@ export interface User { name: string, sticker_ids: string[], username: string | null, -}; +} const anonymous: User = { name: 'Anonymous', sticker_ids: [], - username: null, + username: 'anonymous', }; export interface State { @@ -21,9 +21,10 @@ export const useSessionStore = defineStore('session', { actions: { async login(username: string, password: string) { const response = await fetch( - `/api/login/${username}`, // TODO: Should not be in path. + `/api/login`, { method: 'POST', + body: JSON.stringify({username, password}), }, ); const json = await response.json(); @@ -50,7 +51,7 @@ export const useSessionStore = defineStore('session', { return state.user.name[0] || ''; }, isAnonymous: (state: State): boolean => { - return state.user.name === 'Anonymous'; + return state.user.username === 'anonymous'; }, }, state: (): State => { diff --git a/src/stores/stickerStore.ts b/src/stores/stickerStore.ts index b79b04c..c4db07b 100644 --- a/src/stores/stickerStore.ts +++ b/src/stores/stickerStore.ts @@ -1,6 +1,6 @@ import {acceptHMRUpdate, defineStore} from 'pinia' -import {useUserStore} from "@/stores/userStore"; +import {type User, useUserStore} from "@/stores/userStore"; export interface Sticker { @@ -9,6 +9,10 @@ export interface Sticker { uuid: string, } +export interface State { + sticker: Sticker | null; +} + export const useStickerStore = defineStore('sticker', { actions: { async fetch(uuid: string) { @@ -19,8 +23,7 @@ export const useStickerStore = defineStore('sticker', { const response = await fetch( `/api/sticker/${uuid}`, ); - const json = await response.json(); - this.sticker = json; + this.sticker = await response.json(); }, async put(uuid: string) { const response = await fetch( @@ -34,7 +37,7 @@ export const useStickerStore = defineStore('sticker', { // Disable this for now so we don't clobber the new owner. // this.sticker = json; }, - setOwner(user) { + setOwner(user: User) { if (!this.sticker) { return; } @@ -48,7 +51,7 @@ export const useStickerStore = defineStore('sticker', { userStore.addSticker(user.username, this.sticker.uuid); }, }, - state: () => { + state: (): State => { return { sticker: null, }; diff --git a/src/stores/stickersStore.ts b/src/stores/stickersStore.ts index dcd88ad..cd50f4e 100644 --- a/src/stores/stickersStore.ts +++ b/src/stores/stickersStore.ts @@ -23,12 +23,12 @@ export const useStickersStore = defineStore('stickers', { }, getters: { getSticker: (state: State) => { - return (uuid: string): Sticker | null => { + return (uuid: string): Sticker | undefined => { return state.stickers.find((sticker: Sticker) => sticker.uuid === uuid) }; }, }, - state: () => { + state: (): State => { return { stickers: [], }; diff --git a/src/stores/userStore.ts b/src/stores/userStore.ts index e330f3c..f3fc7b4 100644 --- a/src/stores/userStore.ts +++ b/src/stores/userStore.ts @@ -6,8 +6,8 @@ import {useStickersStore} from "@/stores/stickersStore"; export interface User { name: string, sticker_ids: string[], - username: string | null, -}; + username: string, +} export interface State { users: User[], @@ -34,14 +34,14 @@ export const useUserStore = defineStore('user', { this.users.push(json); const stickersStore = useStickersStore(); - json.sticker_ids.forEach((sticker_id) => { + json.sticker_ids.forEach((sticker_id: string) => { stickersStore.fetch(sticker_id); }) }, }, getters: { getUser: (state: State) => { - return (username: string): User | null => { + return (username: string): User | undefined => { return state.users.find((user: User) => user.username === username) }; },