Implement flight deletion

This commit is contained in:
april
2024-01-05 14:54:44 -06:00
parent 40108d1070
commit 654393bab8
6 changed files with 356 additions and 275 deletions

View File

@@ -5,8 +5,9 @@ import {
Fieldset,
Group,
NumberInput,
ScrollAreaAutosize,
ScrollArea,
Stack,
Text,
TextInput,
Textarea,
Title,
@@ -23,7 +24,6 @@ import ListInput from "@/ui/form/list-input";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useApi } from "@/util/api";
import { useNavigate } from "@remix-run/react";
import { useAuth } from "@/util/auth";
import { AxiosError } from "axios";
export default function NewFlight() {
@@ -82,24 +82,18 @@ export default function NewFlight() {
const client = useApi();
const { clearUser } = useAuth();
const createFlight = useMutation({
mutationFn: async (values: FlightFormSchema) => {
const newFlight = flightCreateHelper(values);
const res = await client.post("/flights", newFlight);
return res.data;
if (newFlight) {
const res = await client.post("/flights", newFlight);
return res.data;
}
throw new Error("Flight creation failed");
},
retry: (failureCount, error: AxiosError) => {
return !error || error.response?.status !== 401;
},
onError: (error: AxiosError) => {
console.log(error);
if (error.response?.status === 401) {
clearUser();
navigate("/login");
}
},
onSuccess: async (data: { id: string }) => {
await queryClient.invalidateQueries({ queryKey: ["flights-list"] });
navigate(`/logbook/flights/${data.id}`);
@@ -112,7 +106,7 @@ export default function NewFlight() {
<Title order={2}>New Flight</Title>
<form onSubmit={form.onSubmit((values) => createFlight.mutate(values))}>
<ScrollAreaAutosize mah="calc(100vh - 95px - 110px)">
<ScrollArea.Autosize mah="calc(100vh - 95px - 110px)">
<Container>
{/* Date and Aircraft */}
@@ -347,9 +341,14 @@ export default function NewFlight() {
/>
</Fieldset>
</Container>
</ScrollAreaAutosize>
</ScrollArea.Autosize>
<Group justify="flex-end" mt="md">
{createFlight.isError ? (
<Text c="red" fw={700}>
{createFlight.error.message}
</Text>
) : null}
<Button type="submit" leftSection={<IconPencil />}>
Create
</Button>