Application Architecture
Tienda ETCA is built using a modern React architecture with a component-based structure. The application follows a clear separation of concerns with Context API for state management and React Router for navigation.Core Architecture Patterns
Component-Based
Modular UI components with clear responsibilities and reusable design
Context API
Centralized state management for cart, authentication, and admin operations
React Router
Client-side routing with protected routes and role-based access control
Vite Build
Fast development server and optimized production builds
Application Entry Point
The application initializes inmain.jsx with a hierarchical provider structure:
Provider Hierarchy
The nested provider structure ensures proper context availability:- Router - Enables routing throughout the application
- AuthProvider - Manages authentication state and user roles
- CartProvider - Handles shopping cart state and product data
- AdminProvider - Controls admin panel operations and product management
Routing Architecture
The routing system is defined inApp.jsx using React Router v7:
Route Protection
TheRutaProtegida component (~/workspace/source/src/auth/RutaProtegida.jsx) implements role-based access control:
Protected Route Implementation
Protected Route Implementation
- Unauthenticated users are redirected to login
- Users without required roles are redirected to home
- Admin routes are only accessible to admin users
State Management with Context API
The application uses three main contexts for state management:1. AuthContext
Location:~/workspace/source/src/context/AuthContext.jsx
Responsibilities:
- User authentication and login
- Role-based authorization (admin/cliente)
- Session persistence with localStorage
- Navigation after authentication
2. CartContext
Location:~/workspace/source/src/context/CartContext.jsx
Responsibilities:
- Shopping cart state management
- Product data fetching from API
- Cart operations (add, remove, update quantity)
- Product search and filtering
handleAddToCart(product)- Adds or increments product quantityborrarProducto(product)- Decrements quantity or removes itemeliminarProducto(product)- Removes product completelyvaciarCarrito()- Clears entire cart
3. AdminContext
Location:~/workspace/source/src/context/AdminContext.jsx
Responsibilities:
- Admin product management (CRUD operations)
- Product list state for admin panel
- Modal state for create/edit forms
- API interactions for product updates
agregarProducto(producto)- POST new productactualizarProducto(producto)- PUT update existing producteliminarProducto(id)- DELETE productcargarProductos()- GET all products
Component Structure
The application follows a clear component hierarchy:Layout Components
Layout Components
Purpose: Full page layouts
Home- Landing pageGaleriaProductos- Product listing pageDetallesProductos- Product detail pageAcercaDe- About pageContacto- Contact pageLogin- Authentication pageAdmin- Admin dashboard
UI Components
UI Components
Purpose: Reusable UI elements
Header- Navigation and cart iconFooter- Site footerCart- Shopping cart modalProduct- Individual product cardProductList- Product grid displayNotFound- 404 error page
Form Components
Form Components
Purpose: Data input and management
Formulario- Contact formFormularioProducto- New product formFormularioEdicion- Edit product formListaUsuarios- User management
Notification System
The application uses two notification libraries:React Toastify
Used for cart operations and user feedback:SweetAlert2
Used for admin confirmations and alerts:Data Flow
Key Design Decisions
Why Context API over Redux?
Why Context API over Redux?
Context API provides sufficient state management for this application size without the complexity of Redux. The three contexts (Auth, Cart, Admin) have clear boundaries and minimal prop drilling.
Why MockAPI for backend?
Why MockAPI for backend?
MockAPI provides a quick REST API for prototyping without backend infrastructure. It supports full CRUD operations suitable for this e-commerce demo.
Why localStorage for auth persistence?
Why localStorage for auth persistence?
Simple session persistence across page refreshes. Stores authentication status and user role for role-based access control.
Next Steps
Project Structure
Explore the detailed file and directory organization
Technologies
Learn about the tech stack and dependencies