From 90bba7f40fbeac78a394a275b29f7b8facb22eaa Mon Sep 17 00:00:00 2001 From: Drew Rautenberg Date: Mon, 20 Jan 2025 15:42:49 -0600 Subject: [PATCH] form stuff --- barkmanui/src/features/inventory/EditItem.tsx | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/barkmanui/src/features/inventory/EditItem.tsx b/barkmanui/src/features/inventory/EditItem.tsx index 2755408..3859a82 100644 --- a/barkmanui/src/features/inventory/EditItem.tsx +++ b/barkmanui/src/features/inventory/EditItem.tsx @@ -3,6 +3,7 @@ import { useForm } from '@mantine/form'; import {useParams} from "react-router"; import {useQuery} from "@tanstack/react-query"; import {InventoryItem} from "./types.ts"; +import {useEffect} from "react"; function EditItem() { const params = useParams(); @@ -10,14 +11,20 @@ function EditItem() { const form = useForm({ mode: 'uncontrolled', initialValues: { - email: '', - termsOfService: false, + name: "", + brand: "", + status: "", + serialNumber: "", + rentalPrice: 0, + replacementCost: 0, + notes: "", }, validate: { }, }); + const { isPending, error, data, isFetching } = useQuery({ queryKey: ['inventory', params.itemId], queryFn: async (): Promise => { @@ -31,22 +38,28 @@ function EditItem() { }, }); + useEffect(() => { + if (data) { + // Even if query.data changes, form will be initialized only once + form.initialize(data); + } + }, [data, form]); + if (isPending) return 'Loading...' if (error) return 'An error has occurred: ' + error.message - return (
console.log(values))}>
{isFetching ? 'Updating...' : ''}
ID: {data.id} - - - - - - -