Products module handles everything related to products: rendering the clickable product grid on the sales screen, managing the full inventory list with edit and delete actions, and the product create/update form. It reads from and writes to Storage and calls Sales.addToCart() when a product card is tapped.
Properties
| Property | Type | Description |
|---|---|---|
currentProducts | array | In-memory cache of all products, loaded from Storage. Updated on every loadProducts() call. |
searchTerm | string | The current inventory search filter string. Updated by the #searchInventory input. Defaults to ''. |
Methods
Products.init()
Loads products from storage and binds all DOM event listeners. Called once by App.init().
Products.loadProducts()
Fetches the products array from Storage.getProducts(), stores it in currentProducts, and calls render() to update the sales view grid.
Products.bindEvents()
Attaches event listeners to:
#btnAddProduct— opens the product form for creating a new product.#productForm— handles form submission viasaveProduct().#searchInventory— updatessearchTermand callsrenderInventory()on each keystroke.#inventoryList— delegates clicks on.btn-editand.btn-deletebuttons toeditProduct()anddeleteProduct().
Products.render(searchTerm)
Renders the product grid (#productsGrid) used on the sales view. Filters currentProducts against searchTerm, matching on product name (nombre) or barcode (codigo). Each card gets a click listener that calls Sales.addToCart(product).
Shows a “No se encontraron productos” message if no products match the filter.
Text to filter products by. Matched case-insensitively against
nombre and codigo.Products.renderInventory()
Renders the inventory list (#inventoryList) used on the products management view. Filters using the module-level this.searchTerm. Each row shows the product name, barcode, price, and edit/delete action buttons.
Products.showProductForm(product)
Opens the product modal. If product is provided, pre-fills the form fields for editing and sets the title to “Editar Producto”. If no product is passed, resets the form and sets the title to “Agregar Producto”.
The product to edit. Pass
null (or omit) to open the form in create mode.Products.saveProduct()
Reads the current form values (codigo, nombre, precio), validates that all fields are present and precio is a non-negative number, then calls either Storage.updateProduct() (if an id is set) or Storage.addProduct(). Reloads the product list, re-renders the inventory, shows a toast, and closes the product modal.
Shows an error toast and returns early if validation fails.
Products.editProduct(id)
Finds the product with the given id in currentProducts and calls showProductForm(product) to open the edit modal.
The id of the product to edit.
Products.deleteProduct(id)
Prompts the user to confirm deletion. On confirmation, calls Storage.deleteProduct(id), shows a success toast, and reloads the product list and inventory.
The id of the product to delete.
Products.search(term)
Filters the sales view product grid by calling render(term). Called by Sales.bindEvents() on the #searchProducts input (with a 300 ms debounce).
The search string to pass to
render().Products.escapeHtml(text)
Sanitizes a string to prevent XSS when injecting user-provided content into HTML. Creates a temporary <div> element, assigns the text as textContent, and returns innerHTML.
The raw string to sanitize.
The HTML-escaped string safe for insertion into the DOM.