Skip to content

Commit f935aaf

Browse files
committed
add finished movie seat booking
1 parent 339a289 commit f935aaf

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

60-movie seat booking/script.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const container = document.querySelector(".container");
2+
const seats = document.querySelectorAll(".row .seat:not(.occupied)");
3+
const count = document.getElementById("count");
4+
const total = document.getElementById("total");
5+
const movieSelect = document.getElementById("movie");
6+
let ticketPrice = +movieSelect.value;
7+
8+
function populateUI() {
9+
const selectedSeats = JSON.parse(localStorage.getItem("selectedSeats"));
10+
if (selectedSeats !== null && selectedSeats.length > 0) {
11+
seats.forEach((seat, index) => {
12+
if (selectedSeats.indexOf(index) > -1) seat.classList.add("selected");
13+
});
14+
}
15+
const selectedMovieIndex = localStorage.getItem("selectedMovieIndex");
16+
if (selectedMovieIndex !== null)
17+
movieSelect.selectedIndex = selectedMovieIndex;
18+
}
19+
20+
function setMovieData(movieIndex, moviePrice) {
21+
localStorage.setItem("selectedMovieIndex", movieIndex);
22+
localStorage.setItem("selectedMoviePrice", moviePrice);
23+
}
24+
25+
function updateSelectedCount() {
26+
const selectedSeats = document.querySelectorAll(".row .seat.selected");
27+
const seatsIndex = [...selectedSeats].map((seat) => [...seats].indexOf(seat));
28+
localStorage.setItem("selectedSeats", JSON.stringify(seatsIndex));
29+
const selectedSeatsCount = selectedSeats.length;
30+
count.innerText = selectedSeatsCount;
31+
total.innerText = selectedSeatsCount * ticketPrice;
32+
}
33+
34+
movieSelect.addEventListener("change", (e) => {
35+
ticketPrice = +e.target.value;
36+
setMovieData(e.target.selectedIndex, e.target.value);
37+
updateSelectedCount();
38+
});
39+
40+
container.addEventListener("click", (e) => {
41+
if (
42+
e.target.classList.contains("seat") &&
43+
!e.target.classList.contains("occupied")
44+
) {
45+
e.target.classList.toggle("selected");
46+
updateSelectedCount();
47+
}
48+
});
49+
50+
// Init
51+
populateUI();
52+
updateSelectedCount();

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
| 57 | [Parallax Background with SVG](https://github.com/solygambas/html-css-fifty-projects/tree/master/57-parallax%20background%20svg) | [Live Demo](https://codepen.io/solygambas/full/vYyjjbz) |
6666
| 58 | [3D Product Card](https://github.com/solygambas/html-css-fifty-projects/tree/master/58-3D%20product%20card) | [Live Demo](https://codepen.io/solygambas/full/wvoXWPq) |
6767
| 59 | [Form Validator](https://github.com/solygambas/html-css-fifty-projects/tree/master/59-form%20validator) | [Live Demo](https://codepen.io/solygambas/full/MWbPJjb) |
68-
| 60 | [Movie Seat Booking](https://github.com/solygambas/html-css-fifty-projects/tree/master/60-movie%20seat%20booking) | [Live Demo]() |
68+
| 60 | [Movie Seat Booking](https://github.com/solygambas/html-css-fifty-projects/tree/master/60-movie%20seat%20booking) | [Live Demo](https://codepen.io/solygambas/full/xxRQEOy) |
6969

7070
Mainly based on 2 courses by Brad Traversy (2020):
7171

0 commit comments

Comments
 (0)