diff --git a/barkmanui/src/features/inventory/InventoryList.tsx b/barkmanui/src/features/inventory/InventoryList.tsx index d97f18b..764293f 100644 --- a/barkmanui/src/features/inventory/InventoryList.tsx +++ b/barkmanui/src/features/inventory/InventoryList.tsx @@ -1,27 +1,13 @@ -import {useQuery} from "@tanstack/react-query"; -import {InventoryItem} from "./types"; import {Flex, Table} from '@mantine/core'; import BarkButton from "../../common/components/BarkButton.tsx"; import {Link, NavLink} from "react-router"; +import useInventoryList from "./hooks/useInventoryList.tsx"; function InventoryList() { - const { isPending, error, data, isFetching } = useQuery({ - queryKey: ['inventory'], - queryFn: async (): Promise => { - const response = await fetch( - import.meta.env.VITE_API_URL + '/inventory', - ) + const data = useInventoryList(); - 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 return ( <> @@ -40,8 +26,6 @@ function InventoryList() { -
{isFetching ? 'Updating...' : ''}
- @@ -53,7 +37,7 @@ function InventoryList() { - {data.map((data) => ( + {data?.map((data) => ( {data.barcode} {data.brand} diff --git a/barkmanui/src/features/inventory/hooks/useInventoryList.tsx b/barkmanui/src/features/inventory/hooks/useInventoryList.tsx new file mode 100644 index 0000000..6c1ab01 --- /dev/null +++ b/barkmanui/src/features/inventory/hooks/useInventoryList.tsx @@ -0,0 +1,17 @@ +import {useQuery} from "@tanstack/react-query"; +import {InventoryItem} from "../types.js"; + + const useInventoryList = () => 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() + }, + }); + +export default useInventoryList; \ No newline at end of file