1
0
Fork 0
This commit is contained in:
Brian D 2024-12-01 23:59:26 -05:00
parent 6d2df5d44a
commit e7c7a44a9d
5 changed files with 19 additions and 16 deletions

View file

@ -47,7 +47,6 @@ const requestListener = function (req, res) {
switch (urlParts[1]) { switch (urlParts[1]) {
case "login": case "login":
res.writeHead(200); res.writeHead(200);
res.end(JSON.stringify(users[urlParts[2]]));
break; break;
case "sticker": case "sticker":
if (!(urlParts[2] in stickers)) { if (!(urlParts[2] in stickers)) {

View file

@ -5,12 +5,12 @@ export interface User {
name: string, name: string,
sticker_ids: string[], sticker_ids: string[],
username: string | null, username: string | null,
}; }
const anonymous: User = { const anonymous: User = {
name: 'Anonymous', name: 'Anonymous',
sticker_ids: [], sticker_ids: [],
username: null, username: 'anonymous',
}; };
export interface State { export interface State {
@ -21,9 +21,10 @@ export const useSessionStore = defineStore('session', {
actions: { actions: {
async login(username: string, password: string) { async login(username: string, password: string) {
const response = await fetch( const response = await fetch(
`/api/login/${username}`, // TODO: Should not be in path. `/api/login`,
{ {
method: 'POST', method: 'POST',
body: JSON.stringify({username, password}),
}, },
); );
const json = await response.json(); const json = await response.json();
@ -50,7 +51,7 @@ export const useSessionStore = defineStore('session', {
return state.user.name[0] || ''; return state.user.name[0] || '';
}, },
isAnonymous: (state: State): boolean => { isAnonymous: (state: State): boolean => {
return state.user.name === 'Anonymous'; return state.user.username === 'anonymous';
}, },
}, },
state: (): State => { state: (): State => {

View file

@ -1,6 +1,6 @@
import {acceptHMRUpdate, defineStore} from 'pinia' import {acceptHMRUpdate, defineStore} from 'pinia'
import {useUserStore} from "@/stores/userStore"; import {type User, useUserStore} from "@/stores/userStore";
export interface Sticker { export interface Sticker {
@ -9,6 +9,10 @@ export interface Sticker {
uuid: string, uuid: string,
} }
export interface State {
sticker: Sticker | null;
}
export const useStickerStore = defineStore('sticker', { export const useStickerStore = defineStore('sticker', {
actions: { actions: {
async fetch(uuid: string) { async fetch(uuid: string) {
@ -19,8 +23,7 @@ export const useStickerStore = defineStore('sticker', {
const response = await fetch( const response = await fetch(
`/api/sticker/${uuid}`, `/api/sticker/${uuid}`,
); );
const json = await response.json(); this.sticker = await response.json();
this.sticker = json;
}, },
async put(uuid: string) { async put(uuid: string) {
const response = await fetch( 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. // Disable this for now so we don't clobber the new owner.
// this.sticker = json; // this.sticker = json;
}, },
setOwner(user) { setOwner(user: User) {
if (!this.sticker) { if (!this.sticker) {
return; return;
} }
@ -48,7 +51,7 @@ export const useStickerStore = defineStore('sticker', {
userStore.addSticker(user.username, this.sticker.uuid); userStore.addSticker(user.username, this.sticker.uuid);
}, },
}, },
state: () => { state: (): State => {
return { return {
sticker: null, sticker: null,
}; };

View file

@ -23,12 +23,12 @@ export const useStickersStore = defineStore('stickers', {
}, },
getters: { getters: {
getSticker: (state: State) => { getSticker: (state: State) => {
return (uuid: string): Sticker | null => { return (uuid: string): Sticker | undefined => {
return state.stickers.find((sticker: Sticker) => sticker.uuid === uuid) return state.stickers.find((sticker: Sticker) => sticker.uuid === uuid)
}; };
}, },
}, },
state: () => { state: (): State => {
return { return {
stickers: [], stickers: [],
}; };

View file

@ -6,8 +6,8 @@ import {useStickersStore} from "@/stores/stickersStore";
export interface User { export interface User {
name: string, name: string,
sticker_ids: string[], sticker_ids: string[],
username: string | null, username: string,
}; }
export interface State { export interface State {
users: User[], users: User[],
@ -34,14 +34,14 @@ export const useUserStore = defineStore('user', {
this.users.push(json); this.users.push(json);
const stickersStore = useStickersStore(); const stickersStore = useStickersStore();
json.sticker_ids.forEach((sticker_id) => { json.sticker_ids.forEach((sticker_id: string) => {
stickersStore.fetch(sticker_id); stickersStore.fetch(sticker_id);
}) })
}, },
}, },
getters: { getters: {
getUser: (state: State) => { getUser: (state: State) => {
return (username: string): User | null => { return (username: string): User | undefined => {
return state.users.find((user: User) => user.username === username) return state.users.find((user: User) => user.username === username)
}; };
}, },