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

70 lines
1.7 KiB
TypeScript
Raw Normal View History

import {acceptHMRUpdate, defineStore} from 'pinia'
import type { User } from '@/stores/types'
import { useUserStore } from '@/stores/userStore'
export interface Sticker {
color: string,
owner: string | null,
uuid: string,
}
export interface State {
stickers: Sticker[];
}
export const useStickersStore = defineStore('stickers', {
actions: {
async fetch(uuid: string) {
if (this.getSticker(uuid)) {
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.stickers.push(json);
},
async put(uuid: string) {
const sticker = this.getSticker(uuid)
if (!sticker) {
return;
}
const response = await fetch(
`/api/sticker/${uuid}`,
{
body: JSON.stringify(sticker),
method: 'PUT',
},
);
const json = await response.json();
// Disable this for now so we don't clobber the new owner.
// this.sticker = json;
},
setOwner(sticker: Sticker, user: User) {
sticker.owner = user.username;
this.put(sticker.uuid);
const userStore= useUserStore();
userStore.addSticker(user.username, sticker.uuid);
},
},
getters: {
getSticker: (state: State) => {
2024-12-01 23:59:26 -05:00
return (uuid: string): Sticker | undefined => {
return state.stickers.find((sticker: Sticker) => sticker.uuid === uuid)
};
},
},
2024-12-01 23:59:26 -05:00
state: (): State => {
return {
stickers: [],
};
},
})
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useStickersStore, import.meta.hot))
}