Move everything to StickersStore. Reason is because after setting the sticker owner, it was updating the sticker in the sticker store, but it should update the store that has all the stickers. Profile page will lookup stickers in the stickers store.
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
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) => {
|
|
return (uuid: string): Sticker | undefined => {
|
|
return state.stickers.find((sticker: Sticker) => sticker.uuid === uuid)
|
|
};
|
|
},
|
|
},
|
|
state: (): State => {
|
|
return {
|
|
stickers: [],
|
|
};
|
|
},
|
|
})
|
|
|
|
if (import.meta.hot) {
|
|
import.meta.hot.accept(acceptHMRUpdate(useStickersStore, import.meta.hot))
|
|
}
|