-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
259 lines (219 loc) · 9.69 KB
/
app.js
File metadata and controls
259 lines (219 loc) · 9.69 KB
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
const { API_BASE_URL } = require('./api/config');
document.addEventListener('DOMContentLoaded', function() {
console.log('Flight Scanner app initialized');
// Constants
// Import API configuration from config file
const ENDPOINTS = {
airports: '/airports/search', // Match your server route
flights: '/flights'
};
// DOM Elements
const fromInput = document.getElementById('from');
const toInput = document.getElementById('to');
const searchBtn = document.getElementById('search-btn');
const resultsSection = document.getElementById('results-section');
// Setup autocomplete for airport inputs
if (fromInput) setupAirportAutocomplete(fromInput);
if (toInput) setupAirportAutocomplete(toInput);
// Setup search button handler
if (searchBtn) {
searchBtn.addEventListener('click', handleFlightSearch);
}
// Airport autocomplete setup
// In app.js's setupAirportAutocomplete function, add this:
async function searchAirports(query) {
try {
console.log(`Attempting to search airports with query: "${query}"`);
console.log(`Requesting: ${API_BASE_URL}${ENDPOINTS.airports}?query=${encodeURIComponent(query)}`);
const response = await fetch(`${API_BASE_URL}${ENDPOINTS.airports}?query=${encodeURIComponent(query)}`);
console.log('Response status:', response.status);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json();
console.log('Airport search results:', data);
return data;
} catch (error) {
console.error('Error in airport search:', error);
throw error;
}
}
function setupAirportAutocomplete(input) {
console.log(`Setting up autocomplete for ${input.id}`);
// Create results container
let resultsContainer = input.parentNode.querySelector('.autocomplete-results');
if (!resultsContainer) {
resultsContainer = document.createElement('div');
resultsContainer.className = 'autocomplete-results';
input.parentNode.appendChild(resultsContainer);
}
// Add input event with debounce
let timeout = null;
input.addEventListener('input', function() {
clearTimeout(timeout);
const query = this.value.trim();
// Clear previous results if query is empty
if (!query) {
resultsContainer.innerHTML = '';
resultsContainer.style.display = 'none';
return;
}
// Set a small delay to prevent too many requests
timeout = setTimeout(async function() {
try {
console.log(`Searching airports with query: "${query}"`);
// Make API request to airport search endpoint
const response = await fetch(`${API_BASE_URL}/airports/search?query=${encodeURIComponent(query)}`);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json();
console.log('Airport search results:', data);
// Display results if found
if (data.places && data.places.length > 0) {
displayAirportResults(data.places, resultsContainer, input);
} else {
resultsContainer.innerHTML = '<div class="no-results">No airports found</div>';
resultsContainer.style.display = 'block';
}
} catch (error) {
console.error('Error searching airports:', error);
resultsContainer.innerHTML = '<div class="no-results error">Error searching airports</div>';
resultsContainer.style.display = 'block';
}
}, 300);
});
// Close autocomplete when clicking outside
document.addEventListener('click', function(e) {
if (!input.contains(e.target) && !resultsContainer.contains(e.target)) {
resultsContainer.style.display = 'none';
}
});
}
// Display airport search results
function displayAirportResults(airports, container, input) {
container.innerHTML = '';
airports.forEach(airport => {
const item = document.createElement('div');
item.className = 'autocomplete-item';
item.innerHTML = `
<div class="airport-code">${airport.iataCode}</div>
<div class="airport-details">
<div class="airport-name">${airport.name}</div>
<div class="airport-location">${airport.cityName}, ${airport.countryName}</div>
</div>
`;
item.addEventListener('click', function() {
input.value = `${airport.cityName} (${airport.iataCode})`;
input.dataset.iataCode = airport.iataCode;
container.style.display = 'none';
});
container.appendChild(item);
});
container.style.display = 'block';
}
// Handle flight search
async function handleFlightSearch(e) {
if (e) e.preventDefault();
console.log('Flight search initiated');
// Validation
if (!fromInput || !fromInput.dataset.iataCode) {
alert('Please select a departure airport from the suggestions');
return;
}
if (!toInput || !toInput.dataset.iataCode) {
alert('Please select a destination airport from the suggestions');
return;
}
const departureDate = document.getElementById('departure-date');
if (!departureDate || !departureDate.value) {
alert('Please select a departure date');
return;
}
// Show loading state
if (resultsSection) {
resultsSection.innerHTML = '<div class="loading">Searching for flights...</div>';
}
try {
const searchData = {
from: fromInput.dataset.iataCode,
to: toInput.dataset.iataCode,
date: departureDate.value,
returnDate: document.getElementById('return-date')?.value || null,
passengers: parseInt(document.getElementById('passengers')?.value) || 1
};
console.log('Sending flight search request:', searchData);
const response = await fetch(`${API_BASE_URL}/flights/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(searchData)
});
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json();
console.log('Flight search results:', data);
// Display results
if (resultsSection) {
displayFlightResults(data, resultsSection);
}
} catch (error) {
console.error('Error searching flights:', error);
if (resultsSection) {
resultsSection.innerHTML = `
<div class="error-message">
<h3>Error searching flights</h3>
<p>${error.message}</p>
</div>
`;
}
}
}
// Display flight results
function displayFlightResults(data, container) {
if (!data.flights || data.flights.length === 0) {
container.innerHTML = `
<div class="no-flights">
<h3>No flights found</h3>
<p>Try different search criteria or dates</p>
</div>
`;
return;
}
let html = '<div class="flight-results">';
data.flights.forEach(flight => {
html += `
<div class="flight-card">
<div class="flight-header">
<div class="airline">${flight.airline}</div>
<div class="flight-number">${flight.id}</div>
</div>
<div class="flight-body">
<div class="flight-time">
<div class="departure">
<div class="time">${flight.departureTime}</div>
<div class="airport">${flight.from}</div>
</div>
<div class="flight-duration">
<div class="duration-line"></div>
<div class="duration-time">${flight.duration}</div>
</div>
<div class="arrival">
<div class="time">${flight.arrivalTime}</div>
<div class="airport">${flight.to}</div>
</div>
</div>
</div>
<div class="flight-footer">
<div class="price">₹${flight.price}</div>
<button class="select-btn">Select</button>
</div>
</div>
`;
});
html += '</div>';
container.innerHTML = html;
}
});