1
0
Fork 0
activist/src/stores/stickerStore.ts

65 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-12-02 19:35:53 -05:00
import { acceptHMRUpdate, defineStore } from 'pinia'
2024-11-25 20:06:22 -05:00
2024-12-02 19:35:53 -05:00
import { useUserStore } from "@/stores/userStore";
import { type User } from '@/stores/types';
export interface Sticker {
color: string,
owner: string | null,
uuid: string,
}
2024-12-01 23:59:26 -05:00
export interface State {
sticker: Sticker | null;
}
2024-11-25 20:06:22 -05:00
export const useStickerStore = defineStore('sticker', {
actions: {
async fetch(uuid: string) {
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
);
2024-12-01 23:59:26 -05:00
this.sticker = await response.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;
},
2024-12-01 23:59:26 -05:00
setOwner(user: 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);
2024-11-26 22:58:23 -05:00
const userStore= useUserStore();
userStore.addSticker(user.username, this.sticker.uuid);
2024-11-25 20:06:22 -05:00
},
},
2024-12-01 23:59:26 -05:00
state: (): State => {
2024-11-25 20:06:22 -05:00
return {
sticker: null,
};
},
})
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useStickerStore, import.meta.hot))
}