Skip to content

Commit

Permalink
feat: add title with name of the airport
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Pedro Henrique committed Jan 23, 2023
1 parent 8f5edba commit 7527ca7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
8 changes: 5 additions & 3 deletions packages/app/src/components/FlightsTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import Flight from '@mach/common'
import { FC, useState } from 'react'
import { Airport } from '../../services/fetch-airports'
import { formatAirport } from '../../utils/format-airport'
import Button from '../Button'
import FlightModal from '../FlightModal'
import styles from './index.module.css'

type Props = {
items: Flight[]
items: Array<Flight & { departure: Airport, arrival: Airport }>
}

const FlightsTable: FC<Props> = ({ items }) => {
Expand All @@ -31,8 +33,8 @@ const FlightsTable: FC<Props> = ({ items }) => {
{items.map((flight, key) => (
<tr key={key}>
<td>{flight.callsign}</td>
<td>{flight.departureIcao}</td>
<td>{flight.arrivalIcao}</td>
<td><abbr title={formatAirport(flight.departure)}>{flight.departureIcao}</abbr></td>
<td><abbr title={formatAirport(flight.arrival)}>{flight.arrivalIcao}</abbr></td>
<td>{flight.estimatedOffBlockTime}</td>
<td>{flight.aircraft.icaoCode}</td>
<td className="grid justify-items-center">
Expand Down
7 changes: 3 additions & 4 deletions packages/app/src/components/SearchFlightsForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import { ChangeEventHandler, FC, useState } from 'react'
import { Airport } from '../../services/fetch-airports'
import { formatAirport } from '../../utils/format-airport'
import Button from '../Button'
import FormInput from '../FormInput'
import styles from './index.module.css'
import Select from 'react-select'
import { SelectInput } from '../SelectInput'
import styles from './index.module.css'

export type SearchFlightsFormFields = { departureIcao: string; arrivalIcao: string, company: string }

Expand All @@ -32,7 +31,7 @@ const SearchFlightsForm: FC<Props> = ({ companies, airports }) => {
}

const airportsOptions = airports.map(airport => {
return { value: airport.AeroCode, label: `${airport.AeroCode} - ${airport.name} - ${airport.city}` }
return { value: airport.AeroCode, label: formatAirport(airport) }
})

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/services/fetch-airports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const airportSchema = z.object({

export type Airport = z.infer<typeof airportSchema>;

async function fetchAirportsData(icaoCodes: string[]) {
export async function fetchAirportsData(icaoCodes: string[]) {
const endpoint = new URL("https://aisweb.decea.mil.br/api");
endpoint.searchParams.set("apiKey", environment.AISWEB_API_KEY);
endpoint.searchParams.set("apiPass", environment.AISWEB_API_PASSWORD);
Expand Down
24 changes: 22 additions & 2 deletions packages/app/src/services/fetch-flights.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FlightModel } from "@mach/database";
import { Op } from "sequelize";
import z from "zod";
import { fetchAirportsData } from "./fetch-airports";

const schema = z.object({
departureIcao: z
Expand All @@ -21,11 +22,11 @@ const schema = z.object({
.optional(),
});

export function fetchFlights(searchParams: Record<string, unknown>) {
export async function fetchFlights(searchParams: Record<string, unknown>) {
const today = new Date();

const where = schema.parse(searchParams);
return FlightModel.findAll({
const flights = await FlightModel.findAll({
where: {
...(where.departureIcao && { departureIcao: where.departureIcao }),
...(where.arrivalIcao && { arrivalIcao: where.arrivalIcao }),
Expand All @@ -44,4 +45,23 @@ export function fetchFlights(searchParams: Record<string, unknown>) {
},
raw: true,
});

const icaos = flights.flatMap((flight) => [
flight.departureIcao,
flight.arrivalIcao,
]);

const airports = await fetchAirportsData(icaos);

return flights.map((flight) => {
return {
...flight,
departure: airports.find(
({ AeroCode }) => AeroCode === flight.departureIcao
)!,
arrival: airports.find(
({ AeroCode }) => AeroCode === flight.arrivalIcao
)!,
};
});
}
8 changes: 8 additions & 0 deletions packages/app/src/utils/format-airport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Airport } from "../services/fetch-airports";

export function formatAirport(airport: Airport) {
if (airport.name === airport.city) {
return `${airport.AeroCode} - ${airport.name}`;
}
return `${airport.AeroCode} - ${airport.name} - ${airport.city}`;
}

1 comment on commit 7527ca7

@vercel
Copy link

@vercel vercel bot commented on 7527ca7 Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mach – ./

mach-jpedroh.vercel.app
mach.jpedroh.dev
mach-git-master-jpedroh.vercel.app
mach-five.vercel.app

Please sign in to comment.