2024-12-01 19:20:21 -05:00
|
|
|
import {acceptHMRUpdate, defineStore} from 'pinia'
|
|
|
|
|
|
2024-12-04 00:20:41 -05:00
|
|
|
import type { User } from '@/stores/types'
|
|
|
|
|
import { useUserStore } from '@/stores/userStore'
|
2024-12-01 19:20:21 -05:00
|
|
|
|
|
|
|
|
|
2024-12-04 00:20:41 -05:00
|
|
|
export interface Sticker {
|
|
|
|
|
color: string,
|
2024-12-05 20:43:01 -05:00
|
|
|
layout: string,
|
|
|
|
|
message1: string | null,
|
|
|
|
|
message2: string | null,
|
|
|
|
|
message3: string | null,
|
|
|
|
|
message4: string | null,
|
2024-12-04 00:20:41 -05:00
|
|
|
owner: string | null,
|
|
|
|
|
uuid: string,
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:43:01 -05:00
|
|
|
export const activist: Sticker = {
|
|
|
|
|
color: 'FF0000',
|
|
|
|
|
layout: 'layout1',
|
|
|
|
|
message1: 'I AM AN ACTIVIST',
|
|
|
|
|
message2: null,
|
|
|
|
|
message3: null,
|
|
|
|
|
message4: null,
|
|
|
|
|
owner: null,
|
|
|
|
|
uuid: '6dcd2dae-7be2-4f79-8b10-caeb379452fc',
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-01 19:20:21 -05:00
|
|
|
export interface State {
|
2024-12-05 20:43:01 -05:00
|
|
|
activeSticker: string | null; // uuid
|
2024-12-01 19:20:21 -05:00
|
|
|
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);
|
|
|
|
|
},
|
2024-12-04 00:20:41 -05:00
|
|
|
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);
|
|
|
|
|
},
|
2024-12-01 19:20:21 -05:00
|
|
|
},
|
|
|
|
|
getters: {
|
|
|
|
|
getSticker: (state: State) => {
|
2024-12-01 23:59:26 -05:00
|
|
|
return (uuid: string): Sticker | undefined => {
|
2024-12-01 19:20:21 -05:00
|
|
|
return state.stickers.find((sticker: Sticker) => sticker.uuid === uuid)
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-01 23:59:26 -05:00
|
|
|
state: (): State => {
|
2024-12-01 19:20:21 -05:00
|
|
|
return {
|
2024-12-05 20:43:01 -05:00
|
|
|
activeSticker: null,
|
2024-12-01 19:20:21 -05:00
|
|
|
stickers: [],
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (import.meta.hot) {
|
|
|
|
|
import.meta.hot.accept(acceptHMRUpdate(useStickersStore, import.meta.hot))
|
|
|
|
|
}
|