diff --git a/assets/js/http/booking_http_client.js b/assets/js/http/booking_http_client.js index 9a2e66b2..67bc89b7 100755 --- a/assets/js/http/booking_http_client.js +++ b/assets/js/http/booking_http_client.js @@ -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; } @@ -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; @@ -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; } diff --git a/assets/js/pages/booking.js b/assets/js/pages/booking.js index a2c9b16f..fab6f9cc 100644 --- a/assets/js/pages/booking.js +++ b/assets/js/pages/booking.js @@ -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. */ @@ -104,6 +116,8 @@ App.Pages.Booking = (function () { } monthTimeout = setTimeout(() => { + const previousMoment = moment(instance.selectedDates[0]); + const displayedMonthMoment = moment( instance.currentYearElement.value + '-' + @@ -111,16 +125,21 @@ App.Pages.Booking = (function () { '-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 + '-' + @@ -128,10 +147,13 @@ App.Pages.Booking = (function () { '-01', ); + const monthChangeStep = detectDatepickerMonthChangeStep(previousMoment, displayedMonthMoment); + App.Http.Booking.getUnavailableDates( $selectProvider.val(), $selectService.val(), displayedMonthMoment.format('YYYY-MM-DD'), + monthChangeStep, ); }, 500); },