From 4a0e49a959a94cee47fddc3a01b82750f3911111 Mon Sep 17 00:00:00 2001 From: april Date: Fri, 5 Jan 2024 15:41:47 -0600 Subject: [PATCH] Implement flight updating --- web/app/routes/logbook.flights.$id/route.tsx | 7 ++- .../routes/logbook.flights.edit.$id/route.tsx | 63 +++++++++++++++++++ web/app/routes/logbook.flights.new/route.tsx | 1 + web/app/ui/form/flight-form.tsx | 19 +++++- web/app/util/types.ts | 27 ++++++++ 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 web/app/routes/logbook.flights.edit.$id/route.tsx diff --git a/web/app/routes/logbook.flights.$id/route.tsx b/web/app/routes/logbook.flights.$id/route.tsx index d6961e9..2130f02 100644 --- a/web/app/routes/logbook.flights.$id/route.tsx +++ b/web/app/routes/logbook.flights.$id/route.tsx @@ -91,7 +91,12 @@ export default function Flight() { Flight Log - + + navigate(`/logbook/flights/edit/${params.id}`) + } + > diff --git a/web/app/routes/logbook.flights.edit.$id/route.tsx b/web/app/routes/logbook.flights.edit.$id/route.tsx new file mode 100644 index 0000000..602a6e4 --- /dev/null +++ b/web/app/routes/logbook.flights.edit.$id/route.tsx @@ -0,0 +1,63 @@ +import { Container, Stack, Title } from "@mantine/core"; +import { + FlightFormSchema, + flightCreateHelper, + flightEditHelper, +} from "@/util/types"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { useApi } from "@/util/api"; +import { useNavigate, useParams } from "@remix-run/react"; +import { AxiosError } from "axios"; +import FlightForm from "@/ui/form/flight-form"; + +export default function EditFlight() { + const params = useParams(); + + const navigate = useNavigate(); + const queryClient = useQueryClient(); + + const client = useApi(); + + const flight = useQuery({ + queryKey: [params.id], + queryFn: async () => + await client.get(`/flights/${params.id}`).then((res) => res.data), + }); + + const editFlight = useMutation({ + mutationFn: async (values: FlightFormSchema) => { + const newFlight = flightCreateHelper(values); + if (newFlight) { + const res = await client.put(`/flights/${params.id}`, newFlight); + return res.data; + } + throw new Error("Flight updating failed"); + }, + retry: (failureCount, error: AxiosError) => { + return !error || error.response?.status !== 401; + }, + onSuccess: async (data: { id: string }) => { + await queryClient.invalidateQueries({ queryKey: ["flights-list"] }); + navigate(`/logbook/flights/${data.id}`); + }, + }); + + return ( + + + Edit Flight + + navigate(`/logbook/flights/${params.id}`)} + mah="calc(100vh - 95px - 110px)" + /> + + + ); +} diff --git a/web/app/routes/logbook.flights.new/route.tsx b/web/app/routes/logbook.flights.new/route.tsx index 18531f0..0f38997 100644 --- a/web/app/routes/logbook.flights.new/route.tsx +++ b/web/app/routes/logbook.flights.new/route.tsx @@ -39,6 +39,7 @@ export default function NewFlight() { onSubmit={createFlight.mutate} isError={createFlight.isError} error={createFlight.error} + mah="calc(100vh - 95px - 110px)" /> diff --git a/web/app/ui/form/flight-form.tsx b/web/app/ui/form/flight-form.tsx index 253cc12..725b90a 100644 --- a/web/app/ui/form/flight-form.tsx +++ b/web/app/ui/form/flight-form.tsx @@ -26,11 +26,19 @@ export default function FlightForm({ isError, error, initialValues, + mah, + submitButtonLabel, + withCancelButton, + cancelFunc, }: { onSubmit: (values: FlightFormSchema) => void; isError: boolean; error: AxiosError | null; - initialValues?: FlightFormSchema; + initialValues?: FlightFormSchema | null; + mah?: string; + submitButtonLabel?: string; + withCancelButton?: boolean; + cancelFunc?: () => void; }) { const form = useForm({ initialValues: initialValues ?? { @@ -84,7 +92,7 @@ export default function FlightForm({ return (
onSubmit(values))}> - + {/* Date and Aircraft */} @@ -289,8 +297,13 @@ export default function FlightForm({ {error?.message} ) : null} + {withCancelButton ? ( + + ) : null} diff --git a/web/app/util/types.ts b/web/app/util/types.ts index 91cc973..b1ad463 100644 --- a/web/app/util/types.ts +++ b/web/app/util/types.ts @@ -135,7 +135,34 @@ const flightCreateHelper = ( } }; +const flightEditHelper = ( + values: FlightCreateSchema +): FlightFormSchema | void => { + try { + const flight = { + ...values, + date: dayjs(values.date), + time_start: Number( + `${dayjs(values.time_start).hour()}${dayjs(values.time_start).minute()}` + ), + time_off: Number( + `${dayjs(values.time_off).hour()}${dayjs(values.time_off).minute()}` + ), + time_down: Number( + `${dayjs(values.time_down).hour()}${dayjs(values.time_down).minute()}` + ), + time_stop: Number( + `${dayjs(values.time_stop).hour()}${dayjs(values.time_stop).minute()}` + ), + }; + return flight; + } catch (err) { + console.log(err); + } +}; + export { + flightEditHelper, flightCreateHelper, type FlightFormSchema, type FlightCreateSchema,