Introduction to Grocery Inventory Apps
Managing grocery inventory can be a daunting task, but with the right tools, it can be simplified. A grocery inventory app allows users to track their food supplies efficiently. In this post, we will explore how to build a grocery inventory application using React, a popular JavaScript library.
Setting Up Your React Environment
Before starting on your grocery inventory app, ensure you have Node.js installed on your system. You can create your React application using create-react-app
, which provides a powerful starter kit. Run the following command in your terminal:
npm create vite@latest
This command generates a new React application directory with a basic structure, allowing you to focus on building the features of your grocery inventory app.
App.jsx
// src/Inventory.js import React, { useState } from 'react'; import './App.css'; const App = () => { const [items, setItems] = useState([]); const [itemName, setItemName] = useState(''); const [itemQuantity, setItemQuantity] = useState(''); const [itemCost, setItemCost] = useState(''); const addItem = () => { if (itemName && itemQuantity && itemCost) { setItems([...items, { name: itemName, quantity: parseInt(itemQuantity), cost: parseFloat(itemCost) }]); setItemName(''); setItemQuantity(''); setItemCost(''); } }; const deleteItem = (index) => { const newItems = items.filter((_, i) => i !== index); setItems(newItems); }; const getTotalQuantity = () => { return items.reduce((total, item) => total + item.quantity, 0); }; const getTotalCost = () => { return items.reduce((total, item) => total + (item.quantity * item.cost), 0).toFixed(2); }; const handlePrint = () => { const printContent = document.getElementById('printable-receipt').innerHTML; const originalContent = document.body.innerHTML; document.body.innerHTML = printContent; window.print(); document.body.innerHTML = originalContent; window.location.reload(); // Reload the page to restore original content }; return ( <div className="inventory"> <h1>Grocery Inventory</h1> <div className="add-item"> <input type="text" placeholder="Item Name" value={itemName} onChange={(e) => setItemName(e.target.value)} /> <input type="number" placeholder="Quantity" value={itemQuantity} onChange={(e) => setItemQuantity(e.target.value)} /> <input type="number" placeholder="Cost per Item" value={itemCost} onChange={(e) => setItemCost(e.target.value)} /> <button onClick={addItem}>Add Item</button> </div> <table> <thead> <tr> <th>Item Name</th> <th>Quantity</th> <th>Cost per Item</th> <th>Total Cost</th> <th>Actions</th> </tr> </thead> <tbody> {items.map((item, index) => ( <tr key={index}> <td>{item.name}</td> <td>{item.quantity}</td> <td>${item.cost.toFixed(2)}</td> <td>${(item.quantity * item.cost).toFixed(2)}</td> <td> <button onClick={() => deleteItem(index)}>Delete</button> </td> </tr> ))} </tbody> </table> <div className="total-quantity"> <h2>Total Quantity: {getTotalQuantity()}</h2> <h2>Total Cost: ${getTotalCost()}</h2> </div> <button onClick={handlePrint} className="print-button">Print Receipt</button> {/* Hidden printable receipt */} <div id="printable-receipt" className="printable-receipt"> <h2>Grocery Inventory Receipt</h2> <table> <thead> <tr> <th>Item</th> <th>Qty</th> <th>Cost</th> <th>Total</th> </tr> </thead> <tbody> {items.map((item, index) => ( <tr key={index}> <td>{item.name}</td> <td>{item.quantity}</td> <td>${item.cost.toFixed(2)}</td> <td>${(item.quantity * item.cost).toFixed(2)}</td> </tr> ))} </tbody> </table> <div className="total"> <p>Total Quantity: {getTotalQuantity()}</p> <p>Total Cost: ${getTotalCost()}</p> </div> </div> </div> ); }; export default App;
App.css
/* src/Inventory.css */ .inventory { text-align: center; margin-top: 50px; } .add-item { margin-bottom: 20px; } .add-item input { margin-right: 10px; padding: 10px; font-size: 16px; } .add-item button { padding: 10px 20px; font-size: 16px; background-color: #28a745; color: white; border: none; cursor: pointer; } .add-item button:hover { background-color: #218838; } table { width: 80%; margin: 0 auto; border-collapse: collapse; } th, td { border: 1px solid #ddd; padding: 8px; } th { background-color: #f2f2f2; font-size: 18px; } td { font-size: 16px; } tbody tr:nth-child(even) { background-color: #f9f9f9; } tbody tr:hover { background-color: #f1f1f1; } td button { padding: 5px 10px; font-size: 16px; background-color: #dc3545; color: white; border: none; cursor: pointer; } td button:hover { background-color: #c82333; } .total-quantity { margin-top: 20px; font-size: 20px; } .print-button { margin-top: 20px; padding: 10px 20px; font-size: 16px; background-color: #007bff; color: white; border: none; cursor: pointer; } .print-button:hover { background-color: #0056b3; } /* Print styles */ @media print { .add-item, .print-button, .inventory table { display: none; } .printable-receipt { display: block; text-align: left; width: 300px; /* Adjust width for POS receipt */ margin: 0 auto; } .printable-receipt table { width: 100%; border-collapse: collapse; } .printable-receipt th, .printable-receipt td { font-size: 12px; border: none; padding: 4px; } .printable-receipt .total { margin-top: 10px; font-size: 14px; } }
i have attached the video link below. which will do this tutorials step by step.