Skip to content

Commit

Permalink
script.js done
Browse files Browse the repository at this point in the history
  • Loading branch information
alej4ndro-cm committed Oct 6, 2024
1 parent bc4efb5 commit 1066b5f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions form-validator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ <h2>Register With Us</h2>
</div>

<script src="script.js"></script>

</body>
</html>
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@
</label>
</div>
<div class="container">
<h1>Welcome to WebVanillaProjects</h1>
<p>Explore our collection of vanilla JavaScript projects. Click on a project to get started!</p>
<h1>Welcome to Web Vanilla Projects</h1>
<p>Explore a collection of vanilla frontend JavaScript projects. Click on a project to get started!</p>
<div class="project-list">
<a href="form-validator/index.html" class="project-button">Form Validator Project</a>
<a href="movie-seat-booking/index.html" class="project-button">Movie Seat Booking Project</a>
Expand Down
14 changes: 14 additions & 0 deletions movie-seat-booking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Movie Seat Booking

Display movie choices and seats in a theater to select from in order to purchase tickets

## Project Specifications

- Display UI with movie select, screen, seats, legend & seat info
- User can select a movie/price
- User can select/deselect seats
- User can not select occupied seats
- Number of seats and price will update
- Save seats, movie and price to local storage so that UI is still populated on refresh

Design inspiration from [Dribbble](https://dribbble.com/shots/3628370-Movie-Seat-Booking)
Binary file added movie-seat-booking/results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions movie-seat-booking/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const container = document.querySelector('.container'); //<div class="container">
const seats = document.querySelectorAll('.row .seat:not(.occupied)'); //grab all the seats in a row that are not occupied
const count = document.getElementById('count');
const total = document.getElementById('total');
const movieSelect = document.getElementById('movie');

populateUI();

//Initial selected ticket price
let ticketPrice = +movieSelect.value;

//Save selected movie index and price
function setMovieData(movieIndex, moviePrice){
localStorage.setItem('selectedMovieIndex', movieIndex);
localStorage.setItem('selectedMoviePrice', moviePrice);
}

//Copy selected seats into an array
//Map through array and return a new array indexes
function updateSelectedCount() {
const selectedSeats = document.querySelectorAll('.row .seat.selected');

//... copy the elements of an array
const seatsIndex = [...selectedSeats].map((seat)=>[...seats].indexOf(seat)); //return the index of the seats that are selected

localStorage.setItem('selectedSeats', JSON.stringify(seatsIndex));

//get the lentgh of the node list
const selectedSeatsCount = selectedSeats.length;

count.innerText = selectedSeatsCount;
total.innerText = selectedSeatsCount * ticketPrice;
}

//Get data from localstorage and populate UI
function populateUI() {
const selectedSeats = JSON.parse(localStorage.getItem('selectedSeats'));

if(selectedSeats !== null && selectedSeats.length > 0) {
seats.forEach((seat, index)=>{
if(selectedSeats.indexOf(index)>-1) {
seat.classList.add('selected');
}
});
}

const selectedMovieIndex = localStorage.getItem('selectedMovieIndex');

if(selectedMovieIndex !== null){
movieSelect.selectedIndex = selectedMovieIndex;
}
}

//Movie select event
movieSelect.addEventListener('change', e=> {
ticketPrice = +e.target.value;
setMovieData(e.target.selectedIndex, e.target.value);
updateSelectedCount();
})

//Seat click event
//When I click on a seat I wanna change the class to selected
//Note: function(e) is the same as (e)=>
container.addEventListener('click', (e)=>{
if(e.target.classList.contains('seat') && !e.target.classList.contains('occupied')){
e.target.classList.toggle('selected');

updateSelectedCount();
}
});

//Initial count and total set
updateSelectedCount();

0 comments on commit 1066b5f

Please sign in to comment.