Correctly validate selected date range

This commit is contained in:
Björn Fromme
2018-10-05 08:54:51 +02:00
parent 2cb8e95933
commit 46c3e82ffc
4 changed files with 52 additions and 15 deletions
@@ -142,6 +142,16 @@
}
return this.selectedTo.diff(this.selectedFrom, 'days');
},
minNightsForSelectedDate () {
if (this.selectedFrom === null) {
return 0;
}
const key = this.selectedFrom.format('YYYY-MM-DD');
if (!key in this.availableDates) {
return 0;
}
return this.availableDates[key].minNights;
},
message () {
if (this.loading) {
return '';
@@ -149,6 +159,10 @@
if (this.selectedFrom === null || this.selectedTo === null) {
return 'Bitte einen Zeitraum auswählen.';
}
const minNights = this.minNightsForSelectedDate;
if (this.selectedNumberOfDays < minNights) {
return 'Bitte mindestens ' + minNights + ' Nächte auswählen.';
}
if (this.availableRooms.length === 0) {
return 'Im gewählten Zeitraum sind leider keine Zimmer verfügbar.';
}
@@ -166,11 +180,13 @@
this.loading = true;
$.post(this.contingentEndpointUri, query, (json) => {
this.loading = false;
this.availableDates = json;
const enabledDates = [];
if (json.length) {
for (const entry of this.availableDates) {
enabledDates.push(entry.date);
if (json) {
this.availableDates = json;
const enabledDates = [];
for (const date in this.availableDates) {
if (this.availableDates.hasOwnProperty(date)) {
enabledDates.push(this.availableDates[date].date);
}
}
this.picker.set('enable', enabledDates);
this.picker.set('minDate', enabledDates[0]);
@@ -212,20 +228,16 @@
if (selectedDates.length === 2) {
this.selectedFrom = dayjs(selectedDates[0]);
this.selectedTo = dayjs(selectedDates[1]);
if (this.selectedNumberOfDays > 1) {
if (this.selectedNumberOfDays >= this.minNightsForSelectedDate) {
this.fetchAvailableRooms();
}
}
},
onDayCreate: (dObj, dStr, fp, dayElem) => {
const day = dayjs(dayElem.dateObj).format('YYYY-MM-DD');
for (const entry of this.availableDates) {
if (entry.date === day) {
dayElem.classList.add('available');
return;
}
if (day in this.availableDates) {
dayElem.classList.add('available');
}
}
});
Vue.nextTick(() => {