From 54adaa99ceb73e529de21ac65b983352ce3309bb Mon Sep 17 00:00:00 2001 From: Drew Rautenberg Date: Mon, 3 Feb 2025 11:23:16 -0600 Subject: [PATCH] added query to return last used barcode and autopoulate field --- barkmanui/src/features/inventory/AddItem.tsx | 24 +++++++++++++++++-- .../src/features/inventory/InventoryList.tsx | 1 - barkmanui/src/features/inventory/types.ts | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/barkmanui/src/features/inventory/AddItem.tsx b/barkmanui/src/features/inventory/AddItem.tsx index 33be18c..f457a06 100644 --- a/barkmanui/src/features/inventory/AddItem.tsx +++ b/barkmanui/src/features/inventory/AddItem.tsx @@ -1,7 +1,7 @@ import {Button, Group, TextInput, NumberInput, Container, Title, Flex} from '@mantine/core'; import {useForm} from '@mantine/form'; -import {useMutation} from "@tanstack/react-query"; -import {NewItem} from "./types.ts"; +import {useMutation, useQuery} from "@tanstack/react-query"; +import {InventoryItem, NewItem} from "./types.ts"; import { useNavigate} from "react-router"; import { IconX, IconCheck } from '@tabler/icons-react'; import { notifications } from '@mantine/notifications'; @@ -10,9 +10,29 @@ function AddItem() { const navigate = useNavigate(); + const { isPending, error, data} = useQuery({ + queryKey: ['inventory'], + queryFn: async (): Promise => { + const response = await fetch( + import.meta.env.VITE_API_URL + '/inventory', + ) + + if (!response.ok) throw new Error('Failed to fetch inventory ' + response.statusText) + + return await response.json() + }, + }); + + if (isPending) return 'Loading...' + + if (error) return 'An error has occurred: ' + error.message + + const lastBarcode = data[data.length - 1].barcode; + const newItemForm = useForm({ mode: 'uncontrolled', initialValues: { + barcode: lastBarcode + 1, name: "", brand: "", serialNumber: "", diff --git a/barkmanui/src/features/inventory/InventoryList.tsx b/barkmanui/src/features/inventory/InventoryList.tsx index 4020b67..d97f18b 100644 --- a/barkmanui/src/features/inventory/InventoryList.tsx +++ b/barkmanui/src/features/inventory/InventoryList.tsx @@ -23,7 +23,6 @@ function InventoryList() { if (error) return 'An error has occurred: ' + error.message - return ( <>