From 4e07a242dffe9b8f8643108952375e8b7fe16954 Mon Sep 17 00:00:00 2001 From: Ben Sherriff Date: Fri, 22 Dec 2023 16:16:27 -0500 Subject: [PATCH] Condensed airport update/create forms into modal --- ui/src/app/admin/page.tsx | 35 +++- ui/src/components/Admin/AirportForm.tsx | 159 ++++++++++++++++++ ui/src/components/Admin/AirportTablePanel.tsx | 15 +- .../components/Admin/CreateAirportPanel.tsx | 140 --------------- .../components/Admin/UpdateAirportModal.tsx | 140 --------------- 5 files changed, 199 insertions(+), 290 deletions(-) create mode 100644 ui/src/components/Admin/AirportForm.tsx delete mode 100644 ui/src/components/Admin/CreateAirportPanel.tsx delete mode 100644 ui/src/components/Admin/UpdateAirportModal.tsx diff --git a/ui/src/app/admin/page.tsx b/ui/src/app/admin/page.tsx index 4ee6c41..cca1594 100644 --- a/ui/src/app/admin/page.tsx +++ b/ui/src/app/admin/page.tsx @@ -1,15 +1,16 @@ 'use client'; +import { createAirport, removeAirport, updateAirport } from "@/api/airport"; import { Airport } from "@/api/airport.types"; +import AirportForm from "@/components/Admin/AirportForm"; import AirportTablePanel from "@/components/Admin/AirportTablePanel"; -import CreateAirportPanel from "@/components/Admin/CreateAirportPanel"; -import UpdateAirportModal from "@/components/Admin/UpdateAirportModal"; import { isAdminState } from "@/state/auth"; -import { Container, Grid, SimpleGrid } from "@mantine/core"; +import { Container, Grid, Modal, SimpleGrid } from "@mantine/core"; import { useState } from "react"; import { useRecoilValue } from "recoil"; export default function Page() { + const [showModal, setShowModal] = useState(false); const [airport, setAirport] = useState(undefined); const isAdmin = useRecoilValue(isAdminState); @@ -20,14 +21,32 @@ export default function Page() { - + - - - - + { + setAirport(undefined); + setShowModal(false); + }}> + { + const response = await removeAirport({ icao: airport.icao }); + setShowModal(false); + } : undefined} + onSubmit={async (value) => { + if (airport) { + const response = await updateAirport({ airport: value }); + } else { + const response = await createAirport({ airport: value }); + } + setShowModal(false); + }} + /> + )} diff --git a/ui/src/components/Admin/AirportForm.tsx b/ui/src/components/Admin/AirportForm.tsx new file mode 100644 index 0000000..f201d41 --- /dev/null +++ b/ui/src/components/Admin/AirportForm.tsx @@ -0,0 +1,159 @@ +import { Airport, AirportCategory } from '@/api/airport.types'; +import { Button, Checkbox, Container, Flex, Group, NumberInput, Paper, Select, TextInput, Title } from '@mantine/core'; +import { useForm } from '@mantine/form'; + +interface AirportFormProps { + title: string; + airport?: Airport; + submitText: string; + onSubmit: (airport: Airport) => Promise; + onDelete?: () => Promise; +} + +export default function AirportForm({ title, airport, submitText, onSubmit, onDelete }: AirportFormProps) { + const form = useForm({ + initialValues: { + icao: airport?.icao || '', + category: airport?.category || AirportCategory.SMALL, + name: airport?.name || '', + elevation_ft: airport?.elevation_ft || 0, + iso_country: airport?.iso_country || '', + iso_region: airport?.iso_region || '', + municipality: airport?.municipality || '', + iata: airport?.iata || '', + local: airport?.local || '', + latitude: airport?.latitude || 0, + longitude: airport?.longitude || 0, + has_tower: airport?.has_tower || false, + has_beacon: airport?.has_beacon || false, + runways: airport?.runways || [], + frequencies: airport?.frequencies || [], + } + }); + + return ( + + {title} + +
{ + await onSubmit(values); + form.reset(); + })}> + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - -} \ No newline at end of file diff --git a/ui/src/components/Admin/UpdateAirportModal.tsx b/ui/src/components/Admin/UpdateAirportModal.tsx deleted file mode 100644 index 9be5f37..0000000 --- a/ui/src/components/Admin/UpdateAirportModal.tsx +++ /dev/null @@ -1,140 +0,0 @@ -import { removeAirport, updateAirport } from "@/api/airport"; -import { Airport, AirportCategory } from "@/api/airport.types"; -import { Button, Container, Flex, Group, Modal, Paper, Select, TextInput, Title } from "@mantine/core"; -import { useForm } from "@mantine/form"; -import { useEffect } from "react"; - -export default function UpdateAirportModal({ airport, setAirport }: { airport: Airport | undefined, setAirport: (airport: Airport | undefined) => void}) { - const form = useForm({ - initialValues: { - icao: airport?.icao || '', - category: airport?.category || AirportCategory.SMALL, - name: airport?.name || '', - elevation_ft: airport?.elevation_ft || 0, - iso_country: airport?.iso_country || '', - iso_region: airport?.iso_region || '', - municipality: airport?.municipality || '', - iata: airport?.iata || '', - local: airport?.local || '', - latitude: airport?.latitude || 0, - longitude: airport?.longitude || 0, - has_tower: airport?.has_tower || false, - has_beacon: airport?.has_beacon || false, - runways: airport?.runways || [], - frequencies: airport?.frequencies || [], - } - }); - - useEffect(() => { - if (airport) { - form.setValues(airport); - } - }, [airport]); - - return ( - setAirport(undefined)} withCloseButton={false} size={'50%'}> - - Update Airport - -
{ - const response = await updateAirport({ airport: values }); - if (response.success) { - setAirport(undefined); - } - })}> - -