Overview
Pagosapp uses React’s Context API for global state management, with localStorage for persistence. The state architecture is simple yet effective, avoiding the complexity of external state management libraries.ItemsContext
Location:src/states/ItemsContext.jsx
The central state management system for all payment items.
Context Creation
Provider Implementation
State Structure
Each payment item has the following shape:Context API Methods
addItem(item)
Adds a new payment item to the collection:- Creates a copy of the item
- If
mesescontains ‘mensual’, setsmensual: trueand clearsmeses - Otherwise, sets
mensual: false - Appends to items array
removeItem(item)
Removes a payment item from the collection:Data Persistence
Automatic synchronization with localStorage:- Every state change triggers localStorage update
- On mount, loads from localStorage or falls back to
paymentsConstants - Data persists across browser sessions
usePago Hook
Location:src/logic/usePago.js
Custom hook for managing individual payment state:
State Keys
Per-payment, per-month state stored in localStorage:checked-{mes}-{nombre}: Boolean string, payment completion statusstartDate-{mes}-{nombre}: ISO date string, due datepaymentDate-{mes}-{nombre}: ISO date string, actual payment date
Computed Values
dateDifference
Calculates days until due date:src/utils/dateUtils.js):
- Positive number: Days until due date
- Negative number: Days overdue
- Used for color coding in UI
Default Data
Location:src/constants/payments.constants.js
Initial payment items loaded on first run:
- Loaded when localStorage is empty
- Provides sensible defaults for first-time users
- Can be customized by modifying this file
State Flow Diagram
Best Practices
Consuming Context
Always use theuseItems hook instead of useContext(ItemsContext):
State Updates
Always create new arrays/objects for immutability:localStorage Keys
Use consistent naming convention:Potential Improvements
- Add unique IDs: Use UUIDs instead of object references for removal
- Optimize localStorage: Debounce writes to reduce I/O
- Type safety: Add TypeScript for better type checking
- State normalization: Use normalized state shape for better performance
- Action creators: Centralize state update logic
- IndexedDB: For handling larger datasets
- State persistence library: Consider redux-persist or zustand for more robust persistence