added query to return last used barcode and autopoulate field

This commit is contained in:
2025-02-03 11:23:16 -06:00
parent 571914ac0f
commit 54adaa99ce
3 changed files with 23 additions and 3 deletions
+22 -2
View File
@@ -1,7 +1,7 @@
import {Button, Group, TextInput, NumberInput, Container, Title, Flex} from '@mantine/core'; import {Button, Group, TextInput, NumberInput, Container, Title, Flex} from '@mantine/core';
import {useForm} from '@mantine/form'; import {useForm} from '@mantine/form';
import {useMutation} from "@tanstack/react-query"; import {useMutation, useQuery} from "@tanstack/react-query";
import {NewItem} from "./types.ts"; import {InventoryItem, NewItem} from "./types.ts";
import { useNavigate} from "react-router"; import { useNavigate} from "react-router";
import { IconX, IconCheck } from '@tabler/icons-react'; import { IconX, IconCheck } from '@tabler/icons-react';
import { notifications } from '@mantine/notifications'; import { notifications } from '@mantine/notifications';
@@ -10,9 +10,29 @@ function AddItem() {
const navigate = useNavigate(); const navigate = useNavigate();
const { isPending, error, data} = useQuery({
queryKey: ['inventory'],
queryFn: async (): Promise<InventoryItem[]> => {
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<NewItem>({ const newItemForm = useForm<NewItem>({
mode: 'uncontrolled', mode: 'uncontrolled',
initialValues: { initialValues: {
barcode: lastBarcode + 1,
name: "", name: "",
brand: "", brand: "",
serialNumber: "", serialNumber: "",
@@ -23,7 +23,6 @@ function InventoryList() {
if (error) return 'An error has occurred: ' + error.message if (error) return 'An error has occurred: ' + error.message
return ( return (
<> <>
<Flex <Flex
@@ -12,6 +12,7 @@ export interface InventoryItem {
} }
export interface NewItem { export interface NewItem {
barcode: number,
brand: string, brand: string,
name: string, name: string,
serialNumber: string, serialNumber: string,