-
Notifications
You must be signed in to change notification settings - Fork 4
/
flight.js
234 lines (182 loc) · 8.52 KB
/
flight.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
let userName = localStorage.getItem("userName") || "x";
console.log(userName);
if(userName=="x"){
window.location.href = "login-signup.html";
alert("Please login First!!!");
}
//<<<<<<<<<<<<<<<<<<<<<<User Login Validation>>>>>>>>>>>>>>>>>>>>>>
let today = new Date();
today.setDate(new Date().getDate());
let todayISO = today.toISOString().substr(0, 10);
// Set the default value of the date input field to today's date
document.getElementById("dep_date").defaultValue = todayISO;
// -----------------------------------------------------------------
let FlightData =[]
let FlightLS = JSON.parse(localStorage.getItem("flight")) || []
window.addEventListener("load",()=>{
fetchAndRenderFlights(1)
})
let mainDiv = document.getElementById("data-list-wrapper")
function fetchAndRenderFlights(){
fetch(`https://api-by-alisha-khan.onrender.com/flights`)
.then(res =>{
return res.json();
})
.then(data =>{
console.log(data);
FlightData = data;
showData(FlightData);
})
.catch(err => {console.log(err)})
}
function showData(data) {
mainDiv.innerHTML = null;
let cardlist = document.createElement("div");
cardlist.className = "cardlist";
data.forEach(element => {
let card = document.createElement("div");
card.className = "card";
let imageSection = document.createElement("div");
imageSection.className = "imageSection";
let flightNum = document.createElement("div");
flightNum.className = "flightNum";
flightNum.innerText = element.flight_number;
let airline = document.createElement("div");
airline.className = "airline";
airline.innerText = element.airline;
imageSection.append(flightNum, airline)
let infoSection = document.createElement("div");
infoSection.className="infoSection";
let upper = document.createElement("div");
upper.className = "infoUpper";
let dDiv = document.createElement("div");
dDiv.className = "cityDiv";
dDiv.innerText = `${element.from_city} (${element.from_code})`;
let aDiv = document.createElement("div");
aDiv.className = "cityDiv";
aDiv.innerText = `${element.to_city} (${element.to_code})`;
let midDiv = document.createElement("div");
midDiv.className = "midDiv";
let midTop = document.createElement("div");
midTop.className = "midTop";
midTop.innerText = `${element.duration}`;
let midBottom = document.createElement("div");
midBottom.className = "midBottom";
midBottom.innerText = `${element.departure} - ${element.arrival}`;
midDiv.append(midTop,midBottom);
upper.append(dDiv,midDiv,aDiv);
let lower = document.createElement("div");
lower.className = "infoLower";
let priceDiv = document.createElement("div");
priceDiv.className="priceDiv";
priceDiv.innerText = `Price: ₹ ${element.price}`;
let seatsDiv = document.createElement("div");
seatsDiv.className = "seatsDiv";
seatsDiv.innerText = `Seats: ${element.seats_available}`;
lower.append(priceDiv,seatsDiv)
infoSection.append(upper,lower);
let buttonSection = document.createElement("div");
buttonSection.className = "buttonSection";
let bookbtn = document.createElement("div")
bookbtn.className = "editBtn cardButton"
bookbtn.innerText = "Book"
buttonSection.append(bookbtn);
bookbtn.addEventListener("click",()=>{
//---------------------------------> add flightLS here
let depDate = document.getElementById("dep_date").value
let arrDate = document.getElementById("ret_date").value
console.log(depDate,arrDate);
FlightLS = []
FlightLS = ({name : element.airline,
price : element.price,
fromCity : element.from_city,
fromCode : element.from_code,
toCity : element.to_city,
toCode : element.to_code,
arrival : element.arrival,
departure : element.departure,
duration : element.duration,
flightNumber : element.flight_number,
depDate : depDate,
arrDate : arrDate
})
localStorage.setItem("flight",JSON.stringify(FlightLS));
//---------------------------------> add payment location here
// window.location.href =""
})
card.append(imageSection, infoSection, buttonSection);
cardlist.append(card);
});
mainDiv.append(cardlist);
}
//------------------------------------Functionality------------------------------------------
let directCheckbox = document.querySelector("#direct")
let oneStopCheckbox = document.querySelector("#onestop")
let twostopCheckbox = document.querySelector("#twostop")
directCheckbox.addEventListener("change",()=>{filterCheck()})
oneStopCheckbox.addEventListener("change",()=>{filterCheck()})
twostopCheckbox.addEventListener("change",()=>{filterCheck()})
let airIndiaCheckbox = document.querySelector("#air_india");
let spiceJetCheckbox = document.querySelector("#spice_jet");
let indiGoCheckbox = document.querySelector("#indi_go");
let vistaraCheckbox = document.querySelector("#vistara");
let goAirCheckbox = document.querySelector("#go_air");
airIndiaCheckbox.addEventListener("change",()=>{filterCheck()})
spiceJetCheckbox.addEventListener("change",()=>{filterCheck()})
indiGoCheckbox.addEventListener("change",()=>{filterCheck()})
vistaraCheckbox.addEventListener("change",()=>{filterCheck()})
goAirCheckbox.addEventListener("change",()=>{filterCheck()})
function filterCheck(){
console.log("filterData");
let filterData = FlightData.filter((ele)=>{
if((directCheckbox.checked && ele.layovers.length ==0) || (oneStopCheckbox.checked && ele.layovers.length ==1) || (twostopCheckbox.checked && ele.layovers.length ==2) ){
console.log(ele.id);
return true;
}
if (airIndiaCheckbox.checked && ele.airline == 'Air India' || spiceJetCheckbox.checked && ele.airline == 'SpiceJet' || indiGoCheckbox.checked && ele.airline =="IndiGo" || vistaraCheckbox.checked && ele.airline =="Vistara" || goAirCheckbox.checked && ele.airline =="GoAir") {
console.log(ele.id)
return true;
}
// if (ResortsCheckbox.checked && ele.category === 'Resort') {
// console.log(ele.id)
// return true;
// }
return false;
});
if (!airIndiaCheckbox.checked && !spiceJetCheckbox.checked && !indiGoCheckbox.checked && !vistaraCheckbox.checked && !goAirCheckbox.checked && !twostopCheckbox.checked && !oneStopCheckbox.checked &&
!directCheckbox.checked) {
fetchAndRenderFlights()
} else {
showData(filterData);
}
}
let searchFrom = document.querySelector(".searchContainer_input_from");
let searchTo = document.querySelector(".searchContainer_input_to");
let Searchbtn = document.querySelector(".searchbtn")
Searchbtn.addEventListener("click",()=>{
let filter ;
if(searchFrom == null && searchTo == null){
fetchAndRenderFlights()
}
else{
filter = FlightData.filter((ele)=>{
if(ele.from_city.toUpperCase().includes(searchFrom.value.toUpperCase()) || ele.from_code.toUpperCase().includes(searchFrom.value.toUpperCase())){
return true
}
if(ele.to_city.toUpperCase().includes(searchTo.value.toUpperCase()) || ele.to_code.toUpperCase().includes(searchTo.value.toUpperCase())){
return true
}
else{
return false
}
})
console.log(filter)
showData(filter);
}
})
let userLogOut = document.querySelector("#logOutButton");
userLogOut.addEventListener("click",function(){
localStorage.removeItem("userName");
localStorage.removeItem("userPass");
window.location.href = "login-signup.html";
})