Create constant for future month search

This commit is contained in:
Alex Tselegidis 2024-05-11 15:21:41 +02:00
parent 949a73ad58
commit 37bfc08641

View file

@ -23,6 +23,8 @@ App.Http.Booking = (function () {
const $captchaHint = $('#captcha-hint'); const $captchaHint = $('#captcha-hint');
const $captchaTitle = $('.captcha-title'); const $captchaTitle = $('.captcha-title');
const MONTH_SEARCH_LIMIT = 2; // Months in the future
const moment = window.moment; const moment = window.moment;
let unavailableDatesBackup; let unavailableDatesBackup;
@ -263,32 +265,41 @@ App.Http.Booking = (function () {
data: data, data: data,
dataType: 'json', dataType: 'json',
}).done((response) => { }).done((response) => {
// In case the current month has no availability, the app will try the next one or the one after in order to
// find a date that has at least one slot
if (response.is_month_unavailable) { if (response.is_month_unavailable) {
if (!searchedMonthStart) { if (!searchedMonthStart) {
searchedMonthStart = selectedDateString; searchedMonthStart = selectedDateString;
} }
if (searchedMonthCounter >= 2) { if (searchedMonthCounter >= MONTH_SEARCH_LIMIT) {
// Need to mark the current month dates as unavailable // Need to mark the current month dates as unavailable
const selectedDateMoment = moment(searchedMonthStart); const selectedDateMoment = moment(searchedMonthStart);
const startOfMonthMoment = selectedDateMoment.clone().startOf('month'); const startOfMonthMoment = selectedDateMoment.clone().startOf('month');
const endOfMonthMoment = selectedDateMoment.clone().endOf('month'); const endOfMonthMoment = selectedDateMoment.clone().endOf('month');
const unavailableDates = []; const unavailableDates = [];
while (startOfMonthMoment.isSameOrBefore(endOfMonthMoment)) { while (startOfMonthMoment.isSameOrBefore(endOfMonthMoment)) {
unavailableDates.push(startOfMonthMoment.format('YYYY-MM-DD')); unavailableDates.push(startOfMonthMoment.format('YYYY-MM-DD'));
startOfMonthMoment.add(monthChangeStep, 'days'); // Move to the next day startOfMonthMoment.add(monthChangeStep, 'days'); // Move to the next day
} }
applyUnavailableDates(unavailableDates, searchedMonthStart, true); applyUnavailableDates(unavailableDates, searchedMonthStart, true);
searchedMonthStart = undefined; searchedMonthStart = undefined;
searchedMonthCounter = 0; searchedMonthCounter = 0;
return; // Stop searching return; // Stop searching
} }
searchedMonthCounter++; searchedMonthCounter++;
const selectedDateMoment = moment(selectedDateString); const selectedDateMoment = moment(selectedDateString);
selectedDateMoment.add(1, 'month'); selectedDateMoment.add(1, 'month');
const nextSelectedDate = selectedDateMoment.format('YYYY-MM-DD'); const nextSelectedDate = selectedDateMoment.format('YYYY-MM-DD');
getUnavailableDates(providerId, serviceId, nextSelectedDate, monthChangeStep); getUnavailableDates(providerId, serviceId, nextSelectedDate, monthChangeStep);
return; return;
} }