Component Hierarchy
The React application follows a simple, hierarchical component structure:Root Component: App
TheApp component serves as the central hub for state management and coordination between child components.
State Management
Located insrc/client/index.jsx:13:
route- Object containing the calculated route data (array of stops)
The application uses React class components with local state management. No external state management libraries (Redux, MobX) are used.
Key Methods
findRoute(routeData)
findRoute(routeData)
Purpose: Sends route request to backend and updates state with responseLocation: Flow:
src/client/index.jsx:53-67- Makes POST request to
/routeendpoint - Sends
routeDatacontainingfromandtocoordinates - Updates state with response
- Triggers map marker update
updateMarkers(stops)
updateMarkers(stops)
Purpose: Visualizes the route on Google MapsLocation: Flow:
src/client/index.jsx:18-51- Clears existing markers from map
- Creates new markers for each stop
- Configures waypoints for Google Directions API
- Renders optimized route on map
RouteForm Component
Manages user input for origin and destination coordinates. Location:src/client/RouteForm.jsx
State Structure
Component Interface
onFind- Callback function invoked when user clicks “Buscar ruta” button
Change Handler
TheonChange method updates nested state immutably:
Result Component
Displays the calculated route as a human-readable list of instructions. Location:src/client/Result.jsx
Route Beautification
Before display, the component simplifies the route by removing intermediate stops on the same branch:This algorithm condenses routes like “Stop A → Stop B → Stop C” (all same branch) into “Stop A → Stop C” for better readability.
Display Logic
Google Maps Integration
Google Maps is initialized globally inindex.jsx:82-93:
The map is initialized in the global scope rather than within the React component. In a production application, consider integrating this more tightly with React lifecycle methods.
Google Maps APIs Used
- Maps JavaScript API - Core map rendering
- Directions API - Route polyline rendering
- Markers API - Bus stop visualization
Component Communication Pattern
The application uses a props-down, events-up pattern:
Example Flow:
Build Configuration
The Webpack configuration bundles the React application: Entry Point:src/client/index.jsxOutput:
public/main.js and public/vendors.js
Key Design Decisions
Why Class Components?
The application uses class components instead of hooks:- Written before React Hooks were widely adopted
- State management needs are simple
- No need for complex side effects
Global Map Instance
Google Maps objects are global variables:- Simpler integration with external library
- Avoids React ref complexity
- Trade-off: harder to test and less idiomatic React
Minimal State Management
Only the route is stored in React state:- Form state is local to RouteForm
- Map state is managed by Google Maps
- Reduces complexity for this small application
For a larger application, consider using React Context or a state management library to avoid prop drilling.