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]) {
case "login":
res.writeHead(200);
res.end(JSON.stringify(users[urlParts[2]]));
break;
case "sticker":
if (!(urlParts[2] in stickers)) {

View file

@ -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 => {

View file

@ -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,
};

View file

@ -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: [],
};

View file

@ -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)
};
},