aboutsummaryrefslogtreecommitdiffstats
path: root/stores/favourites.ts
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2025-01-17 13:42:21 +0000
committerLeonardo Bishop <me@leonardobishop.com>2025-01-17 13:42:21 +0000
commit70ebc77f843207a1d4b46c8d960dafbff37e7e2e (patch)
tree2d03f7a66b877bb6ffa2f92c0504ac90f26db55f /stores/favourites.ts
Initial commit
Diffstat (limited to 'stores/favourites.ts')
-rw-r--r--stores/favourites.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/stores/favourites.ts b/stores/favourites.ts
new file mode 100644
index 0000000..2bf7257
--- /dev/null
+++ b/stores/favourites.ts
@@ -0,0 +1,43 @@
+import { defineStore } from "pinia";
+import { type Event } from "./schedule";
+
+interface Favourite {
+ id: number;
+ eventGuid?: string;
+ eventId?: number;
+}
+
+export const useFavouritesStore = defineStore('favourites', () => {
+ const favourites = ref([] as Favourite[])
+ const status = ref('idle' as 'idle' | 'pending')
+
+ const setFavourites = (newFavourites: Favourite[]) => {
+ favourites.value = newFavourites
+ }
+ const addFavourite = (favourite: Favourite) => {
+ favourites.value.push(favourite)
+ }
+ const removeFavourite = (favourite: { eventGuid?: string, eventId?: number }) => {
+ if (favourite.eventGuid) {
+ favourites.value = favourites.value.filter(f => f.eventGuid !== favourite.eventGuid)
+ }
+ if (favourite.eventId) {
+ favourites.value = favourites.value.filter(f => f.eventId !== favourite.eventId)
+ }
+ }
+ const isFavourite = (event: Event) => {
+ return favourites.value.some(f => {
+ if (f.eventGuid) {
+ return f.eventGuid === event.guid
+ } else if (f.eventId) {
+ return f.eventId === event.id
+ }
+ })
+ }
+
+ const setStatus = (newStatus: 'idle' | 'pending') => {
+ status.value = newStatus
+ }
+
+ return {favourites, status, setFavourites, addFavourite, removeFavourite, isFavourite, setStatus}
+})