Documentation Index
Fetch the complete documentation index at: https://mintlify.com/betovildoza/tiendaetca/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheCartContext provides centralized state management for the shopping cart, product catalog, and search functionality. It handles cart operations like adding, removing, and updating products, as well as fetching products from the API.
Source: ~/workspace/source/src/context/CartContext.jsx
Setup
Wrap your application with theCartProvider to make the context available to all components:
Usage
Access the cart context in any component:State Properties
cart
Array of product objects currently in the shopping cart. Each item includes the product data plus a
cantidad (quantity) property.productos
Complete product catalog fetched from the API. Updated on component mount.
productosFiltrados
Filtered product list based on the current search query (
busqueda). Automatically filters by product name (case-insensitive).cartCount
Number of unique items in the cart (not total quantity).
isCartOpen
Controls the visibility of the cart drawer/modal.
setCartOpen
Function to toggle cart drawer visibility.
busqueda
Current search query string.
setBusqueda
Update the search query. Automatically filters
productosFiltrados.carga
Loading state while fetching products from the API. Initially
true, becomes false after 2-second delay.error
Error state if product fetching fails.
Methods
handleAddToCart
Adds a product to the cart or increments quantity if already present. Displays toast notifications.Product object to add. Must include
cantidad property set to 1 for new additions.- If product exists in cart: increments
cantidadby 1 - If product is new: adds to cart with specified
cantidad - Shows success toast for new items
- Shows info toast for quantity increases
src/components/Product.jsx
eliminarProducto
Completely removes a product from the cart regardless of quantity.Product object with
id property to identify which item to remove.src/components/Cart.jsx
borrarProducto
Decrements the quantity of a product by 1. Removes the product entirely if quantity reaches 0.Product object with
id and cantidad properties.- If
cantidad > 1: decrements by 1 - If
cantidad === 1: removes product from cart
src/components/Cart.jsx
vaciarCarrito
Clears all items from the cart. No parameters Example:src/components/Cart.jsx
Complete Usage Example
Here’s a full example from the Cart component showing all cart operations:src/components/Cart.jsx
API Endpoint
The context fetches products from:Implementation Details
- Toast Notifications: Uses
react-toastifyfor add-to-cart feedback - Local State: Cart state is stored in component state (not persisted)
- Auto-filtering: Search filter updates automatically when
busquedachanges - Error Handling: Displays error message if API fetch fails