feat: add fetchInventoryStatus API and integrate into inventory page

This commit is contained in:
Pattadon 2025-04-01 16:07:09 +07:00
parent a00111aa07
commit e7bdb5d6a1
3 changed files with 39 additions and 6 deletions

View File

@ -1,11 +1,29 @@
import axiosInstance from "./config";
import type { InventoryItem, CreateInventoryItemInput } from "@/types";
import type {
InventoryItem,
CreateInventoryItemInput,
InventoryItemStatus,
} from "@/types";
/**
* Simulates an API call to fetch inventory items.
* Waits for a simulated delay and then attempts an axios GET request.
* If the request fails, returns fallback dummy data.
*
*
*/
export async function fetchInventoryStatus(): Promise<InventoryItemStatus[]> {
try {
const response = await axiosInstance.get<InventoryItemStatus[]>(
"/inventory/status"
);
return response.data;
} catch (error) {
console.error("Error fetching inventory status:", error);
return [];
}
}
export async function fetchInventoryItems(): Promise<InventoryItem[]> {
try {
const response = await axiosInstance.get<InventoryItem[]>("/api/inventory");

View File

@ -31,7 +31,7 @@ import { Search } from "lucide-react";
import { FaSort, FaSortUp, FaSortDown } from "react-icons/fa";
import { Badge } from "@/components/ui/badge";
import { fetchInventoryItems } from "@/api/inventory";
import { fetchInventoryItems, fetchInventoryStatus } from "@/api/inventory";
import { AddInventoryItem } from "./add-inventory-item";
import {
EditInventoryItem,
@ -48,14 +48,25 @@ export default function InventoryPage() {
const {
data: inventoryItems = [],
isLoading,
isError,
isLoading: isItemLoading,
isError: isItemError,
} = useQuery({
queryKey: ["inventoryItems"],
queryFn: fetchInventoryItems,
staleTime: 60 * 1000,
});
const {
data: inventoryStatus = [],
isLoading: isLoadingStatus,
isError: isErrorStatus,
} = useQuery({
queryKey: ["inventoryStatus"],
queryFn: fetchInventoryStatus,
staleTime: 60 * 1000,
});
// console.table(inventoryItems);
console.table(inventoryStatus);
const [searchTerm, setSearchTerm] = useState("");
const filteredItems = useMemo(() => {
return inventoryItems
@ -122,13 +133,13 @@ export default function InventoryPage() {
onPaginationChange: setPagination,
});
if (isLoading)
if (isItemLoading || isLoadingStatus)
return (
<div className="flex min-h-screen items-center justify-center">
Loading...
</div>
);
if (isError)
if (isItemError || isErrorStatus)
return (
<div className="flex min-h-screen items-center justify-center">
Error loading inventory data.

View File

@ -69,6 +69,10 @@ export type InventoryItem = {
lastUpdated: string;
status: string;
};
export type InventoryItemStatus = {
id: number;
name: string;
};
export type CreateInventoryItemInput = Omit<InventoryItem, "id" | "lastUpdated" | "status">;