2024-12-22 13:40:25 -05:00
|
|
|
import { acceptHMRUpdate, defineStore } from 'pinia'
|
2024-12-01 19:20:21 -05:00
|
|
|
|
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-22 13:40:25 -05:00
|
|
|
createdBy: string | null,
|
2024-12-05 20:43:01 -05:00
|
|
|
layout: string,
|
2024-12-08 20:41:54 -05:00
|
|
|
orgs: string[],
|
2024-12-05 20:43:01 -05:00
|
|
|
message1: string | null,
|
|
|
|
|
message2: string | null,
|
|
|
|
|
message3: string | null,
|
|
|
|
|
message4: string | null,
|
2024-12-04 00:20:41 -05:00
|
|
|
owner: string | null,
|
2024-12-08 14:40:46 -05:00
|
|
|
id: string,
|
2024-12-04 00:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:43:01 -05:00
|
|
|
export const activist: Sticker = {
|
2024-12-08 20:41:54 -05:00
|
|
|
color: '888888',
|
2024-12-22 13:40:25 -05:00
|
|
|
createdBy: null,
|
2024-12-05 20:43:01 -05:00
|
|
|
layout: 'layout1',
|
2024-12-08 20:41:54 -05:00
|
|
|
orgs: [],
|
2024-12-05 20:43:01 -05:00
|
|
|
message1: 'I AM AN ACTIVIST',
|
|
|
|
|
message2: null,
|
|
|
|
|
message3: null,
|
|
|
|
|
message4: null,
|
|
|
|
|
owner: null,
|
2024-12-08 14:40:46 -05:00
|
|
|
id: '6dcd2dae-7be2-4f79-8b10-caeb379452fc',
|
2024-12-05 20:43:01 -05:00
|
|
|
};
|
|
|
|
|
|
2024-12-22 13:40:25 -05:00
|
|
|
export const colors = [
|
|
|
|
|
'c9c918', 'abe11c', '8bf226',
|
|
|
|
|
'6afc37', '4bfd4c', '2ff665',
|
|
|
|
|
'18e780', '09d09a', '02b4b3',
|
|
|
|
|
'0395c9', '0d74d9', '1e54e3',
|
|
|
|
|
'3637e7', '541ee3', '740dd9',
|
|
|
|
|
'9503c9', 'b402b3', 'd0099a',
|
|
|
|
|
'e71880', 'f62f65', 'fd4b4c',
|
|
|
|
|
'fc6a37', 'f28b26', 'e1ab1c',
|
|
|
|
|
];
|
|
|
|
|
|
2024-12-01 19:20:21 -05:00
|
|
|
export interface State {
|
2024-12-08 14:40:46 -05:00
|
|
|
activeSticker: string | null; // id
|
2024-12-01 19:20:21 -05:00
|
|
|
stickers: Sticker[];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 13:40:25 -05:00
|
|
|
function randomColor() {
|
|
|
|
|
return colors[Math.floor(Math.random() * colors.length)];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 19:20:21 -05:00
|
|
|
export const useStickersStore = defineStore('stickers', {
|
|
|
|
|
actions: {
|
2024-12-22 13:40:25 -05:00
|
|
|
create(username: string) {
|
|
|
|
|
const sticker = structuredClone(activist);
|
|
|
|
|
sticker.color = randomColor();
|
|
|
|
|
sticker.createdBy = username;
|
|
|
|
|
sticker.id = self.crypto.randomUUID();
|
|
|
|
|
this.stickers.push(sticker);
|
|
|
|
|
return sticker;
|
|
|
|
|
},
|
2024-12-08 14:40:46 -05:00
|
|
|
async fetch(id: string) {
|
|
|
|
|
if (this.getSticker(id)) {
|
2024-12-01 19:20:21 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// TODO: Check if we have it already and if it's stale.
|
|
|
|
|
const response = await fetch(
|
2024-12-08 14:40:46 -05:00
|
|
|
`/api/sticker/${id}`,
|
2024-12-01 19:20:21 -05:00
|
|
|
);
|
|
|
|
|
const json = await response.json();
|
|
|
|
|
this.stickers.push(json);
|
|
|
|
|
},
|
2024-12-08 14:40:46 -05:00
|
|
|
async put(id: string) {
|
|
|
|
|
const sticker = this.getSticker(id)
|
2024-12-04 00:20:41 -05:00
|
|
|
if (!sticker) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const response = await fetch(
|
2024-12-08 14:40:46 -05:00
|
|
|
`/api/sticker/${id}`,
|
2024-12-04 00:20:41 -05:00
|
|
|
{
|
|
|
|
|
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;
|
2024-12-08 14:40:46 -05:00
|
|
|
this.put(sticker.id);
|
2024-12-04 00:20:41 -05:00
|
|
|
const userStore= useUserStore();
|
2024-12-08 14:40:46 -05:00
|
|
|
userStore.addSticker(user.username, sticker.id);
|
2024-12-04 00:20:41 -05:00
|
|
|
},
|
2024-12-01 19:20:21 -05:00
|
|
|
},
|
|
|
|
|
getters: {
|
|
|
|
|
getSticker: (state: State) => {
|
2024-12-08 14:40:46 -05:00
|
|
|
return (id: string): Sticker | undefined => {
|
|
|
|
|
return state.stickers.find((sticker: Sticker) => sticker.id === id)
|
2024-12-01 19:20:21 -05:00
|
|
|
};
|
|
|
|
|
},
|
2024-12-22 13:40:25 -05:00
|
|
|
getStickersCreatedBy: (state: State) => {
|
|
|
|
|
return (username: string | null | undefined): Sticker[] => {
|
|
|
|
|
if (!username) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
return state.stickers
|
|
|
|
|
.filter((sticker: Sticker) => sticker.createdBy === username);
|
|
|
|
|
};
|
|
|
|
|
},
|
2024-12-01 19:20:21 -05:00
|
|
|
},
|
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))
|
|
|
|
|
}
|