Files
cat/scripts/booking.js
T

104 lines
3.6 KiB
JavaScript

let roommateSearchController = null;
function updateRoommateSuggestions() {
const guestTypeElement = document.getElementById('guest_type');
const roomTypeElement = document.getElementById('room_type');
const roommateInput = document.getElementById('roommates');
const suggestionList = document.getElementById('roommate_suggestions');
if (!guestTypeElement || !roomTypeElement || !roommateInput || !suggestionList) {
return;
}
const eligible = guestTypeElement.value === 'regular_guest'
&& ['double', 'multi'].includes(roomTypeElement.value);
const query = roommateInput.value.trim();
suggestionList.replaceChildren();
if (!eligible || query.length < 2) {
if (roommateSearchController) {
roommateSearchController.abort();
roommateSearchController = null;
}
return;
}
if (roommateSearchController) {
roommateSearchController.abort();
}
roommateSearchController = new AbortController();
const params = new URLSearchParams({
q: query,
room_type: roomTypeElement.value,
});
fetch(`./roommate_suggestions.php?${params.toString()}`, {
method: 'GET',
headers: { Accept: 'application/json' },
signal: roommateSearchController.signal,
})
.then((response) => response.ok ? response.json() : [])
.then((names) => {
if (!Array.isArray(names)) {
return;
}
names.forEach((name) => {
if (typeof name !== 'string') {
return;
}
const option = document.createElement('option');
option.value = name;
suggestionList.appendChild(option);
});
})
.catch((error) => {
if (error.name !== 'AbortError') {
console.warn('Zimmernachbar-Vorschläge konnten nicht geladen werden.');
}
});
}
function updateRoomOptions() {
const guestTypeElement = document.getElementById('guest_type');
const roomTypeElement = document.getElementById('room_type');
const regularOptions = document.getElementById('regular_guest_options');
const dayOptions = document.getElementById('day_guest_options');
const roommateInfo = document.getElementById('roommate_info');
const roommateInput = document.getElementById('roommates');
const guestType = guestTypeElement ? guestTypeElement.value : '';
const roomType = roomTypeElement ? roomTypeElement.value : '';
const roommateEligible = guestType === 'regular_guest' && ['double', 'multi'].includes(roomType);
if (regularOptions) {
regularOptions.style.display = guestType === 'regular_guest' ? 'block' : 'none';
}
if (dayOptions) {
dayOptions.style.display = guestType === 'day_guest' ? 'block' : 'none';
}
if (roommateInfo) {
roommateInfo.style.display = roommateEligible ? 'block' : 'none';
}
if (roommateInput) {
roommateInput.disabled = !roommateEligible;
}
updateRoommateSuggestions();
}
document.addEventListener('DOMContentLoaded', function () {
const guestTypeElement = document.getElementById('guest_type');
const roomTypeElement = document.getElementById('room_type');
const roommateInput = document.getElementById('roommates');
if (guestTypeElement) {
guestTypeElement.addEventListener('change', updateRoomOptions);
}
if (roomTypeElement) {
roomTypeElement.addEventListener('change', updateRoomOptions);
}
if (roommateInput) {
roommateInput.addEventListener('input', updateRoommateSuggestions);
}
updateRoomOptions();
});