Skip to content

Commit

Permalink
feat: add select for cycle in homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Sep 17, 2023
1 parent 5ee0aa8 commit 4f88509
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/app/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SearchFlightsForm from '../src/components/SearchFlightsForm'
import { fetchCompanies } from '../src/services/fetch-companies'
import { fetchAircraftIcaoCodes } from '../src/services/fetch-aircraft-icao-codes'
import { fetchAirports } from '../src/services/fetch-airports'
import { fetchCycles } from '../src/services/fetch-cycles'

export const revalidate = 3600

Expand All @@ -14,6 +15,7 @@ export const metadata = {
}

export default async function Page() {
const cycles = await fetchCycles()
const companies = await fetchCompanies()
const airports = await fetchAirports()
const aircraftIcaoCodes = await fetchAircraftIcaoCodes()
Expand All @@ -22,6 +24,7 @@ export default async function Page() {
<GeneralLayout>
<Lead>To begin, fill at least one of the following fields.</Lead>
<SearchFlightsForm
cycles={cycles}
airports={airports}
companies={companies}
aircraftIcaoCodes={aircraftIcaoCodes}
Expand Down
20 changes: 20 additions & 0 deletions packages/app/src/components/SearchFlightsForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ import { SelectInput } from '../SelectInput'
import styles from './index.module.css'

export type SearchFlightsFormFields = {
cycle: string
departureIcao: string
arrivalIcao: string
company: string
aircraftIcaoCode: string
}

type Props = {
cycles: string[]
companies: string[]
aircraftIcaoCodes: string[]
airports: Airport[]
}

const SearchFlightsForm: FC<Props> = ({
cycles,
companies,
aircraftIcaoCodes,
airports,
}) => {
const [form, setForm] = useState<SearchFlightsFormFields>({
cycle: cycles[0],
arrivalIcao: '',
departureIcao: '',
company: '',
Expand All @@ -51,8 +55,24 @@ const SearchFlightsForm: FC<Props> = ({
return { value: airport.AeroCode, label: formatAirport(airport) }
})

const cyclesOptions = cycles.map((cycle) => {
return { value: cycle, label: cycle }
})

return (
<form className={styles.container} action={'/search'}>
<div>
<label htmlFor="cycle">Cycle</label>
<SelectInput
options={cyclesOptions}
name="cycle"
defaultValue={cyclesOptions[0]}
onChange={(cycle) =>
setForm((form) => ({ ...form, cycle }))
}
/>
</div>

<div>
<label htmlFor="departureIcao">Departure ICAO</label>
<SelectInput
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/components/SelectInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export function SelectInput(props: {
options: Option[]
onChange: (value: string) => void
name: string
defaultValue?: Option
}) {
return (
<Select<Option>
options={props.options}
name={props.name}
unstyled
defaultValue={props.defaultValue}
classNames={{
control: (state) => {
let className =
Expand Down
13 changes: 13 additions & 0 deletions packages/app/src/services/fetch-cycles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { db, flights } from '@mach/database'
import { desc, sql } from 'drizzle-orm'

export async function fetchCycles() {
const cycles = await db
.select({
cycle: sql<string>`DISTINCT(${flights.cycle})`,
})
.from(flights)
.orderBy(desc(flights.cycle))

return cycles.map((v) => v.cycle)
}
6 changes: 5 additions & 1 deletion packages/app/src/services/fetch-flights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { fetchAirportsData } from './fetch-airports'
import { currentCycleSubquery } from '../utils/currentCycleSubquery'

const schema = z.object({
cycle: z
.string()
.transform((v) => new Date(v))
.optional(),
departureIcao: z
.string()
.transform((v) => v.toUpperCase())
Expand Down Expand Up @@ -33,7 +37,7 @@ export async function fetchFlights(searchParams: Record<string, unknown>) {
const flights = await db.query.flights.findMany({
where: (fields, { sql, and, eq, or }) =>
and(
eq(fields.cycle, currentCycleSubquery),
where.cycle ? eq(fields.cycle, where.cycle) : eq(fields.cycle, currentCycleSubquery),
where.departureIcao
? eq(fields.departureIcao, where.departureIcao)
: undefined,
Expand Down

0 comments on commit 4f88509

Please sign in to comment.