Skip to content

Commit

Permalink
Don't fetch available days when no products have been selected
Browse files Browse the repository at this point in the history
Refs #20
  • Loading branch information
Ugoku committed Oct 17, 2019
1 parent b66047d commit ab0e746
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 135 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## master
* Don't fetch available days when no products have been selected

## 1.1.1 (2019-10-03)
* Technical: Upgrade to Babel 7
* Fix position of styling in the `head`, making overriding styles easier
Expand Down
125 changes: 60 additions & 65 deletions dist/onlinebooking.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ function () {
existingEl.parentNode.removeChild(existingEl);
}

var html = "\n <form class=\"recras-discounts\">\n <div>\n <label for=\"discountcode\">".concat(this.languageHelper.translate('DISCOUNT_TITLE'), "</label>\n <input type=\"text\" id=\"discountcode\" class=\"discountcode\" maxlength=\"50\">\n </div>\n <button type=\"submit\" class=\"button-secondary\">").concat(this.languageHelper.translate('DISCOUNT_CHECK'), "</button>\n </form>\n ");
var html = "\n <form class=\"recras-discounts\">\n <div>\n <label for=\"discountcode\">".concat(this.languageHelper.translate('DISCOUNT_TITLE'), "</label>\n <input type=\"text\" id=\"discountcode\" class=\"discountcode\" maxlength=\"50\" disabled>\n </div>\n <button type=\"submit\" class=\"button-secondary\">").concat(this.languageHelper.translate('DISCOUNT_CHECK'), "</button>\n </form>\n ");
this.findElement('.recras-datetime').insertAdjacentHTML('afterend', html);
this.findElement('.recras-discounts').addEventListener('submit', function (e) {
e.preventDefault();
Expand Down Expand Up @@ -1224,91 +1224,86 @@ function () {
value: function showDateTimeSelection(pack) {
var _this23 = this;

var startDate = new Date();
var endDate = new Date();
endDate.setMonth(endDate.getMonth() + 3);
return this.getAvailableDays(pack.id, startDate, endDate).then(function () {
_this23.addDateTimeSelectionHtml();
this.addDateTimeSelectionHtml();
this.showDiscountFields();

_this23.showDiscountFields();
if (this.options.getPreFilledAmounts()) {
this.preFillAmounts(this.options.getPreFilledAmounts());
}

if (_this23.options.getPreFilledAmounts()) {
_this23.preFillAmounts(_this23.options.getPreFilledAmounts());
}
var pikadayOptions = _extends(RecrasCalendarHelper.defaultOptions(), {
disableDayFn: function disableDayFn(day) {
var dateFmt = RecrasDateHelper.datePartOnly(day);
return _this23.availableDays.indexOf(dateFmt) === -1;
},
field: this.findElement('.recras-onlinebooking-date'),
i18n: RecrasCalendarHelper.i18n(this.languageHelper),
onDraw: function onDraw(pika) {
var lastMonthYear = pika.calendars[pika.calendars.length - 1];
var lastDay = new Date(lastMonthYear.year, lastMonthYear.month, 31);

var pikadayOptions = _extends(RecrasCalendarHelper.defaultOptions(), {
disableDayFn: function disableDayFn(day) {
var dateFmt = RecrasDateHelper.datePartOnly(day);
return _this23.availableDays.indexOf(dateFmt) === -1;
},
field: _this23.findElement('.recras-onlinebooking-date'),
i18n: RecrasCalendarHelper.i18n(_this23.languageHelper),
onDraw: function onDraw(pika) {
var lastMonthYear = pika.calendars[pika.calendars.length - 1];
var lastDay = new Date(lastMonthYear.year, lastMonthYear.month, 31);

var lastAvailableDay = _this23.availableDays.reduce(function (acc, curVal) {
return curVal > acc ? curVal : acc;
}, '');

if (!lastAvailableDay) {
lastAvailableDay = new Date();
} else {
lastAvailableDay = new Date(lastAvailableDay);
}
var lastAvailableDay = _this23.availableDays.reduce(function (acc, curVal) {
return curVal > acc ? curVal : acc;
}, '');

if (lastAvailableDay > lastDay) {
return;
}
if (!lastAvailableDay) {
lastAvailableDay = new Date();
} else {
lastAvailableDay = new Date(lastAvailableDay);
}

var newEndDate = RecrasDateHelper.clone(lastAvailableDay);
newEndDate.setFullYear(lastMonthYear.year);
newEndDate.setMonth(lastMonthYear.month + 2);
if (lastAvailableDay > lastDay) {
return;
}

_this23.getAvailableDays(pack.id, lastAvailableDay, newEndDate);
},
onSelect: function onSelect(date) {
_this23.loadingIndicatorShow(_this23.findElement('label[for="recras-onlinebooking-time"]'));
var newEndDate = RecrasDateHelper.clone(lastAvailableDay);
newEndDate.setFullYear(lastMonthYear.year);
newEndDate.setMonth(lastMonthYear.month + 2);

_this23.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_DATE_SELECTED);
_this23.getAvailableDays(pack.id, lastAvailableDay, newEndDate);
},
onSelect: function onSelect(date) {
_this23.loadingIndicatorShow(_this23.findElement('label[for="recras-onlinebooking-time"]'));

_this23.selectedDate = date;
_this23.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_DATE_SELECTED);

_this23.getAvailableTimes(pack.id, date).then(function (times) {
times = times.map(function (time) {
return RecrasDateHelper.timePartOnly(new Date(time));
});
_this23.selectedDate = date;

_this23.showTimes(times);
_this23.getAvailableTimes(pack.id, date).then(function (times) {
times = times.map(function (time) {
return RecrasDateHelper.timePartOnly(new Date(time));
});

_this23.loadingIndicatorHide();
_this23.showTimes(times);

_this23.selectSingleTime();
});
_this23.loadingIndicatorHide();

_this23.maybeDisableBookButton();
}
});
_this23.selectSingleTime();
});

_this23.findElement('#discountcode').removeAttribute('disabled');

_this23.datePicker = new Pikaday(pikadayOptions);
_this23.maybeDisableBookButton();
}
});

_this23.findElement('.recras-onlinebooking-time').addEventListener('change', function () {
_this23.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_TIME_SELECTED);
this.datePicker = new Pikaday(pikadayOptions);
this.findElement('.recras-onlinebooking-time').addEventListener('change', function () {
_this23.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_TIME_SELECTED);

_this23.selectedTime = _this23.findElement('.recras-onlinebooking-time').value;
_this23.selectedTime = _this23.findElement('.recras-onlinebooking-time').value;

_this23.nextSectionActive('.recras-datetime', '.recras-discounts');
_this23.nextSectionActive('.recras-datetime', '.recras-discounts');

_this23.nextSectionActive(null, '.recras-contactform');
_this23.nextSectionActive(null, '.recras-contactform');

_this23.selectedDate = RecrasDateHelper.setTimeForDate(_this23.selectedDate, _this23.selectedTime);
_this23.selectedDate = RecrasDateHelper.setTimeForDate(_this23.selectedDate, _this23.selectedTime);

if (_this23.options.getPreviewTimes() === true) {
_this23.previewTimes();
}
if (_this23.options.getPreviewTimes() === true) {
_this23.previewTimes();
}

_this23.maybeDisableBookButton();
});
_this23.maybeDisableBookButton();
});
}
}, {
Expand Down
133 changes: 63 additions & 70 deletions src/booking.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ class RecrasBooking {
<form class="recras-discounts">
<div>
<label for="discountcode">${ this.languageHelper.translate('DISCOUNT_TITLE') }</label>
<input type="text" id="discountcode" class="discountcode" maxlength="50">
<input type="text" id="discountcode" class="discountcode" maxlength="50" disabled>
</div>
<button type="submit" class="button-secondary">${ this.languageHelper.translate('DISCOUNT_CHECK') }</button>
</form>
Expand Down Expand Up @@ -989,82 +989,75 @@ class RecrasBooking {
this.appendHtml(html);
}
showDateTimeSelection(pack) {
let startDate = new Date();
let endDate = new Date();
endDate.setMonth(endDate.getMonth() + 3);
this.addDateTimeSelectionHtml();
this.showDiscountFields();

return this.getAvailableDays(pack.id, startDate, endDate)
.then(() => {
this.addDateTimeSelectionHtml();
this.showDiscountFields();

if (this.options.getPreFilledAmounts()) {
this.preFillAmounts(this.options.getPreFilledAmounts());
}

let pikadayOptions = Object.assign(
RecrasCalendarHelper.defaultOptions(),
{
disableDayFn: (day) => {
let dateFmt = RecrasDateHelper.datePartOnly(day);
return this.availableDays.indexOf(dateFmt) === -1;
},
field: this.findElement('.recras-onlinebooking-date'),
i18n: RecrasCalendarHelper.i18n(this.languageHelper),
onDraw: (pika) => {
let lastMonthYear = pika.calendars[pika.calendars.length - 1];
let lastDay = new Date(lastMonthYear.year, lastMonthYear.month, 31);

let lastAvailableDay = this.availableDays.reduce((acc, curVal) => {
return curVal > acc ? curVal : acc;
}, '');
if (!lastAvailableDay) {
lastAvailableDay = new Date();
} else {
lastAvailableDay = new Date(lastAvailableDay);
}
if (lastAvailableDay > lastDay) {
return;
}

let newEndDate = RecrasDateHelper.clone(lastAvailableDay);
newEndDate.setFullYear(lastMonthYear.year);
newEndDate.setMonth(lastMonthYear.month + 2);

this.getAvailableDays(pack.id, lastAvailableDay, newEndDate);
},
onSelect: (date) => {
this.loadingIndicatorShow(this.findElement('label[for="recras-onlinebooking-time"]'));
this.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_DATE_SELECTED);
this.selectedDate = date;
this.getAvailableTimes(pack.id, date).then(times => {
times = times.map(time => RecrasDateHelper.timePartOnly(new Date(time)));
this.showTimes(times);
this.loadingIndicatorHide();
this.selectSingleTime();
});
this.maybeDisableBookButton();
},
if (this.options.getPreFilledAmounts()) {
this.preFillAmounts(this.options.getPreFilledAmounts());
}
let pikadayOptions = Object.assign(
RecrasCalendarHelper.defaultOptions(),
{
disableDayFn: (day) => {
let dateFmt = RecrasDateHelper.datePartOnly(day);
return this.availableDays.indexOf(dateFmt) === -1;
},
field: this.findElement('.recras-onlinebooking-date'),
i18n: RecrasCalendarHelper.i18n(this.languageHelper),
onDraw: (pika) => {
let lastMonthYear = pika.calendars[pika.calendars.length - 1];
let lastDay = new Date(lastMonthYear.year, lastMonthYear.month, 31);

let lastAvailableDay = this.availableDays.reduce((acc, curVal) => {
return curVal > acc ? curVal : acc;
}, '');
if (!lastAvailableDay) {
lastAvailableDay = new Date();
} else {
lastAvailableDay = new Date(lastAvailableDay);
}
);
if (lastAvailableDay > lastDay) {
return;
}

let newEndDate = RecrasDateHelper.clone(lastAvailableDay);
newEndDate.setFullYear(lastMonthYear.year);
newEndDate.setMonth(lastMonthYear.month + 2);

this.getAvailableDays(pack.id, lastAvailableDay, newEndDate);
},
onSelect: (date) => {
this.loadingIndicatorShow(this.findElement('label[for="recras-onlinebooking-time"]'));
this.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_DATE_SELECTED);
this.selectedDate = date;
this.getAvailableTimes(pack.id, date).then(times => {
times = times.map(time => RecrasDateHelper.timePartOnly(new Date(time)));
this.showTimes(times);
this.loadingIndicatorHide();
this.selectSingleTime();
});
this.findElement('#discountcode').removeAttribute('disabled');
this.maybeDisableBookButton();
},
}
);

this.datePicker = new Pikaday(pikadayOptions);
this.datePicker = new Pikaday(pikadayOptions);

this.findElement('.recras-onlinebooking-time').addEventListener('change', () => {
this.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_TIME_SELECTED);
this.selectedTime = this.findElement('.recras-onlinebooking-time').value;
this.findElement('.recras-onlinebooking-time').addEventListener('change', () => {
this.eventHelper.sendEvent(RecrasEventHelper.PREFIX_BOOKING, RecrasEventHelper.EVENT_BOOKING_TIME_SELECTED);
this.selectedTime = this.findElement('.recras-onlinebooking-time').value;

this.nextSectionActive('.recras-datetime', '.recras-discounts');
this.nextSectionActive(null, '.recras-contactform');
this.nextSectionActive('.recras-datetime', '.recras-discounts');
this.nextSectionActive(null, '.recras-contactform');

this.selectedDate = RecrasDateHelper.setTimeForDate(this.selectedDate, this.selectedTime);
if (this.options.getPreviewTimes() === true) {
this.previewTimes();
}
this.selectedDate = RecrasDateHelper.setTimeForDate(this.selectedDate, this.selectedTime);
if (this.options.getPreviewTimes() === true) {
this.previewTimes();
}

this.maybeDisableBookButton();
});
});
this.maybeDisableBookButton();
});
}

showPackages(packages) {
Expand Down

0 comments on commit ab0e746

Please sign in to comment.