1
0
Fork 0
activist/src/stores/stickerStore.ts
Brian DeRocher 582010fb7b Fetch the user and their stickers.
Add useStickersStore.

Rename stickers to sticker_ids.
2024-12-01 19:20:21 -05:00

60 lines
1.4 KiB
TypeScript

import {acceptHMRUpdate, defineStore} from 'pinia'
import {useUserStore} from "@/stores/userStore";
export interface Sticker {
color: string,
owner: string | null,
uuid: string,
}
export const useStickerStore = defineStore('sticker', {
actions: {
async fetch(uuid: string) {
if (this.sticker) {
return;
}
// TODO: Check if we have it already and if it's stale.
const response = await fetch(
`/api/sticker/${uuid}`,
);
const json = await response.json();
this.sticker = json;
},
async put(uuid: string) {
const response = await fetch(
`/api/sticker/${uuid}`,
{
body: JSON.stringify(this.sticker),
method: 'PUT',
},
);
const json = await response.json();
// Disable this for now so we don't clobber the new owner.
// this.sticker = json;
},
setOwner(user) {
if (!this.sticker) {
return;
}
// this.sticker.owner = user.username; does not work
this.sticker = {
...this.sticker,
owner: user.username,
}
this.put(this.sticker.uuid);
const userStore= useUserStore();
userStore.addSticker(this.sticker.uuid);
},
},
state: () => {
return {
sticker: null,
};
},
})
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useStickerStore, import.meta.hot))
}