Mohit69-User
/
Movie-Booking-JS---DOM-Manipulation-JS-Changing-this-call-bind-apply---Post---qnvb5g7gvq1x
Public
forked from Shraddha08-cmyk/Movie-Booking-JS---DOM-Manipulation-JS-Changing-this-call-bind-apply---Post---qnvb5g7gvq1x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
164 lines (150 loc) · 5.96 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {fetchMovieAvailability,fetchMovieList} from "./api.js"
const mainElement = document.getElementById('main');
let bookerGridHolder = document.getElementById('booker-grid-holder');
let bookTicketBtn = document.getElementById('book-ticket-btn');
let booker = document.getElementById('booker');
async function renderMovies() {
mainElement.innerHTML = `<div id='loader'></div>`;
let movies = await fetchMovieList();
let movieHolder = document.createElement('div');
movieHolder.setAttribute('class', 'movie-holder');
movies.forEach(movie => {
createSeparateMovieTabs(movie, movieHolder);
});
mainElement.innerHTML = '';
mainElement.appendChild(movieHolder);
setEventToLinks();
}
renderMovies();
function createSeparateMovieTabs(data, wrapper) {
let a = document.createElement('a');
a.setAttribute('data-movie-name', `${data.name}`);
a.classList.add('movie-link');
a.href = `#${data.name}`;
a.innerHTML = `<div class="movie" data-id=${data.name}>
<div class="movie-img-wrapper" style="background-image:url(${data.imgUrl})"></div>
<h4>${data.name}</h4>
</div>`;
wrapper.appendChild(a);
}
function setEventToLinks() {
let movieLinks = document.querySelectorAll('.movie-link');
movieLinks.forEach(movieLink => {
movieLink.addEventListener('click', (e) => {
renderSeatsGrid(movieLink.getAttribute('data-movie-name'));
})
})
}
async function renderSeatsGrid(movieName) {
bookerGridHolder.innerHTML = `<div id='loader'></div>`;
bookTicketBtn.classList.add('v-none');
let data = await fetchMovieAvailability(movieName);
renderSeats(data);
setEventsToSeats();
}
function renderSeats(data) {
if(booker.firstElementChild.tagName !== "H3"){
seatsSelected = [];
booker.innerHTML = `<h3 class="v-none">Seat Selector</h3>
<div id="booker-grid-holder"></div>
<button id="book-ticket-btn" class="v-none">Book my seats</button>`;
}
bookerGridHolder = document.getElementById('booker-grid-holder');
bookTicketBtn = document.getElementById('book-ticket-btn');
bookerGridHolder.innerHTML = '';
booker.firstElementChild.classList.remove('v-none');
createSeatsGrid(data)
}
function createSeatsGrid(data){
let bookingGrid1 = document.createElement("div");
let bookingGrid2 = document.createElement("div");
bookingGrid2.classList.add("booking-grid");
bookingGrid1.classList.add("booking-grid");
for (let i = 1; i < 25; i++) {
let seat = document.createElement("div");
seat.innerHTML = i;
seat.setAttribute("id", `booking-grid-${i}`);
if (data.includes(i)) seat.classList.add("seat", "unavailable-seat");
else seat.classList.add("seat", "available-seat");
if (i > 12) bookingGrid2.appendChild(seat);
else bookingGrid1.appendChild(seat);
}
bookerGridHolder.appendChild(bookingGrid1);
bookerGridHolder.appendChild(bookingGrid2);
setTicketBooking();
}
let seatsSelected = [];
function setEventsToSeats() {
let AvaliableSeats = document.querySelectorAll('.available-seat');
AvaliableSeats.forEach(seat => {
seat.addEventListener('click', _ => {
saveSelectedSeat(seat);
})
})
}
function saveSelectedSeat(seat) {
if (!seat.classList.contains("select-seat")) {
seat.classList.add('select-seat');
seatsSelected.push(seat.innerText);
bookTicketBtn.classList.remove('v-none');
} else {
seat.classList.remove('select-seat');
seatsSelected = seatsSelected.filter(item => seat.innerText !== item);
if (seatsSelected.length == 0){
bookTicketBtn.classList.add('v-none');
}
}
}
function setTicketBooking(){
bookTicketBtn.addEventListener('click', () => {
if (seatsSelected.length > 0) {
booker.innerHTML = '';
confirmTicket();
}
})
}
function confirmTicket(){
let confirmTicketElement = document.createElement('div');
confirmTicketElement.setAttribute('id', 'confirm-purchase');
let h3 = document.createElement('h3');
h3.innerText = `Confirm your booking for seat numbers:${seatsSelected.join(",")}`;
confirmTicketElement.appendChild(h3);
confirmTicketElement.appendChild(createForm());
booker.appendChild(confirmTicketElement);
success();
}
function createForm(){
let form = document.createElement("form");
let formElements = `<input type="email" id="email" placeholder="email" required><br><br>
<input type="tel" id="phone" placeholder="phone" required><br><br>
<button id="submitBtn" type="submit">Purchase</button>`;
form.setAttribute("method", "post");
form.setAttribute("id", "customer-detail-form");
form.innerHTML = formElements;
return form;
}
function success(){
let submitBtn = document.getElementById('submitBtn');
submitBtn.addEventListener('click',(e) => {
let form = document.getElementById('customer-detail-form');
if(form.checkValidity()){
e.preventDefault();
let email = document.getElementById('email').value;
let phone = document.getElementById('phone').value;
renderSuccessMessage(email, phone);
}
})
}
function renderSuccessMessage(email, phone){
booker.innerHTML = '';
createSuccessMessage(email, phone);
}
function createSuccessMessage(email, phone){
let successElement = document.createElement("div");
successElement.setAttribute("id", "success");
successElement.innerHTML = `<h3>Booking details</h3>
<p>Seats: ${seatsSelected.join(", ")}</p>
<p>Email: ${email}</p>
<p>Phone number: ${phone}</p>`;
booker.appendChild(successElement);
}