ForFarm/frontend/api/inventory.ts

125 lines
3.0 KiB
TypeScript

import axiosInstance from "./config";
import type {
InventoryItem,
InventoryItemStatus,
InventoryItemCategory,
CreateInventoryItemInput,
} 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 fetchInventoryCategory(): Promise<
InventoryItemCategory[]
> {
try {
const response = await axiosInstance.get<InventoryItemCategory[]>(
"/inventory/category"
);
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[]>("/inventory");
return response.data;
} catch (error) {
// console.error("Error while fetching inventory items! " + error);
// throw error;
// Fallback dummy data
return [
{
id: "1",
name: "Tomato Seeds",
categoryId: "1",
quantity: 500,
unitId: "1",
lastUpdated: "2023-03-01",
statusId: "1",
},
{
id: "2",
name: "NPK Fertilizer",
categoryId: "3",
quantity: 200,
unitId: "2",
lastUpdated: "2023-03-05",
statusId: "2",
},
{
id: "3",
name: "Corn Seeds",
categoryId: "1",
quantity: 300,
unitId: "1",
lastUpdated: "2023-03-10",
statusId: "3",
},
{
id: "4",
name: "Organic Compost",
categoryId: "3",
quantity: 150,
unitId: "2",
lastUpdated: "2023-03-15",
statusId: "1",
},
{
id: "5",
name: "Wheat Seeds",
categoryId: "1",
quantity: 250,
unitId: "2",
lastUpdated: "2023-03-20",
statusId: "2",
},
];
}
}
export async function createInventoryItem(
item: Omit<CreateInventoryItemInput, "id" | "lastUpdated" | "status">
): Promise<InventoryItem> {
try {
const response = await axiosInstance.post<InventoryItem>(
"/inventory",
item
);
return response.data;
} catch (error) {
console.error("Error while creating Inventory Item! " + error);
throw new Error("Failed to create inventory item: " + error);
}
}
export async function deleteInventoryItem(id: string) {
try {
const response = await axiosInstance.delete<InventoryItem>(
"/inventory/" + id
);
return response.data;
} catch (error) {
console.error("Error while deleting Inventory Item! " + error);
throw new Error("Failed to deleting inventory item: " + error);
}
}