import {acceptHMRUpdate, defineStore} from 'pinia' import type { User } from '@/stores/types' import { useUserStore } from '@/stores/userStore' export interface Sticker { color: string, layout: string, orgs: string[], message1: string | null, message2: string | null, message3: string | null, message4: string | null, owner: string | null, id: string, } export const activist: Sticker = { color: '888888', layout: 'layout1', orgs: [], message1: 'I AM AN ACTIVIST', message2: null, message3: null, message4: null, owner: null, id: '6dcd2dae-7be2-4f79-8b10-caeb379452fc', }; export interface State { activeSticker: string | null; // id stickers: Sticker[]; } export const useStickersStore = defineStore('stickers', { actions: { async fetch(id: string) { if (this.getSticker(id)) { return; } // TODO: Check if we have it already and if it's stale. const response = await fetch( `/api/sticker/${id}`, ); const json = await response.json(); this.stickers.push(json); }, async put(id: string) { const sticker = this.getSticker(id) if (!sticker) { return; } const response = await fetch( `/api/sticker/${id}`, { 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.id); const userStore= useUserStore(); userStore.addSticker(user.username, sticker.id); }, }, getters: { getSticker: (state: State) => { return (id: string): Sticker | undefined => { return state.stickers.find((sticker: Sticker) => sticker.id === id) }; }, }, state: (): State => { return { activeSticker: null, stickers: [], }; }, }) if (import.meta.hot) { import.meta.hot.accept(acceptHMRUpdate(useStickersStore, import.meta.hot)) }