This commit is contained in:
2024-09-03 11:40:09 -04:00
parent 32a0ecc1e6
commit cc74be72be
17 changed files with 47 additions and 62 deletions

View File

@@ -1,13 +1,13 @@
import { Airport, AirportOrderField, Bounds, GetAirportResponse, GetAirportsResponse } from './airport.types';
import { Airport, AirportOrderField, Bounds, GetAirportsResponse } from './airport.types';
import { getRequest, deleteRequest, postRequest, putRequest } from '.';
interface GetAirportProps {
icao: string;
}
export async function getAirport({ icao }: GetAirportProps): Promise<GetAirportResponse> {
export async function getAirport({ icao }: GetAirportProps): Promise<Airport> {
const response = await getRequest(`airports/${icao}`);
return response?.json() || { data: undefined };
return response?.json() || {};
}
interface GetAirportsProps {

View File

@@ -86,11 +86,6 @@ export interface Frequency {
frequency_mhz: number;
}
export interface GetAirportResponse {
data: Airport;
meta: Metadata;
}
export interface GetAirportsResponse {
data: Airport[];
meta: Metadata;

View File

@@ -75,6 +75,5 @@ export async function deleteRequest(endpoint: string): Promise<Response> {
export interface Metadata {
limit: number;
page: number;
pages: number;
total: number;
}

View File

@@ -1,15 +1,11 @@
import { Metar } from './metar.types';
import { getRequest } from '.';
interface GetMetarsResponse {
data: Metar[];
}
export async function getMetars(icaos: string[]): Promise<GetMetarsResponse> {
export async function getMetars(icaos: string[]): Promise<Metar[]> {
if (icaos.length == 0) {
return { data: [] };
return [];
}
const stationICAOs: string = icaos.map((icao) => icao).join(',');
const response = await getRequest(`metars`, { icaos: stationICAOs });
return response?.json() || { data: [] };
return response?.json() || [];
}

View File

@@ -13,9 +13,9 @@ export default function Page({ params }: { params: { icao: string } }) {
useEffect(() => {
async function loadAirport() {
const { data: airportData } = await getAirport({ icao: params.icao });
const airportData = await getAirport({ icao: params.icao });
setAirport(airportData);
const { data: metarData } = await getMetars([airportData.icao]);
const metarData = await getMetars([airportData.icao]);
if (metarData.length > 0) {
setMetar(metarData[0]);
}

View File

@@ -57,7 +57,7 @@ export default function MapTiles() {
page: 1
});
const airports = airportData.filter((airport) => airport.has_metar);
const { data: metars } = await getMetars(airports.map((a) => a.icao));
const metars = await getMetars(airports.map((a) => a.icao));
metars.forEach((metar) => {
airportData.forEach((airport) => {
if (metar.station_id == airport.icao) {