Skip to content

Instantly share code, notes, and snippets.

@vtambourine
Created September 28, 2017 15:55
Show Gist options
  • Save vtambourine/a3d00ce9b21f04446894601e1440b00d to your computer and use it in GitHub Desktop.
Save vtambourine/a3d00ce9b21f04446894601e1440b00d to your computer and use it in GitHub Desktop.
class Flight {
constructor(flight) {
this.name = flight.flightName;
this.time = flight.scheduleTime.split(':').splice(0, 2).join(':');
this.gate = flight.gate;
this.route = flight.route.destinations.join(' - ');
}
render() {
return $(`
<tr>
<td>${this.name}</td>
<td>${this.route}</td>
<td>${this.time}</td>
<td>${this.gate}</td>
<td>${' '}</td>
</tr>
`);
}
}
class FlightTable {
constructor(domNode, options) {
this.tableNode = $(domNode);
this.tableBody = this.tableNode.find('tbody');
this.direction = options.direction;
this.page = 0;
var now = Date.now();
var startTime = new Date(now - 20 * 60 * 1000);
this.startTimeFormatted = startTime.getHours() + ':' + startTime.getMinutes();
this.endTime = new Date(startTime.getTime() + 60 * 60 * 1000);
this.callLimit = 10;
}
updateFlight(flightDetails) {
// Ignore if current flight is not main in codeshares
if (flightDetails.mainFlight !== flightDetails.flightName) {
return;
}
// Ignore if currect flight is not passenger flight
if (flightDetails.serviceType !== 'J'
&& flightDetails.serviceType !== 'C') {
return;
}
var flight = new Flight(flightDetails);
this.tableBody.append(flight.render());
}
fetchFlights() {
$.ajax({
url: 'https://api.schiphol.nl/public-flights/flights',
headers: {
'ResourceVersion': 'v3',
},
dataType: 'json',
data: {
app_id: window.B.appId,
app_key: window.B.appKey,
flightdirection: this.direction,
includedelays: true,
scheduletime: this.startTimeFormatted,
sort: '+scheduletime',
page: this.page
},
context: this,
success: function (data, status, xhr) {
data.flights.forEach((flights) => {
this.updateFlight(flights);
});
var lastFlight = data.flights[data.flights.length - 1];
var lastFlightTime = new Date(`${lastFlight.scheduleDate} ${lastFlight.scheduleTime}`);
if (this.endTime > lastFlightTime && --this.callLimit) {
this.page++;
this.fetchFlights();
}
}
});
}
}
const departuresFlightTable = new FlightTable(
document.getElementById('flight-departures-table'),
{ direction: 'D' }
);
departuresFlightTable.fetchFlights();
const arrivalsFlightTable = new FlightTable(
document.getElementById('flight-arrivals-table'),
{ direction: 'A' }
);
arrivalsFlightTable.fetchFlights();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment