From ba8f754d8db79063a424677b6406cfa821c28546 Mon Sep 17 00:00:00 2001 From: THIS ONE IS A LITTLE BIT TRICKY KRUB Date: Tue, 1 Apr 2025 18:39:25 +0700 Subject: [PATCH] feat: add fetchInventoryCategory API and integrate into inventory management --- frontend/api/inventory.ts | 44 ++++++++++-------- .../inventory/edit-inventory-item.tsx | 46 ++++++++----------- frontend/app/(sidebar)/inventory/page.tsx | 39 +++++++++++++--- frontend/types.ts | 11 ++++- 4 files changed, 87 insertions(+), 53 deletions(-) diff --git a/frontend/api/inventory.ts b/frontend/api/inventory.ts index 1c59e20..35c1dd2 100644 --- a/frontend/api/inventory.ts +++ b/frontend/api/inventory.ts @@ -3,14 +3,15 @@ import type { InventoryItem, CreateInventoryItemInput, InventoryItemStatus, + InventoryItemCategory, } 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 { try { @@ -23,6 +24,19 @@ export async function fetchInventoryStatus(): Promise { return []; } } +export async function fetchInventoryCategory(): Promise< + InventoryItemCategory[] +> { + try { + const response = await axiosInstance.get( + "/inventory/category" + ); + return response.data; + } catch (error) { + console.error("Error fetching inventory status:", error); + return []; + } +} export async function fetchInventoryItems(): Promise { try { @@ -34,52 +48,47 @@ export async function fetchInventoryItems(): Promise { { id: 1, name: "Tomato Seeds", - category: "Seeds", - type: "Plantation", + category: "1", quantity: 500, unit: "packets", lastUpdated: "2023-03-01", - status: "In Stock", + status: "1", }, { id: 2, name: "NPK Fertilizer", - category: "Fertilizer", - type: "Fertilizer", + category: "3", quantity: 200, unit: "kg", lastUpdated: "2023-03-05", - status: "Low Stock", + status: "2", }, { id: 3, name: "Corn Seeds", - category: "Seeds", - type: "Plantation", + category: "1", quantity: 300, unit: "packets", lastUpdated: "2023-03-10", - status: "In Stock", + status: "3", }, { id: 4, name: "Organic Compost", - category: "Fertilizer", - type: "Fertilizer", + category: "3", quantity: 150, unit: "kg", lastUpdated: "2023-03-15", - status: "Out Of Stock", + status: "1", }, { id: 5, name: "Wheat Seeds", - category: "Seeds", - type: "Plantation", + category: "1", quantity: 250, unit: "packets", lastUpdated: "2023-03-20", - status: "In Stock", + status: "2", }, ]; } @@ -108,7 +117,6 @@ export async function createInventoryItem( id: Math.floor(Math.random() * 1000), name: item.name, category: item.category, - type: item.type, quantity: item.quantity, unit: item.unit, lastUpdated: new Date().toISOString(), diff --git a/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx b/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx index b5d2a7a..05a0c9e 100644 --- a/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx +++ b/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx @@ -33,6 +33,7 @@ import { SelectValue, } from "@/components/ui/select"; import { cn } from "@/lib/utils"; +import { InventoryItemStatus, InventoryItemCategory } from "@/types"; // import { updateInventoryItem } from "@/api/inventory"; // import type { UpdateInventoryItemInput } from "@/types"; @@ -41,9 +42,10 @@ export interface EditInventoryItemProps { name: string; category: string; status: string; - type: string; unit: string; quantity: number; + fetchedInventoryStatus: InventoryItemStatus[]; + fetchedInventoryCategory: InventoryItemCategory[]; } export function EditInventoryItem({ @@ -51,13 +53,13 @@ export function EditInventoryItem({ name, category, status, - type, unit, quantity, + fetchedInventoryStatus, + fetchedInventoryCategory, }: EditInventoryItemProps) { const [open, setOpen] = useState(false); const [itemName, setItemName] = useState(name); - const [itemType, setItemType] = useState(type); const [itemCategory, setItemCategory] = useState(category); const [itemQuantity, setItemQuantity] = useState(quantity); const [itemUnit, setItemUnit] = useState(unit); @@ -119,17 +121,20 @@ export function EditInventoryItem({
- - Type - Plantation - Fertilizer + Category + {fetchedInventoryCategory.map((categoryItem, _) => ( + + {categoryItem.name} + + ))} @@ -138,35 +143,22 @@ export function EditInventoryItem({ - Status - In Stock - Low Stock - Out Of Stock + {fetchedInventoryStatus.map((statusItem, _) => ( + + {statusItem.name} + + ))}
-
- - setItemCategory(e.target.value)} - /> -