-
Notifications
You must be signed in to change notification settings - Fork 3
/
trains.js
390 lines (328 loc) · 11.7 KB
/
trains.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const CreateTrainCard = (train_detail) => {
// console.log(train_detail)
const name = train_detail['trainname']
const id = train_detail['trainid']
const st_time = train_detail['starttime']
const t_from = train_detail['trainfrom']
const st_date = train_detail['startdate']
const dur = train_detail['duration']
const end_time = train_detail['endtime']
const t_to = train_detail['trainto']
const end_date = train_detail['enddate']
const seats = train_detail['class']
// train
const train = document.createElement('div')
train.classList.add("train")
// top
const top = document.createElement('div')
top.classList.add("top")
// train_name
const name_ele = document.createElement('p')
name_ele.classList.add("name")
const train_name = document.createTextNode(`${name}`)
name_ele.appendChild(train_name)
// train_id
const id_ele = document.createElement('p')
id_ele.classList.add("id")
const train_id = document.createTextNode(`${id}`)
id_ele.appendChild(train_id)
// append to top
top.appendChild(name_ele)
top.appendChild(id_ele)
// append TOP to TRAIN
train.appendChild(top)
// bottom
const bottom = document.createElement('div')
bottom.classList.add("bottom")
// from_div
const from_ele = document.createElement('div')
from_ele.classList.add("from")
// start time
const start_time_ele = document.createElement('p')
const train_start_time = document.createTextNode(`${st_time}`)
start_time_ele.appendChild(train_start_time)
from_ele.appendChild(start_time_ele)
// split
const split_ele_1 = document.createElement('p')
split_ele_1.classList.add("split")
const split_1 = document.createTextNode("|")
split_ele_1.appendChild(split_1)
from_ele.appendChild(split_ele_1)
// train_from
const train_from_ele = document.createElement('p')
const train_from = document.createTextNode(`${t_from}`)
train_from_ele.appendChild(train_from)
from_ele.appendChild(train_from_ele)
// split
const split_ele_2 = document.createElement('p')
split_ele_2.classList.add("split")
const split_2 = document.createTextNode("|")
split_ele_2.appendChild(split_2)
from_ele.appendChild(split_ele_2)
// start date
const start_date_ele = document.createElement('p')
const train_start_date = document.createTextNode(`${st_date}`)
start_date_ele.appendChild(train_start_date)
from_ele.appendChild(start_date_ele)
// add from div to bottom
bottom.appendChild(from_ele)
// duration
const duration_ele = document.createElement('p')
duration_ele.classList.add('duration')
const train_duration = document.createTextNode(`----- ${dur} -----`)
duration_ele.appendChild(train_duration)
bottom.appendChild(duration_ele)
// to div
const to_ele = document.createElement('div')
to_ele.classList.add("to")
// end time
const end_time_ele = document.createElement('p')
const train_end_time = document.createTextNode(`${end_time}`)
end_time_ele.appendChild(train_end_time)
to_ele.appendChild(end_time_ele)
// split
const split_ele_3 = document.createElement('p')
split_ele_3.classList.add("split")
const split_3 = document.createTextNode("|")
split_ele_3.appendChild(split_3)
to_ele.appendChild(split_ele_3)
// train_to
const train_to_ele = document.createElement('p')
const train_to = document.createTextNode(`${t_to}`)
train_to_ele.appendChild(train_to)
to_ele.appendChild(train_to_ele)
// split
const split_ele_4 = document.createElement('p')
split_ele_4.classList.add("split")
const split_4 = document.createTextNode("|")
split_ele_4.appendChild(split_4)
to_ele.appendChild(split_ele_4)
// end date
const end_date_ele = document.createElement('p')
const train_end_date = document.createTextNode(`${end_date}`)
end_date_ele.appendChild(train_end_date)
to_ele.appendChild(end_date_ele)
// add to div to bottom
bottom.appendChild(to_ele)
// append BOTTOM to TRAIN
train.appendChild(bottom)
// seats
const seats_ele = document.createElement('div')
seats_ele.classList.add('seats')
seats_ele.setAttribute('id','seats')
// append SEATS to TRAIN
train.appendChild(seats_ele)
// Adding each seatype to train
Object.keys(seats).forEach(key => {
// seat div
const seat_ele = document.createElement('div')
seat_ele.classList.add("seat")
seat_ele.setAttribute('id',`${key}`)
// seat type
const seat_type_ele = document.createElement('p')
seat_type_ele.innerText=`${key}`;
seat_type_ele.classList.add('seat-type')
seat_ele.appendChild(seat_type_ele)
// seat avail
const seat_avail_ele = document.createElement('p')
seat_avail_ele.innerText=`Available - ${seats[key][0]}`;
seat_avail_ele.classList.add('seat-avail')
seat_ele.appendChild(seat_avail_ele)
// For each TRAIN remove : SeatColor, DisableBookBtn, TicketFare
seat_ele.addEventListener('click',(e)=>
{
// console.log(e.target,id)
// disable all other book-btn
disable_all_book_btns()
// remove all tic fare
remove_all_tic_fare()
// color the seat-avail element
if (e.target.classList.contains('seat-avail') ) {
if(e.target.classList.contains("color") ){
e.target.classList.remove("color");
document.getElementById(`${id}`).classList.add("disabled");
// removing seat-types corresponding fare amt
tic_fare_ele.innerHTML=``
// clear user selection from local storage
clear_user_selection()
}
else{
e.target.classList.add("color");
document.getElementById(`${id}`).classList.remove("disabled");
// setting seat-types corresponding fare amt
tic_fare_ele.innerHTML=`${seats[`${key}`][1]} Rs/-`
// add user selection to local storage
add_user_selection(name,id, key,seats[key][1])
}
}
// color the corresponding seat-avail element of seat-type element
else{
if(e.target.nextElementSibling.classList.contains("color") ){
e.target.nextElementSibling.classList.remove("color");
document.getElementById(`${id}`).classList.add("disabled");
// removing seat-types corresponding fare amt
tic_fare_ele.innerHTML=``
// clear user selection from local storage
clear_user_selection()
}
else{
e.target.nextElementSibling.classList.add("color");
document.getElementById(`${id}`).classList.remove("disabled");
// setting seat-types corresponding fare amt
tic_fare_ele.innerHTML=`${seats[`${key}`][1]} Rs/-`
// add user selection to local storage
add_user_selection(name,id, key,seats[key][1])
}
}
// remove all seat color except self
remove_siblings_seat_color(e)
})
// add to seats
seats_ele.appendChild(seat_ele)
})
// book now
const book_ele = document.createElement('div')
book_ele.classList.add("book")
// book-btn
const book_btn = document.createElement('a')
var linkText = document.createTextNode("Book Now");
book_btn.appendChild(linkText);
book_btn.setAttribute('id',`${id}`)
book_btn.title = "Book Now"
book_btn.classList.add('book-btn')
book_btn.classList.add('disabled')
book_btn.title = "Book now";
book_ele.appendChild(book_btn)
// tic-fare-ele
const tic_fare_ele = document.createElement('p')
tic_fare_ele.classList.add('tic_price')
tic_fare_ele.setAttribute('id','fare')
book_ele.appendChild(tic_fare_ele);
book_ele.addEventListener('click', handleClick)
train.appendChild(book_ele)
// console.log(train)
// ADDING TRAIN TO TRAINS
var trains = document.getElementById('trains')
// console.log(trains)
trains.appendChild(train)
}
const handleClick = () => {
console.log("Present sir")
window.location.href = 'details.html'
}
const remove_siblings_seat_color = (e) => {
const CousinSeats=[...document.querySelectorAll(".seat-avail")];
for(var Seat of CousinSeats)
{
// console.log(Seat == e.target)
if (Seat == e.target || Seat == e.target.nextElementSibling ){
continue
}
if( Seat.classList.contains("color")){
Seat.classList.remove("color");
}
}
}
const disable_all_book_btns = () => {
const CousinBook = [...document.querySelectorAll(".book-btn")]
for( var Btn of CousinBook)
{
Btn.classList.add("disabled")
}
}
const remove_all_tic_fare = () => {
const tic_fares = [...document.querySelectorAll(".tic_price")]
for ( var tic_fare of tic_fares)
{
tic_fare.innerHTML=``
}
}
const clear_user_selection = () => {
// localStorage.setItem("SelectedTrainName",null)
// localStorage.setItem("SelectedTrainId",null)
// localStorage.setItem("SelectedSeatType",null)
// localStorage.setItem("SelectedSeatFare",null)
}
const add_user_selection = (train_name,train_id,selected_seat_type,selected_seat_fare) => {
console.log('NAME: ',train_name)
console.log('ID: ',train_id)
console.log('SEAT-TYPE: ',selected_seat_type)
console.log('SEAT-FARE: ',selected_seat_fare)
// localStorage.setItem("SelectedTrainName",train_name)
// localStorage.setItem("SelectedTrainId",train_id)
// localStorage.setItem("SelectedSeatType",selected_seat_type)
// localStorage.setItem("SelectedSeatFare",selected_seat_fare)
// loop to fetch which train object is clicked
const trains = JSON.parse(localStorage.getItem('trainDetails'))
const train = trains.filter((train) => train.trainid = train_id)[0]
console.log(train)
const trainDetails = {
trainid: train_id,
trainname: train_name,
trainclass: selected_seat_type,
ticketFare: selected_seat_fare,
type_id: train.type_id
}
// Get the existing ticket details and update it with passenger details
const details = JSON.parse(localStorage.getItem('ticketDetails'))
const ticketDetails = JSON.stringify({...details, trainDetails})
localStorage.setItem('ticketDetails', ticketDetails)
}
const createTrains = () => {
// RESULT DATA
// const train_details = JSON.parse(localStorage.getItem('train_result'))
// console.log(train_details)
// dummy data
const trainDetails = [
{
"class": {
"AC1 Tier": [200,150],
"AC2 Tier": [200,250],
"AC3 Tier": [200,350]
},
"duration": "7:40",
"enddate": "2023-01-20",
"endtime": "16:08:02",
"startdate": "2023-01-15",
"starttime": "09:04:00",
"trainfrom": "CBE",
"trainid": 1,
"trainname": "KOVAI EXPRESS",
"trainto": "CHENNAI"
},
{
"class": {
"AC1 Tier": [200,150]
},
"duration": "7:40",
"enddate": "2023-01-20",
"endtime": "16:08:02",
"startdate": "2023-01-15",
"starttime": "09:04:00",
"trainfrom": "CBE",
"trainid": 2,
"trainname": "CHENNAI EXPRESS",
"trainto": "CHENNAI"
}
]
const train_details = JSON.parse(localStorage.getItem('trainDetails'))
// Storing type id which needs to be sent to ther server whille booking ticket
console.log(train_details)
// localStorage.setItem("SelectedTrainName",null)
// localStorage.setItem("SelectedTrainId",null)
// localStorage.setItem("SelectedSeatType",null)
// localStorage.setItem("SelectedSeatFare",null)
var trains = document.createElement('div')
trains.classList.add('trains')
trains.setAttribute('id','trains')
document.body.appendChild(trains)
// console.log(trains)
var no_of_trains = train_details.length
for (let i = 0; i < no_of_trains; i++) {
CreateTrainCard(train_details[i])
}
}
window.onload = () => {
//write your code here
createTrains()
}