import ErrorDisplay from "@/ui/error-display";
import AircraftForm from "@/ui/form/aircraft-form";
import { useApi } from "@/util/api";
import { useAircraft } from "@/util/hooks";
import { AircraftFormSchema, AircraftSchema } from "@/util/types";
import {
ActionIcon,
Button,
Card,
Center,
Container,
Group,
Loader,
Modal,
ScrollArea,
Stack,
Text,
Title,
Tooltip,
} from "@mantine/core";
import { randomId, useDisclosure } from "@mantine/hooks";
import { IconPencil, IconPlus, IconTrash, IconX } from "@tabler/icons-react";
import {
UseQueryResult,
useMutation,
useQueryClient,
} from "@tanstack/react-query";
function AircraftCard({ aircraft }: { aircraft: AircraftSchema }) {
const [deleteOpened, { open: openDelete, close: closeDelete }] =
useDisclosure(false);
const client = useApi();
const queryClient = useQueryClient();
const deleteAircraft = useMutation({
mutationFn: async () =>
await client.delete(`/aircraft/${aircraft.id}`).then((res) => res.data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["aircraft-list"] });
},
});
return (
<>
Are you sure you want to delete this aircraft? This action cannot be
undone.
{deleteAircraft.isError ? (
{deleteAircraft.error.message}
) : null}
{aircraft.tail_no}
{aircraft.make}
{aircraft.model}
{aircraft.aircraft_category}
/
{aircraft.aircraft_class}
{aircraft.hobbs ? Hobbs: {aircraft.hobbs} : null}
>
);
}
function NewAircraftModal({
opened,
close,
}: {
opened: boolean;
close: () => void;
}) {
const client = useApi();
const queryClient = useQueryClient();
const addAircraft = useMutation({
mutationFn: async (values: AircraftFormSchema) => {
const newAircraft = values;
if (newAircraft) {
const res = await client.post("/aircraft", newAircraft);
return res.data;
}
throw new Error("Aircraft creation failed");
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["aircraft-list"] });
close();
},
});
return (
);
}
export default function Aircraft() {
const aircraft: UseQueryResult = useAircraft();
const [newOpened, { open: openNew, close: closeNew }] = useDisclosure(false);
return (
<>
Aircraft
{aircraft.isLoading ? (
) : aircraft.isError ? (
) : aircraft.data && aircraft.data.length === 0 ? (
No Aircraft
) : (
{aircraft.data?.map((item) => (
))}
)}
>
);
}