aboutsummaryrefslogtreecommitdiffstats
path: root/stores/favourites.ts
blob: 2bf725724036c8339f4a2d3638072b5087ccbb7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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}
})