diff --git a/barkmanui/src/App.tsx b/barkmanui/src/App.tsx index d901048..9633399 100644 --- a/barkmanui/src/App.tsx +++ b/barkmanui/src/App.tsx @@ -6,6 +6,7 @@ import {createTheme, MantineProvider} from '@mantine/core'; import '@mantine/core/styles.css'; import ItemDetail from "./features/inventory/ItemDetail.tsx"; import EditItem from "./features/inventory/EditItem.tsx"; +import {BarkHeader} from "./common/components/BarkHeader.tsx"; // Create a client const queryClient = new QueryClient() @@ -33,6 +34,7 @@ function App() { return { ( <> + } /> } /> @@ -40,7 +42,6 @@ function App() { } /> - ) }; diff --git a/barkmanui/src/features/inventory/EditItem.tsx b/barkmanui/src/features/inventory/EditItem.tsx index c0c2e23..e16309b 100644 --- a/barkmanui/src/features/inventory/EditItem.tsx +++ b/barkmanui/src/features/inventory/EditItem.tsx @@ -1,13 +1,30 @@ -import {NumberInput, Stack, Text, Textarea, TextInput} from "@mantine/core"; -import {BarkHeader} from "../../common/components/BarkHeader.tsx"; +import {Button, Group, TextInput, Text, Textarea, NumberInput} from '@mantine/core'; +import { useForm } from '@mantine/form'; import {useParams} from "react-router"; +import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"; import {InventoryItem} from "./types.ts"; -import {useQuery} from "@tanstack/react-query"; - +import {useEffect} from "react"; +type EditableInventoryItem = Omit; function EditItem() { - let params = useParams(); + const params = useParams(); + + const form = useForm({ + mode: 'uncontrolled', + initialValues: { + name: "", + brand: "", + status: "", + serialNumber: "", + rentalPrice: 0, + replacementCost: 0, + notes: "", + }, + + validate: { + }, + }); const { isPending, error, data, isFetching } = useQuery({ queryKey: ['inventory', params.itemId], @@ -22,35 +39,67 @@ function EditItem() { }, }); + const queryClient = useQueryClient(); + + const updateItem = useMutation({ + mutationFn: async (values: EditableInventoryItem) => { + + // await + 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(() => { + if (data) { + // Even if query.data changes, form will be initialized only once + form.initialize(data); + } + }, [data]); + if (isPending) return 'Loading...' if (error) return 'An error has occurred: ' + error.message return ( - <> - - - Edit Item -
{isFetching ? 'Updating...' : ''}
- ID: {data.id} - - - - - - - Replacement Cost: ${data.replacementCost} -