From 37a452ac08ffc11351d92795b467af898fdb7ee1 Mon Sep 17 00:00:00 2001 From: april Date: Thu, 11 Jan 2024 14:54:03 -0600 Subject: [PATCH] Implement aircraft updating --- web/app/routes/logbook.aircraft/route.tsx | 34 ++++++++++++++++++++++- web/app/ui/form/aircraft-form.tsx | 29 +++++++++++++------ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/web/app/routes/logbook.aircraft/route.tsx b/web/app/routes/logbook.aircraft/route.tsx index d426a4a..6d85c0e 100644 --- a/web/app/routes/logbook.aircraft/route.tsx +++ b/web/app/routes/logbook.aircraft/route.tsx @@ -41,6 +41,19 @@ function AircraftCard({ aircraft }: { aircraft: AircraftSchema }) { }, }); + const [editOpened, { open: openEdit, close: closeEdit }] = + useDisclosure(false); + + const updateAircraft = useMutation({ + mutationFn: async (values: AircraftFormSchema) => + await client + .put(`/aircraft/${aircraft.id}`, values) + .then((res) => res.data), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["aircraft-list"] }); + }, + }); + return ( <> + + + updateAircraft.mutate(values) + } + initialValues={aircraft} + submitButtonLabel="Update" + withCancelButton + cancelFunc={closeEdit} + /> + {aircraft.tail_no} - + res.data), }); - const [category, setCategory] = useState(""); - const [classSelection, setClassSelection] = useState(""); + const [category, setCategory] = useState( + initialValues?.aircraft_category ?? "" + ); + const [classSelection, setClassSelection] = useState( + initialValues?.aircraft_class ?? "" + ); const classes = useQuery({ queryKey: ["classes", category], @@ -107,7 +111,6 @@ export default function AircraftForm({ {...newForm.getInputProps("aircraft_category")} label="Category" placeholder="Pick a value" - name="aircraft_category" withAsterisk data={ categories.isFetched && !categories.isError @@ -115,15 +118,18 @@ export default function AircraftForm({ : [] } onChange={(_value, option) => { + newForm.setFieldValue("aircraft_category", option.value); setCategory(option.value); - setClassSelection(""); + newForm.setFieldValue("aircraft_class", ""); + setClassSelection(null); queryClient.invalidateQueries({ queryKey: ["classes", option.value], }); - newForm.setFieldValue("aircraft_category", option.value); }} + key={classSelection} />