From 90e958af74d91e8beed50ebe400449cbba8cdad3 Mon Sep 17 00:00:00 2001 From: zemion Date: Fri, 26 Sep 2025 11:22:45 +0200 Subject: [PATCH] basic flight calc working --- public/example.html | 102 +++++++++++++++++++++++++++--------- public/js/calco2lato.js | 4 +- src/Calco2latoApiClient.php | 2 +- 3 files changed, 80 insertions(+), 28 deletions(-) diff --git a/public/example.html b/public/example.html index ca01f6d..bdc7382 100644 --- a/public/example.html +++ b/public/example.html @@ -1,30 +1,82 @@ -
- - -
-
+ + + + calco2la.to API client example + + + +
+

Search for airports

+
+ + +
+
+
    +
    +

    Calculate flight

    +
    + + + + + +
    +
    +
    
    +      
    +
    - + const flightform = document.querySelector('#flight-search-form'); + const flightresult = document.querySelector('#flight-output'); + + 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 = `${err.message}`; + } + }); + + + \ No newline at end of file diff --git a/public/js/calco2lato.js b/public/js/calco2lato.js index 9ae5299..1a7d868 100644 --- a/public/js/calco2lato.js +++ b/public/js/calco2lato.js @@ -17,8 +17,8 @@ export class Flight { } summary() { const s = []; - if (this.origin?.iata) s.push(this.origin.iata); - if (this.destination?.iata) s.push(this.destination.iata); + if (this.departureAirport?.iata) s.push(this.departureAirport.iata); + if (this.arrivalAirport?.iata) s.push(this.arrivalAirport.iata); const route = s.length ? s.join(' → ') : 'Flight'; const co2 = this.emissions?.co2_total ?? this.co2 ?? null; return co2 != null ? `${route}: ${co2} kg CO₂e` : route; diff --git a/src/Calco2latoApiClient.php b/src/Calco2latoApiClient.php index 978f28b..c60bf23 100644 --- a/src/Calco2latoApiClient.php +++ b/src/Calco2latoApiClient.php @@ -86,6 +86,6 @@ final class Calco2latoApiClient */ public function flightEstimate(array $params): array { - return $this->request('POST', '/latest/transport/flights', [], $params); + return $this->request('POST', '/latest/transport/flight', [], $params); } }