Overview
TheAdminContext 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 theAdminProvider:
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:
nombre(string, required) - Product nameprecio(number, required) - Product pricedescripcion(string, required) - Product descriptionimagen(string, optional) - Image URLstock(number, optional) - Available stock
Promise<void>
Behavior:
- Sends POST request to API
- Shows success alert with SweetAlert2
- Automatically reloads product list
- Handles errors with console logging
src/layout/Admin.jsx
src/components/FormularioProducto.jsx
actualizarProducto
Updates an existing product via PUT request.object
required
Complete product object including
id and all updated fields.id(string, required) - Product ID to update- All other product fields to update
Promise<void>
Behavior:
- Sends PUT request to API endpoint
/productos/{id} - Shows success alert with SweetAlert2
- Closes editor modal
- Clears selected product
- Reloads product list
src/layout/Admin.jsx
eliminarProducto
Deletes a product after user confirmation via DELETE request.string
required
Product ID to delete.
Promise<void>
Behavior:
- Shows confirmation dialog with SweetAlert2
- If confirmed:
- Sends DELETE request to API
- Shows success message
- Reloads product list
- If cancelled: no action taken
- On error: shows error alert
src/layout/Admin.jsx
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:GET /productos- Fetch all productsPOST /productos- Create new productPUT /productos/{id}- Update productDELETE /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: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