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 axiosInstance from "./config";
import type { InventoryItem, CreateInventoryItemInput } from "@/types"; import type {
InventoryItem,
CreateInventoryItemInput,
InventoryItemStatus,
} from "@/types";
/** /**
* Simulates an API call to fetch inventory items. * Simulates an API call to fetch inventory items.
* Waits for a simulated delay and then attempts an axios GET request. * Waits for a simulated delay and then attempts an axios GET request.
* If the request fails, returns fallback dummy data. * 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[]> { export async function fetchInventoryItems(): Promise<InventoryItem[]> {
try { try {
const response = await axiosInstance.get<InventoryItem[]>("/api/inventory"); 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 { FaSort, FaSortUp, FaSortDown } from "react-icons/fa";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
import { fetchInventoryItems } from "@/api/inventory"; import { fetchInventoryItems, fetchInventoryStatus } from "@/api/inventory";
import { AddInventoryItem } from "./add-inventory-item"; import { AddInventoryItem } from "./add-inventory-item";
import { import {
EditInventoryItem, EditInventoryItem,
@ -48,14 +48,25 @@ export default function InventoryPage() {
const { const {
data: inventoryItems = [], data: inventoryItems = [],
isLoading, isLoading: isItemLoading,
isError, isError: isItemError,
} = useQuery({ } = useQuery({
queryKey: ["inventoryItems"], queryKey: ["inventoryItems"],
queryFn: fetchInventoryItems, queryFn: fetchInventoryItems,
staleTime: 60 * 1000, staleTime: 60 * 1000,
}); });
const {
data: inventoryStatus = [],
isLoading: isLoadingStatus,
isError: isErrorStatus,
} = useQuery({
queryKey: ["inventoryStatus"],
queryFn: fetchInventoryStatus,
staleTime: 60 * 1000,
});
// console.table(inventoryItems); // console.table(inventoryItems);
console.table(inventoryStatus);
const [searchTerm, setSearchTerm] = useState(""); const [searchTerm, setSearchTerm] = useState("");
const filteredItems = useMemo(() => { const filteredItems = useMemo(() => {
return inventoryItems return inventoryItems
@ -122,13 +133,13 @@ export default function InventoryPage() {
onPaginationChange: setPagination, onPaginationChange: setPagination,
}); });
if (isLoading) if (isItemLoading || isLoadingStatus)
return ( return (
<div className="flex min-h-screen items-center justify-center"> <div className="flex min-h-screen items-center justify-center">
Loading... Loading...
</div> </div>
); );
if (isError) if (isItemError || isErrorStatus)
return ( return (
<div className="flex min-h-screen items-center justify-center"> <div className="flex min-h-screen items-center justify-center">
Error loading inventory data. Error loading inventory data.

View File

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