mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-18 21:44:08 +01:00
feat: add fetchInventoryStatus API and integrate into inventory page
This commit is contained in:
parent
a00111aa07
commit
e7bdb5d6a1
@ -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");
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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">;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user