Skip to main content

Overview

The AdminContext provides product management functionality for administrators. It handles creating, reading, updating, and deleting products through a REST API, with built-in loading states and user-friendly confirmation dialogs. Source: ~/workspace/source/src/context/AdminContext.jsx

Setup

Wrap admin routes with the AdminProvider:

Usage

Access the admin context in any component:

State Properties

productos

array
Complete product catalog fetched from the API. Automatically loaded on mount and refreshed after CRUD operations.

loading

boolean
Loading state while fetching products. Initially true, becomes false after 2-second delay.

open

boolean
Controls visibility of the “Add Product” modal.

setOpen

function
Toggle the “Add Product” modal visibility.

openEditor

boolean
Controls visibility of the “Edit Product” modal.

setOpenEditor

function
Toggle the “Edit Product” modal visibility.

seleccionado

object | null
Currently selected product for editing. null when no product is selected.

setSeleccionado

function
Set the product to be edited.

Methods

agregarProducto

Creates a new product in the database via POST request.
object
required
Product object with the following properties:
Product Object Properties:
  • nombre (string, required) - Product name
  • precio (number, required) - Product price
  • descripcion (string, required) - Product description
  • imagen (string, optional) - Image URL
  • stock (number, optional) - Available stock
Returns: Promise<void> Behavior:
  1. Sends POST request to API
  2. Shows success alert with SweetAlert2
  3. Automatically reloads product list
  4. Handles errors with console logging
Example:
src/layout/Admin.jsx
Form Component Example:
src/components/FormularioProducto.jsx

actualizarProducto

Updates an existing product via PUT request.
object
required
Complete product object including id and all updated fields.
Required Properties:
  • id (string, required) - Product ID to update
  • All other product fields to update
Returns: Promise<void> Behavior:
  1. Sends PUT request to API endpoint /productos/{id}
  2. Shows success alert with SweetAlert2
  3. Closes editor modal
  4. Clears selected product
  5. Reloads product list
Example:
src/layout/Admin.jsx

eliminarProducto

Deletes a product after user confirmation via DELETE request.
string
required
Product ID to delete.
Returns: Promise<void> Behavior:
  1. Shows confirmation dialog with SweetAlert2
  2. If confirmed:
    • Sends DELETE request to API
    • Shows success message
    • Reloads product list
  3. If cancelled: no action taken
  4. On error: shows error alert
Example:
src/layout/Admin.jsx
Confirmation Dialog: The delete method automatically shows this confirmation:

Internal Methods

cargarProductos

Internal method that fetches products from the API. Called automatically after CRUD operations. Not exposed in context value - used internally only.

API Endpoint

All operations use the MockAPI endpoint:
API Operations:
  • GET /productos - Fetch all products
  • POST /productos - Create new product
  • PUT /productos/{id} - Update product
  • DELETE /productos/{id} - Delete product

Complete Admin Panel Example

src/layout/Admin.jsx

User Feedback

All CRUD operations provide user feedback via SweetAlert2: Add Product Success:
Update Product Success:
Delete Confirmation:
Delete Success:
Delete Error:

Error Handling

All methods include try-catch blocks:
  • Network errors are logged to console
  • User-facing errors show SweetAlert2 dialogs
  • Failed operations don’t reload the product list
  • The UI remains stable even when operations fail