-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: create table logic moved to Classes
- Loading branch information
1 parent
b71afda
commit ca448c1
Showing
10 changed files
with
233 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { fetchArrFlights } from "../js/api"; | ||
import { heroDiv, template } from "../js/const"; | ||
import FlightsList from "./FlightsList"; | ||
|
||
export default class ArrivalFlightsList extends FlightsList { | ||
async createList(airportCode) { | ||
this.showClearButton(); | ||
heroDiv.classList.add("hidden"); | ||
|
||
const flights = await fetchArrFlights(airportCode); | ||
const validFlights = await this.getValidFlights(flights); | ||
const sortedFlights = this.sortFlights(validFlights); | ||
|
||
await this.fetchAirlines(); | ||
|
||
const table = document.importNode(template.content, true); | ||
const tableDiv = table.querySelector("div"); | ||
tableDiv.id = "arr-table"; | ||
const title = table.querySelector("#table-title"); | ||
title.textContent = "arrivals"; | ||
const tableBody = table.querySelector("#table-content"); | ||
|
||
for (let flight of sortedFlights) { | ||
if (this.flightCounter >= 50) { | ||
break; | ||
} | ||
const row = this.createFlightRow(flight); | ||
tableBody.appendChild(row); | ||
this.flightCounter++; | ||
} | ||
|
||
const tableContainer = document.querySelector("#arr-container"); | ||
tableContainer.appendChild(table); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { fetchDepFlights } from "../js/api"; | ||
import { heroDiv, template } from "../js/const"; | ||
import FlightsList from "./FlightsList"; | ||
|
||
export default class DepartureFlightsList extends FlightsList { | ||
async createList(airportCode) { | ||
heroDiv.classList.add("hidden"); | ||
|
||
const flights = await fetchDepFlights(airportCode); | ||
const validFlights = await this.getValidFlights(flights); | ||
const sortedFlights = this.sortFlights(validFlights); | ||
|
||
await this.fetchAirlines(); | ||
|
||
const table = document.importNode(template.content, true); | ||
const tableDiv = table.querySelector("div"); | ||
tableDiv.id = "dep-table"; | ||
const title = table.querySelector("#table-title"); | ||
title.textContent = "departure"; | ||
const tableBody = table.querySelector("#table-content"); | ||
|
||
for (let flight of sortedFlights) { | ||
if (this.flightCounter >= 50) { | ||
break; | ||
} | ||
const row = this.createFlightRow(flight); | ||
tableBody.appendChild(row); | ||
this.flightCounter++; | ||
} | ||
|
||
const tableContainer = document.querySelector("#dep-container"); | ||
tableContainer.appendChild(table); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { fetchAirlines } from "../js/api"; | ||
import { resFields } from "../js/const"; | ||
import { parseTime } from "../js/helpers"; | ||
|
||
export default class FlightsList { | ||
constructor() { | ||
this.flightCounter = 0; | ||
this.airlinesMap = {}; | ||
} | ||
|
||
async fetchAirlines() { | ||
const allAirlines = await fetchAirlines(); | ||
allAirlines.forEach((airline) => { | ||
this.airlinesMap[airline.iata_code] = airline.name; | ||
}); | ||
} | ||
|
||
async getValidFlights(flights) { | ||
return flights.filter((flight) => { | ||
return resFields.every( | ||
(field) => flight[field] !== null && flight[field] !== undefined | ||
); | ||
}); | ||
} | ||
|
||
showClearButton() { | ||
const clearButton = document.querySelector("#clear-button"); | ||
if (clearButton) { | ||
clearButton.classList.remove("hidden"); | ||
} | ||
} | ||
|
||
sortFlights(flights) { | ||
return flights.sort((a, b) => { | ||
const arrivalTimeA = new Date(a.arr_time).getTime(); | ||
const arrivalTimeB = new Date(b.arr_time).getTime(); | ||
return arrivalTimeA - arrivalTimeB; | ||
}); | ||
} | ||
|
||
createFlightRow(flight) { | ||
const row = document.createElement("tr"); | ||
const timeData = document.createElement("td"); | ||
|
||
if (flight.arr_delayed) { | ||
const delayedTimeSpan = document.createElement("span"); | ||
delayedTimeSpan.className = "text-red-600 line-through"; | ||
delayedTimeSpan.textContent = parseTime(new Date(flight.arr_time)); | ||
timeData.appendChild(delayedTimeSpan); | ||
|
||
const estimatedTimeText = document.createTextNode( | ||
` ${parseTime(new Date(flight.arr_estimated))}` | ||
); | ||
timeData.appendChild(estimatedTimeText); | ||
} else { | ||
timeData.textContent = parseTime(new Date(flight.arr_time)); | ||
} | ||
row.appendChild(timeData); | ||
|
||
const airlineData = document.createElement("td"); | ||
const airlineName = this.airlinesMap[flight.airline_iata]; | ||
airlineData.textContent = airlineName; | ||
row.appendChild(airlineData); | ||
|
||
const flightDataElem = document.createElement("td"); | ||
flightDataElem.textContent = flight.flight_iata; | ||
row.appendChild(flightDataElem); | ||
|
||
const originData = document.createElement("td"); | ||
originData.textContent = flight.dep_iata; | ||
row.appendChild(originData); | ||
|
||
const statusData = document.createElement("td"); | ||
statusData.textContent = flight.status; | ||
row.appendChild(statusData); | ||
|
||
return row; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.