Price Estimation Engine
Relevant source files
The following files were used as context for generating this wiki page:
The Price Estimation Engine is a core module within the Wasalny landing page responsible for calculating trip costs based on user-defined parameters. It facilitates a seamless booking experience by providing real-time price breakdowns for various transportation services, including travel routes (e.g., Damietta to Cairo/Airports) and internal Damietta routes.
The engine operates by processing trip details—such as origin, destination, vehicle category, and trip type (one-way or round-trip)—against a predefined pricing dataset. The resulting calculations are then formatted for display and used to generate pre-filled WhatsApp messages for booking confirmation.
Sources: src/utils/pricingCalculator.ts:1-10, src/hooks/usePricingCalculator.ts:15-25
Core Architecture and Data Flow
The engine is built on a modular architecture that separates state management, calculation logic, and UI representation. The data flow follows a unidirectional pattern: user inputs are captured via React state, processed through a custom hook, calculated by utility functions, and finally rendered in the UI.
Logic Flow Diagram
The following diagram illustrates the lifecycle of a price estimation request, from user input to the generation of a WhatsApp booking link.
State Management and Hooks
The usePricingCalculator hook serves as the primary controller for the engine. It manages the complex state of the pricing form, including route types (travel vs internal), location selections, and passenger counts.
Key Hook Responsibilities
- Location Filtering: Dynamically updates available "To" locations based on the selected "From" location and route type.
- Automatic Detection: Uses
detectRouteTypeto automatically switch between travel and internal modes based on selection. - State Persistence: Synchronizes user selections with
localStorageto preserve progress across sessions. - Smart Vehicle Switching: Automatically suggests or switches to a suitable vehicle category if the passenger count exceeds the current vehicle's capacity.
Sources: src/hooks/usePricingCalculator.ts:33-75, src/hooks/usePricingCalculator.ts:145-165
Pricing Logic and Calculation
The calculation logic is encapsulated in the calculatePrice function. Unlike dynamic distance-based pricing, this engine utilizes a fixed-rate model based on route groups and vehicle categories.
Calculation Parameters
| Parameter | Type | Description |
|---|---|---|
fromLocation | string | The ID of the starting location. |
toLocation | string | The ID of the destination. |
vehicleCategory | VehicleCategory | The selected vehicle (e.g., sedan, suv, minibus). |
passengerCount | number | Total number of passengers for capacity validation. |
isRoundTrip | boolean | Flag to determine if a round-trip rate applies. |
Sources: src/utils/pricingCalculator.ts:12-20, src/utils/pricingCalculator.ts:37-55
Implementation Detail: Fixed Rate Calculation
The engine retrieves a routeGroup based on the origin and destination. It then accesses the specific price for the selected vehicle and trip type.
// src/utils/pricingCalculator.ts:70-73
const tripType = isRoundTrip ? 'roundTrip' : 'oneWay';
const basePrice = routeGroup.pricing[vehicleCategory][tripType];
const total = basePrice; // Final total for the selected route
UI Components and Integration
The estimation engine is integrated into the frontend through two primary components: the PricingCalculator section and the PriceBreakdown card.
PricingCalculator Section
This component acts as the layout container. It organizes the input fields into logical groups:
- Route Type Toggle: Switches between 'Travel' and 'Internal' modes.
- RouteSelectionCard: Handles location dropdowns.
- VehiclePassengerCard: Manages vehicle types and passenger limits.
- DateTimeCard: Captures scheduling information and round-trip toggles.
Sources: src/components/sections/PricingCalculator.tsx:40-100
PriceBreakdown Component
The PriceBreakdown component provides a detailed view of the calculation result. It displays the base price, vehicle type, and passenger count. Crucially, it renders any calculation warnings (e.g., capacity issues) and the final CTA button.
Sources: src/components/ui/PriceBreakdown.tsx:35-60
Booking Integration
The final output of the engine is a formatted WhatsApp message. This is generated by the generateWhatsAppMessage function, which compiles the trip details into a human-readable Arabic text.
Booking Workflow Sequence
Conclusion
The Price Estimation Engine provides a robust and user-friendly interface for calculating travel costs. By leveraging a centralized pricing utility and a reactive state hook, it ensures that users receive accurate, real-time feedback on their trip planning. Its integration with WhatsApp serves as the primary conversion point for the Wasalny service, bridging the gap between automated estimation and manual booking confirmation.