Skip to content

Instantly share code, notes, and snippets.

@vtambourine
Last active September 28, 2017 15:44
Show Gist options
  • Save vtambourine/d6149b221fc5dc4431aed0e848649fff to your computer and use it in GitHub Desktop.
Save vtambourine/d6149b221fc5dc4431aed0e848649fff to your computer and use it in GitHub Desktop.
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 rowElement = $(`
<tr>
<td>${flightDetails.flightName}</td>
<td>${flightDetails.route.destinations[0]}</td>
<td>${flightDetails.scheduleTime}</td>
<td>${flightDetails.gate}</td>
<td>${flightDetails.publicFlightState.flightStates[0]}</td>
</tr>
`);
this.tableBody.append(rowElement);
}
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