form stuff pt2

This commit is contained in:
2025-01-20 21:13:17 -06:00
parent 90bba7f40f
commit 581fd5dd7b
+49 -11
View File
@@ -1,14 +1,17 @@
import {Button, Group, TextInput, Text, Textarea, NumberInput} from '@mantine/core'; import {Button, Group, TextInput, Text, Textarea, NumberInput} from '@mantine/core';
import { useForm } from '@mantine/form'; import { useForm } from '@mantine/form';
import {useParams} from "react-router"; import {useParams} from "react-router";
import {useQuery} from "@tanstack/react-query"; import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query";
import {InventoryItem} from "./types.ts"; import {InventoryItem} from "./types.ts";
import {useEffect} from "react"; import {useEffect} from "react";
import {put} from "axios";
type EditableInventoryItem = Omit<InventoryItem, 'id'>;
function EditItem() { function EditItem() {
const params = useParams(); const params = useParams();
const form = useForm({ const form = useForm<EditableInventoryItem>({
mode: 'uncontrolled', mode: 'uncontrolled',
initialValues: { initialValues: {
name: "", name: "",
@@ -25,6 +28,8 @@ function EditItem() {
}); });
const { isPending, error, data, isFetching } = useQuery({ const { isPending, error, data, isFetching } = useQuery({
queryKey: ['inventory', params.itemId], queryKey: ['inventory', params.itemId],
queryFn: async (): Promise<InventoryItem> => { queryFn: async (): Promise<InventoryItem> => {
@@ -38,28 +43,61 @@ function EditItem() {
}, },
}); });
const queryClient = useQueryClient();
const updateItem = useMutation({
mutationFn: async (values: EditableInventoryItem) => {
// await <call the api, do the things>
const result = await fetch(import.meta.env.VITE_API_URL + '/inventory/' + params.itemId, {
method: 'PUT',
body: JSON.stringify(values),
headers: {
'Content-Type': 'application/json'
}
});
if (!result.ok) {
throw new Error('Failed to update inventory item');
}
// invalidate the queries so they pull updated information
// this is a prefix, so it covers both the query that pulls a list, and also `['inventory', itemId]` in this file
await queryClient.invalidateQueries({
queryKey: ['inventory']
});
}
})
useEffect(() => { useEffect(() => {
if (data) { if (data) {
// Even if query.data changes, form will be initialized only once // Even if query.data changes, form will be initialized only once
form.initialize(data); form.initialize(data);
} }
}, [data, form]); }, [data]);
if (isPending) return 'Loading...' if (isPending) return 'Loading...'
if (error) return 'An error has occurred: ' + error.message if (error) return 'An error has occurred: ' + error.message
return ( return (
<form onSubmit={form.onSubmit((values) => console.log(values))}> <form onSubmit={form.onSubmit(async (values) => await
updateItem.mutateAsync(values))}>
<div>{isFetching ? 'Updating...' : ''}</div> <div>{isFetching ? 'Updating...' : ''}</div>
<Text>ID: {data.id}</Text> <Text>ID: {data.id}</Text>
<TextInput withAsterisk key={form.key('brand')} size="md" label="Brand" placeholder="Brand"/> <TextInput withAsterisk key={form.key('brand')} size="md" label="Brand"
<TextInput withAsterisk key={form.key('name')} size="md" label="Name" placeholder="Name"/> placeholder="Brand" {...form.getInputProps('brand')}/>
<TextInput size="md" key={form.key('status')} label="Status" placeholder="Status"/> <TextInput withAsterisk key={form.key('name')} size="md" label="Name"
<TextInput size="md" key={form.key('serialNumber')} label="Serial Number" placeholder="Serial Number"/> placeholder="Name" {...form.getInputProps('name')}/>
<NumberInput size="md" key={form.key('rentalPrice')} label="Rental Price" placeholder="Rental Price"/> <TextInput size="md" key={form.key('status')} label="Status"
<NumberInput size="md" key={form.key('replacementCost')} label="Replacement Cost" placeholder="Replacement Cost"/> placeholder="Status" {...form.getInputProps('status')}/>
<Textarea label="Notes" key={form.key('notes')} placeholder="Notes"/> <TextInput size="md" key={form.key('serialNumber')} label="Serial Number"
placeholder="Serial Number"{...form.getInputProps('serialNumber')}/>
<NumberInput size="md" key={form.key('rentalPrice')} label="Rental Price"
placeholder="Rental Price" {...form.getInputProps('rentalPrice')}/>
<NumberInput size="md" key={form.key('replacementCost')} label="Replacement Cost"
placeholder="Replacement Cost" {...form.getInputProps('replacementCost')}/>
<Textarea label="Notes" key={form.key('notes')} placeholder="Notes" {...form.getInputProps('notes')}/>
<Group justify="flex-end" mt="md"> <Group justify="flex-end" mt="md">
<Button type="submit">Submit</Button> <Button type="submit">Submit</Button>