-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
109 lines (73 loc) · 3.06 KB
/
script.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// @ts-nocheck
const hallContainer = document.querySelector(".hall-container");
const seats = document.querySelectorAll(".row .seat");
const count = document.querySelector("#count");
const total = document.querySelector("#total");
const clearButton = document.querySelector(".button.clear");
const movieSelect = document.querySelector("#movie");
populateUI();
updateSelectedInfo();
// Add event listener to hallContainer because
// this way it will not be neccessary to add event listener to each seat
// but only to the seat that was clicked on
hallContainer.addEventListener("click", selectSeat);
// Movie select event
movieSelect.addEventListener("change", e => {
setMovieData(e.target.selectedIndex, e.target.value);
updateSelectedInfo();
});
// Clear button event
clearButton.addEventListener("click", clearSelectedSeats);
function clearSelectedSeats() {
localStorage.setItem("selectedSeats", JSON.stringify([]));
const selectedSeats = document.querySelectorAll(".row .seat.selected");
selectedSeats.forEach(seat => {
seat.classList.remove("selected");
})
updateSelectedInfo();
}
// Mark selected seat with .selected class
function selectSeat(e) {
if (e.target.classList.contains("seat") && !e.target.classList.contains("occupied")) {
e.target.classList.toggle("selected");
updateSelectedInfo();
}
}
// Update count and total
function updateSelectedInfo() {
const selectedSeats = document.querySelectorAll(".row .seat.selected");
const selectedSeatsIndex = [...selectedSeats].map(function (seat) {
return [...seats].indexOf(seat);
});
localStorage.setItem("selectedSeats", JSON.stringify(selectedSeatsIndex))
const ticketPrice = parseInt(localStorage.getItem("selectedMoviePrice"), 10);
count.innerText = selectedSeats.length;
total.innerText = selectedSeats.length * ticketPrice;
}
// Save selected movie index and price into Local Storage
function setMovieData(movieIndex, moviePrice) {
localStorage.setItem("selectedMovieIndex", movieIndex);
localStorage.setItem("selectedMoviePrice", moviePrice);
}
// Get data (selected seats and movie index) from Local Storage and populate UI
function populateUI() {
const selectedSeats = JSON.parse(localStorage.getItem("selectedSeats"));
// Check if something is in Local Storage && and if it is check if it is not an empty array
if (selectedSeats !== null && selectedSeats.length > 0) {
selectedSeats.forEach((seatIndex) => {
[...seats][seatIndex].classList.add("selected");
});
}
const selectedMovieIndex = localStorage.getItem("selectedMovieIndex");
if (selectedMovieIndex !== null) {
movieSelect.selectedIndex = selectedMovieIndex;
} else {
localStorage.setItem("selectedMovieIndex", movieSelect.selectedIndex);
}
const selectedMoviePrice = localStorage.getItem("selectedMoviePrice");
if (selectedMoviePrice !== null) {
movieSelect.value = selectedMoviePrice;
} else {
localStorage.setItem("selectedMoviePrice", movieSelect.value);
}
}