diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 03a1906..fd7b4d1 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -1,9 +1,18 @@ import { defineStore } from 'pinia'; -import { ID, account, functions, teams } from 'boot/appwrite'; +import { + ID, + account, + functions, + teams, + client, + AppwriteIds, +} from 'boot/appwrite'; import { ExecutionMethod, OAuthProvider, type Models } from 'appwrite'; import { computed, ref } from 'vue'; import { useBoatStore } from './boat'; import { useReservationStore } from './reservation'; +import { useIntervalStore } from './interval'; +import { Interval } from './schedule.types'; export const useAuthStore = defineStore('auth', () => { const currentUser = ref | null>(null); @@ -18,12 +27,32 @@ export const useAuthStore = defineStore('auth', () => { currentUserTeams.value = await teams.list(); await useBoatStore().fetchBoats(); await useReservationStore().fetchUserReservations(); + setupSubscriptions(); } catch { currentUser.value = null; currentUserTeams.value = null; } } + const setupSubscriptions = () => { + const intervalStore = useIntervalStore(); + client.subscribe( + [ + `databases.${AppwriteIds.databaseId}.collections.${AppwriteIds.collection.interval}.documents`, + ], + (response) => { + // Callback will be executed on changes for documents A and all files. + if ( + response.events.includes( + 'databases.65ee1cbf9c2493faf15f.collections.interval.documents.*' + ) + ) { + const interval = response.payload as Interval; + if (interval.$id) intervalStore.intervals.set(interval.$id, interval); + } + } + ); + }; const currentUserTeamNames = computed(() => currentUserTeams.value ? currentUserTeams.value.teams.map((team) => team.name) diff --git a/src/stores/interval.ts b/src/stores/interval.ts index 2bd6f1f..77b766e 100644 --- a/src/stores/interval.ts +++ b/src/stores/interval.ts @@ -138,5 +138,6 @@ export const useIntervalStore = defineStore('interval', () => { updateInterval, deleteInterval, selectedDate, + intervals, }; });