2024-11-25 20:06:22 -05:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
|
2024-11-26 12:36:11 -05:00
|
|
|
import { useUserStore } from "@/stores/userStore";
|
|
|
|
|
|
2024-11-25 20:06:22 -05:00
|
|
|
export const useStickerStore = defineStore('sticker', {
|
|
|
|
|
actions: {
|
|
|
|
|
async fetch(uuid: string) {
|
2024-11-26 14:35:39 -05:00
|
|
|
if (this.sticker) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-25 20:06:22 -05:00
|
|
|
// TODO: Check if we have it already and if it's stale.
|
|
|
|
|
const response = await fetch(
|
2024-11-26 11:30:20 -05:00
|
|
|
`/api/sticker/${uuid}`,
|
2024-11-25 20:06:22 -05:00
|
|
|
);
|
|
|
|
|
const json = await response.json();
|
|
|
|
|
// this.stickers.push(json);
|
|
|
|
|
this.sticker = json;
|
2024-11-26 12:36:11 -05:00
|
|
|
},
|
|
|
|
|
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);
|
2024-11-25 20:06:22 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
state: () => {
|
|
|
|
|
return {
|
|
|
|
|
// stickers: [],
|
|
|
|
|
sticker: null,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
})
|