-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (51 loc) · 1.87 KB
/
Copy pathscript.js
File metadata and controls
61 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const $ = (id) => document.getElementById(id);
const sections = {
menu: $('restaurantMenu'),
dateTime: $('dateTimeSection'),
info: $('reservationInfoSection'),
confirmation: $('confirmationMessage'),
};
const state = { restaurant: '' };
function show(...show) {
Object.values(sections).forEach((el) => el.classList.toggle('hidden', !show.includes(el)));
}
document.querySelectorAll('.restaurantButton').forEach((btn) => {
btn.addEventListener('click', () => {
state.restaurant = btn.dataset.restaurant;
show(sections.dateTime);
});
});
$('enterButton').addEventListener('click', () => {
const date = $('reservationDate').value;
const time = $('reservationTime').value;
if (!date || !time) return alert('Please select a valid date and time.');
show(sections.info);
});
$('submitReservation').addEventListener('click', () => {
const name = $('reservationName').value.trim();
const email = $('reservationEmail').value.trim();
const date = $('reservationDate').value;
const time = $('reservationTime').value;
if (!name || !email || !date || !time) return alert('Please fill in all fields.');
$('confirmationDetails').innerHTML = `
Reservation for <strong>${esc(name)}</strong><br>
Restaurant: <strong>${esc(state.restaurant)}</strong><br>
Date: <strong>${esc(date)}</strong><br>
Time: <strong>${esc(time)}</strong><br>
Email: <strong>${esc(email)}</strong>
`;
show(sections.confirmation);
});
$('backButton').addEventListener('click', reset);
function reset() {
state.restaurant = '';
['reservationDate', 'reservationTime', 'reservationName', 'reservationEmail'].forEach(
(id) => ($(id).value = ''),
);
show(sections.menu);
}
function esc(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}