From 28600578f1e222d5d5abb2e459086e9709825d1c Mon Sep 17 00:00:00 2001 From: Patrick Toal Date: Tue, 30 Apr 2024 17:04:55 -0400 Subject: [PATCH] Fix update of timblock --- .../boat/BoatScheduleTableComponent.vue | 8 +- src/pages/schedule/BoatReservationPage.vue | 74 ++++++++++--------- src/stores/schedule.ts | 7 +- src/stores/schedule.types.ts | 2 +- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/components/scheduling/boat/BoatScheduleTableComponent.vue b/src/components/scheduling/boat/BoatScheduleTableComponent.vue index 4a50b09..9beaacb 100644 --- a/src/components/scheduling/boat/BoatScheduleTableComponent.vue +++ b/src/components/scheduling/boat/BoatScheduleTableComponent.vue @@ -90,17 +90,13 @@ interface BoatData extends Boat { const scheduleStore = useScheduleStore(); const boatStore = useBoatStore(); -const selectedBlock = ref(null); +const selectedBlock = defineModel(); const selectedDate = ref(today()); const boatData = ref(boatStore.boats); const calendar = ref(null); -const emit = defineEmits<{ - updateBoatTime: [block: Timeblock]; -}>(); - function handleSwipe({ ...event }) { event.direction === 'right' ? calendar.value?.prev() : calendar.value?.next(); } @@ -168,8 +164,6 @@ function selectBlock(event: MouseEvent, scope: DayBodyScope, block: Timeblock) { selectedBlock.value === block ? (selectedBlock.value = null) : (selectedBlock.value = block); - - emit('updateBoatTime', block); } function changeEvent({ start }: { start: string }) { diff --git a/src/pages/schedule/BoatReservationPage.vue b/src/pages/schedule/BoatReservationPage.vue index 120aa6c..0b45413 100644 --- a/src/pages/schedule/BoatReservationPage.vue +++ b/src/pages/schedule/BoatReservationPage.vue @@ -24,7 +24,7 @@ Use the calendar to pick a date. Select an available boat and timeslot below. - + (); +const bookingForm = ref({ bookingId: scheduleStore.getNewId(), name: auth.currentUser?.name, boat: undefined, @@ -91,22 +100,21 @@ const bookingForm = reactive({ endDate: date.formatDate(new Date(), dateFormat), }); -watch(bookingForm, (b, a) => { - const newRes = { - id: b.bookingId, - user: b.name, - resource: b.boat, - start: date.extractDate(b.startDate, dateFormat), - end: date.extractDate(b.endDate, dateFormat), - reservationDate: new Date(), - status: 'tentative', - }; - //TODO: Turn this into a validator. - scheduleStore.isReservationOverlapped(newRes) - ? Dialog.create({ message: 'This booking overlaps another!' }) - : scheduleStore.addOrCreateReservation(newRes); +watch(timeblock, (tb_new) => { + console.log('Hi'); + bookingForm.value.boat = useBoatStore().boats.find( + (b) => b.$id === tb_new?.boatId + ); + bookingForm.value.startDate = date.formatDate(tb_new?.start, dateFormat); + bookingForm.value.endDate = date.formatDate(tb_new?.end, dateFormat); + console.log(tb_new); }); +// //TODO: Turn this into a validator. +// scheduleStore.isReservationOverlapped(newRes) +// ? Dialog.create({ message: 'This booking overlaps another!' }) +// : scheduleStore.addOrCreateReservation(newRes); + const onReset = () => { // TODO }; @@ -115,27 +123,27 @@ const onSubmit = () => { // TODO }; -const updateBoat = (block: Timeblock) => { - bookingForm.boat = useBoatStore().boats.find((b) => b.$id === block.boatId); - bookingForm.startDate = date.formatDate(block.start, dateFormat); - bookingForm.endDate = date.formatDate(block.end, dateFormat); - console.log(bookingForm.startDate); -}; const bookingDuration = computed(() => { - const diff = date.getDateDiff( - bookingForm.endDate, - bookingForm.startDate, - 'minutes' - ); - return diff <= 0 - ? 'Invalid' - : (diff > 60 ? Math.trunc(diff / 60) + ' hours' : '') + - (diff % 60 > 0 ? ' ' + (diff % 60) + ' minutes' : ''); + if (bookingForm.value.endDate && bookingForm.value.startDate) { + const diff = date.getDateDiff( + bookingForm.value.endDate, + bookingForm.value.startDate, + 'minutes' + ); + return diff <= 0 + ? 'Invalid' + : (diff > 60 ? Math.trunc(diff / 60) + ' hours' : '') + + (diff % 60 > 0 ? ' ' + (diff % 60) + ' minutes' : ''); + } else { + return 0; + } }); const bookingSummary = computed(() => { - return bookingForm.boat && bookingForm.startDate && bookingForm.endDate - ? `${bookingForm.boat.name} @ ${bookingForm.startDate} for ${bookingDuration.value}` + return bookingForm.value.boat && + bookingForm.value.startDate && + bookingForm.value.endDate + ? `${bookingForm.value.boat.name} @ ${bookingForm.value.startDate} for ${bookingDuration.value}` : ''; }); diff --git a/src/stores/schedule.ts b/src/stores/schedule.ts index 3c7a1a8..d7c88a3 100644 --- a/src/stores/schedule.ts +++ b/src/stores/schedule.ts @@ -81,9 +81,12 @@ export const useScheduleStore = defineStore('schedule', () => { return isResourceTimeOverlapped(res.resource, res.start, res.end); }; - const getNewId = () => { + const getNewId = (): string => { + return [...Array(20)] + .map(() => Math.floor(Math.random() * 16).toString(16)) + .join(''); // Trivial placeholder - return Math.max(...reservations.value.map((item) => item.id)) + 1; + //return Math.max(...reservations.value.map((item) => item.id)) + 1; }; const addOrCreateReservation = (reservation: Reservation) => { diff --git a/src/stores/schedule.types.ts b/src/stores/schedule.types.ts index 3724e25..d03dba7 100644 --- a/src/stores/schedule.types.ts +++ b/src/stores/schedule.types.ts @@ -2,7 +2,7 @@ import type { Boat } from './boat'; export type StatusTypes = 'tentative' | 'confirmed' | 'pending' | undefined; export interface Reservation { - id: number; + id: string; user: string; start: Date; end: Date;