From cf42d76fd789b6bdfcf8159e0ad58e71f5be53f6 Mon Sep 17 00:00:00 2001 From: lcross2002 <liamdcross@outlook.com> Date: Sun, 7 Apr 2024 13:53:12 +0100 Subject: [PATCH] Centralise airports in frontend --- .../components/BookingQuery/BookingQuery.tsx | 2 +- .../FlightCreationForm/FlightCreationForm.tsx | 17 ++++++++++++++--- client/src/helpers/Airports.ts | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 client/src/helpers/Airports.ts diff --git a/client/src/components/BookingQuery/BookingQuery.tsx b/client/src/components/BookingQuery/BookingQuery.tsx index c78c892..1760725 100644 --- a/client/src/components/BookingQuery/BookingQuery.tsx +++ b/client/src/components/BookingQuery/BookingQuery.tsx @@ -1,6 +1,7 @@ import { useState } from 'react'; import { useForm } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; +import { airports } from '../../helpers/Airports'; import './BookingQuery.scss'; interface IBookingQuery { @@ -14,7 +15,6 @@ function BookingQuery() { const navigate = useNavigate(); const [error, setError] = useState(''); const { register, handleSubmit } = useForm<IBookingQuery>({mode: 'onChange', defaultValues: { origin: '', destination: '', seatType: ''}}); - const airports: string[] = ['LGW', 'LHR', 'BHX', 'MAN', 'EDI']; const onSubmit = (query: IBookingQuery) => { if (query.origin === query.destination) { diff --git a/client/src/components/FlightCreationForm/FlightCreationForm.tsx b/client/src/components/FlightCreationForm/FlightCreationForm.tsx index 6e68144..0eccb62 100644 --- a/client/src/components/FlightCreationForm/FlightCreationForm.tsx +++ b/client/src/components/FlightCreationForm/FlightCreationForm.tsx @@ -3,6 +3,7 @@ import { useNavigate } from 'react-router-dom'; import { useForm } from 'react-hook-form'; import { AxiosError } from 'axios'; import { addFlight } from '../../services/FlightForm/FlightForm'; +import { airports } from '../../helpers/Airports'; import './FlightCreationForm.scss'; export interface IFlightCreationForm { @@ -20,7 +21,7 @@ function FlightCreationForm() { const navigate = useNavigate(); const [error, setError] = useState(''); const [disabled, setDisabled] = useState(false); - const { register, handleSubmit } = useForm<IFlightCreationForm>({ mode: 'onChange' }); + const { register, handleSubmit } = useForm<IFlightCreationForm>({ mode: 'onChange', defaultValues: { origin: '', destination: '' } }); const onSubmit = async (formValue: IFlightCreationForm) => { if (!Number.isInteger(formValue.businessCapacity) || !Number.isInteger(formValue.economyCapacity)) { @@ -71,12 +72,22 @@ function FlightCreationForm() { <div className='form-col'> <div className='form-group'> <label>Origin</label> - <input type='text' placeholder='Enter origin' {...register('origin', { required: true })} /> + <select {...register('origin', { required: true })} > + <option value={''} disabled>Select an airport</option> + {airports.map((airport) => { + return <option key={airport} value={airport}>{airport}</option> + })} + </select> </div> <div className='form-group'> <label>Destination</label> - <input type='text' placeholder='Enter destination' {...register('destination', { required: true })} /> + <select {...register('destination', { required: true })} > + <option value={''} disabled>Select an airport</option> + {airports.map((airport) => { + return <option key={airport} value={airport}>{airport}</option> + })} + </select> </div> <div className='form-group'> diff --git a/client/src/helpers/Airports.ts b/client/src/helpers/Airports.ts new file mode 100644 index 0000000..7969723 --- /dev/null +++ b/client/src/helpers/Airports.ts @@ -0,0 +1,16 @@ +const airportMap = new Map<string, string>(); +airportMap.set('London Heathrow', 'LHW'); +airportMap.set('London Gatwick', 'LGW'); +airportMap.set('Manchester', 'MAN'); +airportMap.set('London Stanstead', 'STN'); +airportMap.set('London Luton', 'LTN'); +airportMap.set('Edinburgh', 'EDI'); +airportMap.set('Birmingham', 'BHX'); +airportMap.set('Bristol', 'BRS'); +airportMap.set('Glasgow', 'GLA'); +airportMap.set('Belfast International', 'BFS'); +airportMap.set('Newcastle', 'NCL'); +airportMap.set('Liverpool', 'LPL'); +airportMap.set('Norwich', 'NWI'); + +export const airports = [...airportMap.keys()]; -- GitLab