basic flight calc working
This commit is contained in:
@@ -1,22 +1,61 @@
|
|||||||
<form id="airport-search-form">
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>calco2la.to API client example</title>
|
||||||
|
<style>
|
||||||
|
#wrapper {
|
||||||
|
max-width: 40em;
|
||||||
|
height: 100%;
|
||||||
|
margin: 0em auto;
|
||||||
|
}
|
||||||
|
#airport-results, #flight-results {
|
||||||
|
min-height: 5em;
|
||||||
|
border: 1px solid grey;
|
||||||
|
margin: 1em auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="wrapper">
|
||||||
|
<h1>Search for airports</h1>
|
||||||
|
<form id="airport-search-form">
|
||||||
<input type="text" id="q" name="q" />
|
<input type="text" id="q" name="q" />
|
||||||
<button type="submit" name="Los" title="Los">Los</button>
|
<button type="submit" name="Los" title="Los">Search ...</button>
|
||||||
</form>
|
</form>
|
||||||
<div id="airport-results"></div>
|
<div id="airport-results">
|
||||||
|
<ul id="airport-list"></ul>
|
||||||
|
</div>
|
||||||
|
<h1>Calculate flight</h1>
|
||||||
|
<form id="flight-search-form">
|
||||||
|
<input type="text" id="departure" name="departure" />
|
||||||
|
<input type="text" id="arrival" name="arrival" />
|
||||||
|
<input type="date" id="departuredate" name="departuredate" value="2025-09-26" min="2017-01-01" max="2025-12-31" />
|
||||||
|
<select id="cabinclass" name="cabinclass">
|
||||||
|
<option value="economy">Economy</option>
|
||||||
|
<option value="premium_economy">Business Economy</option>
|
||||||
|
<option value="business">Business</option>
|
||||||
|
<option value="first">First</option>
|
||||||
|
</select>
|
||||||
|
<button type="submit" name="Los" title="Los">Calculate ...</button>
|
||||||
|
</form>
|
||||||
|
<div id="flight-results">
|
||||||
|
<pre id="flight-output"></pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import { Calco2latoClient } from '/js/calco2lato.js';
|
import { Calco2latoClient } from '/js/calco2lato.js';
|
||||||
|
|
||||||
const api = new Calco2latoClient('/api-proxy.php');
|
const api = new Calco2latoClient('/api-proxy.php');
|
||||||
|
|
||||||
const form = document.querySelector('#airport-search-form');
|
const searchform = document.querySelector('#airport-search-form');
|
||||||
const list = document.querySelector('#airport-results');
|
const list = document.querySelector('#airport-list');
|
||||||
|
|
||||||
form.addEventListener('submit', async (e) => {
|
searchform.addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
list.innerHTML = 'Loading…';
|
list.innerHTML = 'Loading…';
|
||||||
try {
|
try {
|
||||||
const q = new FormData(form).get('q');
|
const q = new FormData(searchform).get('q');
|
||||||
const airports = await api.searchAirports(q, 10, 1);
|
const airports = await api.searchAirports(q, 10, 1);
|
||||||
list.innerHTML = airports.map(a => `<li>${a.display}</li>`).join('');
|
list.innerHTML = airports.map(a => `<li>${a.display}</li>`).join('');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -24,7 +63,20 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Example flight estimate:
|
const flightform = document.querySelector('#flight-search-form');
|
||||||
// const est = await api.estimateFlight({ origin: 'FRA', destination: 'LHR', pax: 1, cabin: 'economy' });
|
const flightresult = document.querySelector('#flight-output');
|
||||||
// console.log(est.summary());
|
|
||||||
</script>
|
flightform.addEventListener('submit', async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
flightresult.innerHTML = 'Loading…';
|
||||||
|
try {
|
||||||
|
const f = new FormData(flightform);
|
||||||
|
const flight = await api.estimateFlight({"flights": [{"departure": f.get('departure'), "arrival": f.get('arrival'), "passengerCount": 1, "travelClass": f.get('cabinclass'), "departureDate": f.get('departuredate')}]});
|
||||||
|
flightresult.innerHTML = JSON.stringify(flight, null, 2);
|
||||||
|
} catch (err) {
|
||||||
|
flightresult.innerHTML = `<span style="color:red">${err.message}</span>`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -17,8 +17,8 @@ export class Flight {
|
|||||||
}
|
}
|
||||||
summary() {
|
summary() {
|
||||||
const s = [];
|
const s = [];
|
||||||
if (this.origin?.iata) s.push(this.origin.iata);
|
if (this.departureAirport?.iata) s.push(this.departureAirport.iata);
|
||||||
if (this.destination?.iata) s.push(this.destination.iata);
|
if (this.arrivalAirport?.iata) s.push(this.arrivalAirport.iata);
|
||||||
const route = s.length ? s.join(' → ') : 'Flight';
|
const route = s.length ? s.join(' → ') : 'Flight';
|
||||||
const co2 = this.emissions?.co2_total ?? this.co2 ?? null;
|
const co2 = this.emissions?.co2_total ?? this.co2 ?? null;
|
||||||
return co2 != null ? `${route}: ${co2} kg CO₂e` : route;
|
return co2 != null ? `${route}: ${co2} kg CO₂e` : route;
|
||||||
|
|||||||
@@ -86,6 +86,6 @@ final class Calco2latoApiClient
|
|||||||
*/
|
*/
|
||||||
public function flightEstimate(array $params): array
|
public function flightEstimate(array $params): array
|
||||||
{
|
{
|
||||||
return $this->request('POST', '/latest/transport/flights', [], $params);
|
return $this->request('POST', '/latest/transport/flight', [], $params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user