"use client"; import { useEffect, useState } from "react"; import { Edit2, Package, RefreshCw, Trash2, Download } from "lucide-react"; import * as xlsx from "xlsx"; export default function Dashboard() { const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [editingId, setEditingId] = useState(null); const [editForm, setEditForm] = useState({}); const fetchItems = async () => { setLoading(true); setError(null); try { const res = await fetch("/api/inventory"); if (!res.ok) throw new Error("Failed to fetch inventory from database"); const data = await res.json(); setItems(data); } catch (err) { setError(err.message); } finally { setLoading(false); } }; useEffect(() => { fetchItems(); }, []); const deleteItem = async (id) => { if (!confirm("Are you sure you want to delete this item?")) return; try { const res = await fetch(`/api/inventory?id=${id}`, { method: "DELETE" }); if (res.ok) { setItems(items.filter(item => item.id !== id)); } else { alert("Error deleting item"); } } catch (error) { alert("Error deleting item: " + error.message); } }; const startEdit = (item) => { setEditingId(item.id); setEditForm({ name: item.name, category: item.category || "", quantity: item.quantity, price: item.price }); }; const saveEdit = async (id) => { try { const payload = { id, name: editForm.name, category: editForm.category, quantity: parseInt(editForm.quantity) || 0, price: parseFloat(editForm.price) || 0.0, }; const res = await fetch("/api/inventory", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (res.ok) { const updatedItem = await res.json(); setItems(items.map(item => item.id === id ? updatedItem : item)); setEditingId(null); } else { alert("Failed to save changes"); } } catch (err) { alert("Error saving: " + err.message); } }; const handleExport = () => { if (items.length === 0) return alert("No items to export"); const ws = xlsx.utils.json_to_sheet(items.map(i => ({ "QR ID": i.qrCodeId, "Name": i.name, "Quantity": i.quantity, "Price": i.price, "Category": i.category || "", "Description": i.description || "" }))); const wb = xlsx.utils.book_new(); xlsx.utils.book_append_sheet(wb, ws, "Inventory"); xlsx.writeFile(wb, "Inventory_Export.xlsx"); }; return (
Manage your inventory quantities and prices.
Error: {error}
Ensure your PostgreSQL database is running and DATABASE_URL is configured in your .env or prisma.config.ts
Loading inventory...
Start by adding a new item or importing an Excel sheet.
| Name | QR ID | Category | Quantity | Price | Actions |
|---|---|---|---|---|---|
| {editingId === item.id ? ( setEditForm({...editForm, name: e.target.value})} /> ) : item.name} | {item.qrCodeId} |
{editingId === item.id ? ( setEditForm({...editForm, category: e.target.value})} /> ) : (item.category || "-")} | {editingId === item.id ? ( setEditForm({...editForm, quantity: e.target.value})} /> ) : ( {item.quantity} )} | {editingId === item.id ? ( setEditForm({...editForm, price: e.target.value})} /> ) : `$${Number(item.price).toFixed(2)}`} |
{editingId === item.id ? (
<>
>
) : (
<>
>
)}
|