127 lines
4.7 KiB
JavaScript
127 lines
4.7 KiB
JavaScript
// Gemeinsame JavaScript-Funktionen für das Projekt "cat".
|
|
|
|
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 earlyDepartureEnabled = document.getElementById('early_departure_enabled');
|
|
const earlyDepartureOptions = document.getElementById('early_departure_options');
|
|
const earlyDepartureInputs = document.querySelectorAll('input[name="early_departure"]');
|
|
|
|
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;
|
|
}
|
|
const earlyDepartureVisible = guestType === 'regular_guest'
|
|
&& Boolean(earlyDepartureEnabled && earlyDepartureEnabled.checked);
|
|
if (earlyDepartureEnabled) {
|
|
earlyDepartureEnabled.disabled = guestType !== 'regular_guest';
|
|
if (guestType !== 'regular_guest') {
|
|
earlyDepartureEnabled.checked = false;
|
|
}
|
|
}
|
|
if (earlyDepartureOptions) {
|
|
earlyDepartureOptions.style.display = earlyDepartureVisible ? 'block' : 'none';
|
|
}
|
|
earlyDepartureInputs.forEach((input) => {
|
|
input.disabled = !earlyDepartureVisible;
|
|
});
|
|
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);
|
|
}
|
|
const earlyDepartureEnabled = document.getElementById('early_departure_enabled');
|
|
if (earlyDepartureEnabled) {
|
|
earlyDepartureEnabled.addEventListener('change', updateRoomOptions);
|
|
}
|
|
updateRoomOptions();
|
|
});
|