feat: rudimentary realtime update of intervals

This commit is contained in:
2024-06-20 23:36:05 -04:00
parent 5e8c5a1631
commit 64a59e856f
2 changed files with 31 additions and 1 deletions

View File

@@ -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<Models.User<Models.Preferences> | 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)

View File

@@ -138,5 +138,6 @@ export const useIntervalStore = defineStore('interval', () => {
updateInterval,
deleteInterval,
selectedDate,
intervals,
};
});