Implement aircraft updating
This commit is contained in:
parent
164ab99c95
commit
37a452ac08
@ -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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<Modal
|
<Modal
|
||||||
@ -69,12 +82,31 @@ function AircraftCard({ aircraft }: { aircraft: AircraftSchema }) {
|
|||||||
</Group>
|
</Group>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
<Modal
|
||||||
|
opened={editOpened}
|
||||||
|
onClose={closeEdit}
|
||||||
|
title="Edit Aircraft"
|
||||||
|
centered
|
||||||
|
>
|
||||||
|
<AircraftForm
|
||||||
|
isError={updateAircraft.isError}
|
||||||
|
error={updateAircraft.error}
|
||||||
|
isPending={updateAircraft.isPending}
|
||||||
|
onSubmit={(values: AircraftFormSchema) =>
|
||||||
|
updateAircraft.mutate(values)
|
||||||
|
}
|
||||||
|
initialValues={aircraft}
|
||||||
|
submitButtonLabel="Update"
|
||||||
|
withCancelButton
|
||||||
|
cancelFunc={closeEdit}
|
||||||
|
/>
|
||||||
|
</Modal>
|
||||||
<Card key={randomId()} withBorder shadow="sm">
|
<Card key={randomId()} withBorder shadow="sm">
|
||||||
<Stack>
|
<Stack>
|
||||||
<Group grow justify="space-between">
|
<Group grow justify="space-between">
|
||||||
<Title order={4}>{aircraft.tail_no}</Title>
|
<Title order={4}>{aircraft.tail_no}</Title>
|
||||||
<Group justify="flex-end">
|
<Group justify="flex-end">
|
||||||
<ActionIcon variant="transparent">
|
<ActionIcon variant="transparent" onClick={openEdit}>
|
||||||
<IconPencil />
|
<IconPencil />
|
||||||
</ActionIcon>
|
</ActionIcon>
|
||||||
<ActionIcon
|
<ActionIcon
|
||||||
|
@ -72,8 +72,12 @@ export default function AircraftForm({
|
|||||||
await client.get(`/aircraft/categories`).then((res) => res.data),
|
await client.get(`/aircraft/categories`).then((res) => res.data),
|
||||||
});
|
});
|
||||||
|
|
||||||
const [category, setCategory] = useState("");
|
const [category, setCategory] = useState(
|
||||||
const [classSelection, setClassSelection] = useState("");
|
initialValues?.aircraft_category ?? ""
|
||||||
|
);
|
||||||
|
const [classSelection, setClassSelection] = useState<string | null>(
|
||||||
|
initialValues?.aircraft_class ?? ""
|
||||||
|
);
|
||||||
|
|
||||||
const classes = useQuery({
|
const classes = useQuery({
|
||||||
queryKey: ["classes", category],
|
queryKey: ["classes", category],
|
||||||
@ -107,7 +111,6 @@ export default function AircraftForm({
|
|||||||
{...newForm.getInputProps("aircraft_category")}
|
{...newForm.getInputProps("aircraft_category")}
|
||||||
label="Category"
|
label="Category"
|
||||||
placeholder="Pick a value"
|
placeholder="Pick a value"
|
||||||
name="aircraft_category"
|
|
||||||
withAsterisk
|
withAsterisk
|
||||||
data={
|
data={
|
||||||
categories.isFetched && !categories.isError
|
categories.isFetched && !categories.isError
|
||||||
@ -115,15 +118,18 @@ export default function AircraftForm({
|
|||||||
: []
|
: []
|
||||||
}
|
}
|
||||||
onChange={(_value, option) => {
|
onChange={(_value, option) => {
|
||||||
|
newForm.setFieldValue("aircraft_category", option.value);
|
||||||
setCategory(option.value);
|
setCategory(option.value);
|
||||||
setClassSelection("");
|
newForm.setFieldValue("aircraft_class", "");
|
||||||
|
setClassSelection(null);
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
queryKey: ["classes", option.value],
|
queryKey: ["classes", option.value],
|
||||||
});
|
});
|
||||||
newForm.setFieldValue("aircraft_category", option.value);
|
|
||||||
}}
|
}}
|
||||||
|
key={classSelection}
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
|
{...newForm.getInputProps("aircraft_class")}
|
||||||
label="Class"
|
label="Class"
|
||||||
placeholder="Pick a value"
|
placeholder="Pick a value"
|
||||||
withAsterisk
|
withAsterisk
|
||||||
@ -133,7 +139,10 @@ export default function AircraftForm({
|
|||||||
: []
|
: []
|
||||||
}
|
}
|
||||||
value={classSelection}
|
value={classSelection}
|
||||||
{...newForm.getInputProps("aircraft_class")}
|
onChange={(_value, option) => {
|
||||||
|
newForm.setFieldValue("aircraft_class", option.label);
|
||||||
|
setClassSelection(option.label);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<NumberInput
|
<NumberInput
|
||||||
label="Hobbs"
|
label="Hobbs"
|
||||||
@ -147,11 +156,15 @@ export default function AircraftForm({
|
|||||||
{isError ? (
|
{isError ? (
|
||||||
<Text c="red">
|
<Text c="red">
|
||||||
{error instanceof AxiosError
|
{error instanceof AxiosError
|
||||||
? error?.response?.data?.detail ?? "Error adding aircraft"
|
? error?.response?.data?.detail ??
|
||||||
|
error?.response?.data ??
|
||||||
|
error?.response ??
|
||||||
|
error?.message ??
|
||||||
|
"Failed to submit"
|
||||||
: error?.message}
|
: error?.message}
|
||||||
</Text>
|
</Text>
|
||||||
) : isPending ? (
|
) : isPending ? (
|
||||||
<Text c="yellow">Adding aircraft...</Text>
|
<Text c="yellow">Loading...</Text>
|
||||||
) : null}
|
) : null}
|
||||||
{withCancelButton ? (
|
{withCancelButton ? (
|
||||||
<Button leftSection={<IconX />} color="gray" onClick={cancelFunc}>
|
<Button leftSection={<IconX />} color="gray" onClick={cancelFunc}>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user