feat: rudimentary realtime update of intervals
This commit is contained in:
@@ -1,9 +1,18 @@
|
|||||||
import { defineStore } from 'pinia';
|
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 { ExecutionMethod, OAuthProvider, type Models } from 'appwrite';
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
import { useBoatStore } from './boat';
|
import { useBoatStore } from './boat';
|
||||||
import { useReservationStore } from './reservation';
|
import { useReservationStore } from './reservation';
|
||||||
|
import { useIntervalStore } from './interval';
|
||||||
|
import { Interval } from './schedule.types';
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
const currentUser = ref<Models.User<Models.Preferences> | null>(null);
|
const currentUser = ref<Models.User<Models.Preferences> | null>(null);
|
||||||
@@ -18,12 +27,32 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
currentUserTeams.value = await teams.list();
|
currentUserTeams.value = await teams.list();
|
||||||
await useBoatStore().fetchBoats();
|
await useBoatStore().fetchBoats();
|
||||||
await useReservationStore().fetchUserReservations();
|
await useReservationStore().fetchUserReservations();
|
||||||
|
setupSubscriptions();
|
||||||
} catch {
|
} catch {
|
||||||
currentUser.value = null;
|
currentUser.value = null;
|
||||||
currentUserTeams.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(() =>
|
const currentUserTeamNames = computed(() =>
|
||||||
currentUserTeams.value
|
currentUserTeams.value
|
||||||
? currentUserTeams.value.teams.map((team) => team.name)
|
? currentUserTeams.value.teams.map((team) => team.name)
|
||||||
|
|||||||
@@ -138,5 +138,6 @@ export const useIntervalStore = defineStore('interval', () => {
|
|||||||
updateInterval,
|
updateInterval,
|
||||||
deleteInterval,
|
deleteInterval,
|
||||||
selectedDate,
|
selectedDate,
|
||||||
|
intervals,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user