Sorry in advance, possibly been looking at it for t long. I have a test application which shows a horizontal list of dates and times for a selected date for booking appointment slots but can’t seem to exclude the times that would overlap into other appointment times.
Example or what should happen
A typical appointment length for this selected service is 1hr and 30mins, I have 4 appointments already booked so my app should show the following available appointment times to select from
[12:00, 16:30, 16:45, 17:00]
What is being displayed which is wrong
[12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 16:30, 16:45, 17:00]
In this case, if the user was able to select 12:30 then it would overlap into another appointment time so I would need to exclude [12:15, 12:30 etc…]
Data
availableTimes:
[’09:00′, 09:15 … 16:45, 17:00] – So opening and closing from 09:00 until 17:00
existingBooks for the selected day (2022-01-07):
[stDateTime: ‘2023-01-07 09:00’, enDateTime: ‘2023-01-07 10:30’]
[stDateTime: ‘2023-01-07 10:30’, enDateTime: ‘2023-01-07 12:00’]
FREE SLOT BETWEEN 12:00 and 13:30 so available time to show should be 12:00
[stDateTime: ‘2023-01-07 13:30’, enDateTime: ‘2023-01-07 15:00’]
[stDateTime: ‘2023-01-07 15:00’, enDateTime: ‘2023-01-07 16:30’]
I actually think it’s a problem somewhere in the loop but not sure how to alter the arrays or data.
const updateAvailableTimes = (
bookings: Array<{stDate: string; enDate: string}>,
availables: Array<string>,
currDate: string,
) => {
const newAvailables = [];
const serviceLengthInMinutes = 90; // 1hr and 30 minutes // Get the actual service length from state or passed param
for (const ava of availables) {
const curr = moment(`${currDate} ${ava}`, 'YYYY-MM-DD HH:mm');
let isBooked = false;
for (const booking of bookings) {
const st = moment(booking.stDate, 'YYYY-MM-DD HH:mm');
const en = moment(booking.enDate, 'YYYY-MM-DD HH:mm');
if (!curr.isSameOrAfter(st) || !curr.isSameOrAfter(en) && !curr.isBetween(st, en) || curr.isBefore(en)) {
if(!curr.isBefore(st) && !curr.isAfter(en)){
console.log('Booked Slot: ' + moment(en, 'HH:mm').format('HH:mm'));
newAvailables.push('');
isBooked = true;
break;
}
}
}
if (!isBooked) {
console.log('Available Slot: ' + moment(ava, 'HH:mm').format('HH:mm'))
newAvailables.push(ava);
}
}
return newAvailables;
};