Overview
The Products component (products.js) provides the interface for managing the product inventory. It allows users to add new products with name and price, view all existing products, and delete products from the system.
Responsibilities
- Display all products in the inventory
- Add new products to the database
- Delete products from the database
- Show empty state when no products exist
- Provide visual feedback for all operations
DOM Elements
#productForm- Form for adding new products#pName- Input field for product name#pPrice- Input field for product price#productList- Container for product cards#emptyState- Empty state message (shown when no products)
Functions
renderProducts()
Fetches and displays all products in a grid layout.- Fetches all products from database
- If empty, shows empty state message
- Otherwise, renders product cards with delete buttons
- Attaches delete event handlers to each product
Event Handlers
Product Form Submit
Handles adding new products to the inventory.- Prevents default form submission
- Calls
addProduct()with trimmed name and price - Shows success toast notification
- Clears form inputs
- Re-renders product list to show new product
Data Flow
Adding a Product
Deleting a Product
Dependencies
Database module providing:
addProduct(name, price)- Adds a new productgetProducts()- Retrieves all productsdeleteProduct(id)- Deletes a product by ID
Shared utilities providing:
money(n)- Formats numbers as currencyshowToast(message, type)- Displays notification toast
Data Structure
Product Object:Auto-Execution
The product list automatically loads when the module is imported:User Interactions
Adding a Product
- User enters product name in
#pNameinput - User enters product price in
#pPriceinput - User submits form (click button or press Enter)
- Product is saved to database
- Success toast appears
- Form is cleared
- Product list refreshes to show new product
Deleting a Product
- User clicks delete button (trash icon) on a product card
- Confirmation dialog appears: “Delete this product?”
- If user confirms:
- Product is removed from database
- Success toast appears
- Product list refreshes without the deleted product
- If user cancels, no action is taken
UI States
Empty State
When no products exist, the#emptyState element is shown and #productList is empty.
Populated State
When products exist:#emptyStateis hidden#productListcontains a grid of product cards- Each card shows product name, price, and delete button
- Cards have hover effects for better UX