Fix booking update and reactivity
All checks were successful
Build BAB Application Deployment Artifact / build (push) Successful in 2m28s

This commit is contained in:
2024-06-02 10:08:57 -04:00
parent 9104ccab0f
commit b2420b270c
3 changed files with 51 additions and 17 deletions

View File

@@ -221,9 +221,9 @@ const onSubmit = async () => {
) )
) { ) {
// TODO: Make a proper validator // TODO: Make a proper validator
return; return false;
} }
const reservation = <Reservation>{ const newReservation = <Reservation>{
resource: booking.interval.resource, resource: booking.interval.resource,
start: booking.interval.start, start: booking.interval.start,
end: booking.interval.end, end: booking.interval.end,
@@ -231,6 +231,7 @@ const onSubmit = async () => {
status: 'confirmed', status: 'confirmed',
reason: booking.reason, reason: booking.reason,
comment: booking.comment, comment: booking.comment,
$id: reservation.value?.$id,
}; };
const status = $q.notify({ const status = $q.notify({
color: 'secondary', color: 'secondary',
@@ -243,11 +244,11 @@ const onSubmit = async () => {
group: false, group: false,
}); });
try { try {
const r = await reservationStore.createReservation(reservation); const r = await reservationStore.createOrUpdateReservation(newReservation);
status({ status({
color: 'positive', color: 'positive',
icon: 'cloud_done', icon: 'cloud_done',
message: `Booking successful: ${ message: `Booking ${newReservation.$id ? 'updated' : 'created'}: ${
boatStore.getBoatById(r.resource)?.name boatStore.getBoatById(r.resource)?.name
} at ${formatDate(r.start)}`, } at ${formatDate(r.start)}`,
spinner: false, spinner: false,

View File

@@ -41,7 +41,7 @@
</q-card> </q-card>
<div v-else> <div v-else>
<div <div
v-for="reservation in sortedUserReservations" v-for="reservation in futureUserReservations"
:key="reservation.$id"> :key="reservation.$id">
<ReservationCardComponent :modelValue="reservation" /> <ReservationCardComponent :modelValue="reservation" />
</div> </div>
@@ -61,10 +61,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { useReservationStore } from 'src/stores/reservation'; import { useReservationStore } from 'src/stores/reservation';
import ReservationCardComponent from 'src/components/scheduling/ReservationCardComponent.vue'; import ReservationCardComponent from 'src/components/scheduling/ReservationCardComponent.vue';
import { ref } from 'vue'; import { onMounted, ref } from 'vue';
const { sortedUserReservations, futureUserReservations, pastUserReservations } = const { futureUserReservations, pastUserReservations } = useReservationStore();
useReservationStore();
onMounted(() => useReservationStore().fetchUserReservations());
const tab = ref('upcoming'); const tab = ref('upcoming');

View File

@@ -1,6 +1,6 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import type { Reservation } from './schedule.types'; import type { Reservation } from './schedule.types';
import { computed, ref } from 'vue'; import { computed, ref, watch } from 'vue';
import { AppwriteIds, databases } from 'src/boot/appwrite'; import { AppwriteIds, databases } from 'src/boot/appwrite';
import { ID, Query } from 'appwrite'; import { ID, Query } from 'appwrite';
import { date, useQuasar } from 'quasar'; import { date, useQuasar } from 'quasar';
@@ -61,17 +61,28 @@ export const useReservationStore = defineStore('reservation', () => {
} }
}; };
const createReservation = async ( const createOrUpdateReservation = async (
reservation: Reservation reservation: Reservation
): Promise<Reservation> => { ): Promise<Reservation> => {
let response;
try { try {
const response = await databases.createDocument( if (reservation.$id) {
AppwriteIds.databaseId, response = await databases.updateDocument(
AppwriteIds.collection.reservation, AppwriteIds.databaseId,
ID.unique(), AppwriteIds.collection.reservation,
reservation reservation.$id,
); reservation
);
} else {
response = await databases.createDocument(
AppwriteIds.databaseId,
AppwriteIds.collection.reservation,
ID.unique(),
reservation
);
}
reservations.value.set(response.$id, response as Reservation); reservations.value.set(response.$id, response as Reservation);
userReservations.value.set(response.$id, response as Reservation);
console.info('Reservation booked: ', response); console.info('Reservation booked: ', response);
return response as Reservation; return response as Reservation;
} catch (e) { } catch (e) {
@@ -241,10 +252,31 @@ export const useReservationStore = defineStore('reservation', () => {
return sortedUserReservations.value?.filter((b) => isPast(b.end)); return sortedUserReservations.value?.filter((b) => isPast(b.end));
}); });
// Ensure reactivity for computed properties when Map is modified
watch(
reservations,
() => {
sortedUserReservations.value;
futureUserReservations.value;
pastUserReservations.value;
},
{ deep: true }
);
watch(
userReservations,
() => {
sortedUserReservations.value;
futureUserReservations.value;
pastUserReservations.value;
},
{ deep: true }
);
return { return {
getReservationsByDate, getReservationsByDate,
getReservationById, getReservationById,
createReservation, createOrUpdateReservation,
deleteReservation, deleteReservation,
fetchReservationsForDateRange, fetchReservationsForDateRange,
isReservationOverlapped, isReservationOverlapped,