feat: rudimentary realtime update of intervals
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -138,5 +138,6 @@ export const useIntervalStore = defineStore('interval', () => {
|
||||
updateInterval,
|
||||
deleteInterval,
|
||||
selectedDate,
|
||||
intervals,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user