diff --git a/frontend/api/inventory.ts b/frontend/api/inventory.ts index e1fb1d2..1d0e786 100644 --- a/frontend/api/inventory.ts +++ b/frontend/api/inventory.ts @@ -3,6 +3,7 @@ import type { InventoryItem, InventoryItemStatus, InventoryItemCategory, + CreateInventoryItemInput, } from "@/types"; /** @@ -39,9 +40,11 @@ export async function fetchInventoryCategory(): Promise< export async function fetchInventoryItems(): Promise { try { - const response = await axiosInstance.get("/api/inventory"); + const response = await axiosInstance.get("/inventory"); return response.data; } catch (error) { + // console.error("Error while fetching inventory items! " + error); + // throw error; // Fallback dummy data return [ { @@ -100,18 +103,16 @@ export async function fetchInventoryItems(): Promise { * Note: The function accepts all fields except id, lastUpdated, and status. */ export async function createInventoryItem( - item: Omit + item: Omit ): Promise { - // Simulate network delay - await new Promise((resolve) => setTimeout(resolve, 500)); try { const response = await axiosInstance.post( - "/api/inventory", + "/inventory", item ); return response.data; } catch (error) { - console.error("Error while creating Inventory Item!" + error); + console.error("Error while creating Inventory Item! " + error); throw new Error("Failed to create inventory item: " + error); } } diff --git a/frontend/app/(sidebar)/inventory/add-inventory-item.tsx b/frontend/app/(sidebar)/inventory/add-inventory-item.tsx index 067b441..76a5522 100644 --- a/frontend/app/(sidebar)/inventory/add-inventory-item.tsx +++ b/frontend/app/(sidebar)/inventory/add-inventory-item.tsx @@ -60,23 +60,13 @@ export function AddInventoryItem({ const [itemUnit, setItemUnit] = useState(""); const [itemStatus, setItemStatus] = useState(""); - // const { - // data: inventoryItems = [], - // isLoading: isItemLoading, - // isError: isItemError, - // } = useQuery({ - // queryKey: ["inventoryItems"], - // queryFn: fetchInventoryItems, - // staleTime: 60 * 1000, - // }); - const mutation = useMutation({ mutationFn: (item: CreateInventoryItemInput) => createInventoryItem(item), onSuccess: () => { - // Invalidate queries to refresh inventory data. + // invalidate queries to refresh inventory data. const queryClient = useQueryClient(); queryClient.invalidateQueries({ queryKey: ["inventoryItems"] }); - // Reset form fields and close dialog. + // reset form fields and close dialog. setItemName(""); setItemCategory(""); setItemQuantity(0); @@ -86,9 +76,27 @@ export function AddInventoryItem({ }, }); + const inputStates = [itemName, itemCategory, itemUnit, itemStatus, date]; + const isInputValid = inputStates.every((input) => input); + const handleSave = () => { - // Basic validation (you can extend this as needed) - if (!itemName || !itemCategory || !itemUnit) return; + if (!isInputValid) { + console.error("All fields are required"); + return; + } + + const newItem: CreateInventoryItemInput = { + name: itemName, + categoryId: + inventoryCategory.find((item) => item.name === itemCategory)?.id || 0, + quantity: itemQuantity, + unitId: harvestUnits.find((item) => item.name === itemUnit)?.id || 0, + statusId: + inventoryStatus.find((item) => item.name === itemStatus)?.id || 0, + lastUpdated: date ? date.toISOString() : new Date().toISOString(), + }; + console.table(newItem); + mutation.mutate(newItem); }; return ( @@ -163,7 +171,7 @@ export function AddInventoryItem({ id="quantity" type="number" className="col-span-3" - value={itemQuantity} + value={itemQuantity === 0 ? "" : itemQuantity} onChange={(e) => setItemQuantity(Number(e.target.value))} /> diff --git a/frontend/app/(sidebar)/inventory/page.tsx b/frontend/app/(sidebar)/inventory/page.tsx index 9765528..4b0668a 100644 --- a/frontend/app/(sidebar)/inventory/page.tsx +++ b/frontend/app/(sidebar)/inventory/page.tsx @@ -22,11 +22,13 @@ import { TableHeader, TableRow, } from "@/components/ui/table"; +import { TriangleAlertIcon } from "lucide-react"; import { Pagination, PaginationContent, PaginationItem, } from "@/components/ui/pagination"; +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Search } from "lucide-react"; import { FaSort, FaSortUp, FaSortDown } from "react-icons/fa"; import { fetchHarvestUnits } from "@/api/harvest"; @@ -92,6 +94,8 @@ export default function InventoryPage() { ////////////////////////////// // console.table(inventoryItems); // console.table(inventoryStatus); + // console.table(harvestUnits); + const [searchTerm, setSearchTerm] = useState(""); const filteredItems = useMemo(() => { return inventoryItems @@ -195,6 +199,22 @@ export default function InventoryPage() { const isLoading = loadingStates.some((loading) => loading); const isError = errorStates.some((error) => error); + if (inventoryItems.length === 0) { + return ( +
+ +
+ + No Inventory Data + + You currently have no inventory items. Add a new item to get + started! + +
+
+
+ ); + } if (isLoading) return (
diff --git a/frontend/types.ts b/frontend/types.ts index ea753e7..fa0e53d 100644 --- a/frontend/types.ts +++ b/frontend/types.ts @@ -82,10 +82,14 @@ export type HarvestUnits = { name: string; }; -export type CreateInventoryItemInput = Omit< - InventoryItem, - "id" | "lastUpdated" | "status" ->; +export type CreateInventoryItemInput = { + name: string; + categoryId: number; + quantity: number; + unitId: number; + lastUpdated: string; + statusId: number; +}; export interface Blog { id: number;