Skip to content

Commit

Permalink
Add logic for navigating back to the previously available date
Browse files Browse the repository at this point in the history
  • Loading branch information
alextselegidis committed Feb 18, 2024
1 parent 6fecbc7 commit 609dc93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 4 additions & 3 deletions assets/js/http/booking_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ App.Http.Booking = (function () {
* @param {Number} providerId The selected provider ID.
* @param {Number} serviceId The selected service ID.
* @param {String} selectedDateString Y-m-d value of the selected date.
* @param {Number} monthChangeStep Whether to add or subtract months.
*/
function getUnavailableDates(providerId, serviceId, selectedDateString) {
function getUnavailableDates(providerId, serviceId, selectedDateString, monthChangeStep) {
if (processingUnavailableDates) {
return;
}
Expand Down Expand Up @@ -275,7 +276,7 @@ App.Http.Booking = (function () {
const unavailableDates = [];
while (startOfMonthMoment.isSameOrBefore(endOfMonthMoment)) {
unavailableDates.push(startOfMonthMoment.format('YYYY-MM-DD'));
startOfMonthMoment.add(1, 'days'); // Move to the next day
startOfMonthMoment.add(monthChangeStep, 'days'); // Move to the next day
}
applyUnavailableDates(unavailableDates, searchedMonthStart, false);
searchedMonthStart = undefined;
Expand All @@ -287,7 +288,7 @@ App.Http.Booking = (function () {
const selectedDateMoment = moment(selectedDateString);
selectedDateMoment.add(1, 'month');
const nextSelectedDate = selectedDateMoment.format('YYYY-MM-DD');
getUnavailableDates(providerId, serviceId, nextSelectedDate);
getUnavailableDates(providerId, serviceId, nextSelectedDate, monthChangeStep);
return;
}

Expand Down
22 changes: 22 additions & 0 deletions assets/js/pages/booking.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ App.Pages.Booking = (function () {
*/
let manageMode = vars('manage_mode') || false;

/**
* Detect the month step.
*
* @param previousDateTimeMoment
* @param nextDateTimeMoment
*
* @returns {Number}
*/
function detectDatepickerMonthChangeStep(previousDateTimeMoment, nextDateTimeMoment) {
return previousDateTimeMoment.isAfter(nextDateTimeMoment) ? -1 : 1;
}

/**
* Initialize the module.
*/
Expand Down Expand Up @@ -104,34 +116,44 @@ App.Pages.Booking = (function () {
}

monthTimeout = setTimeout(() => {
const previousMoment = moment(instance.selectedDates[0]);

const displayedMonthMoment = moment(
instance.currentYearElement.value +
'-' +
String(Number(instance.monthsDropdownContainer.value) + 1).padStart(2, '0') +
'-01',
);

const monthChangeStep = detectDatepickerMonthChangeStep(previousMoment, displayedMonthMoment);

App.Http.Booking.getUnavailableDates(
$selectProvider.val(),
$selectService.val(),
displayedMonthMoment.format('YYYY-MM-DD'),
monthChangeStep,
);
}, 500);
},

onYearChange: (selectedDates, dateStr, instance) => {
setTimeout(() => {
const previousMoment = moment(instance.selectedDates[0]);

const displayedMonthMoment = moment(
instance.currentYearElement.value +
'-' +
(Number(instance.monthsDropdownContainer.value) + 1) +
'-01',
);

const monthChangeStep = detectDatepickerMonthChangeStep(previousMoment, displayedMonthMoment);

App.Http.Booking.getUnavailableDates(
$selectProvider.val(),
$selectService.val(),
displayedMonthMoment.format('YYYY-MM-DD'),
monthChangeStep,
);
}, 500);
},
Expand Down

0 comments on commit 609dc93

Please sign in to comment.