Overview
The application layer provides React hooks that encapsulate business logic and state management. These hooks provide a clean interface for components to interact with order data and theme state.useOrders
The primary hook for accessing and managing the global order state. Provides access to all orders, status-grouped orders, and order update functionality. Source:contexts/Orders.context.tsx:108
Signature
Returns
Array of all orders in the system
Async function to update an order’s status with optimistic UI updatesParameters:
id(string): Order ID to updatestatus(OrderStatus): New status to set
Orders grouped by their status for easy rendering in columns
true during initial order load, false after orders are loadedError message if initial load fails, or
null if no errorUsage
The
useOrders hook must be used within an OrdersProvider. It connects to real-time WebSocket updates automatically and implements optimistic UI updates with rollback on error.useTheme
Hook for accessing and controlling the application theme (light/dark mode). Source:contexts/Theme.context.tsx:42
Signature
Returns
Current theme mode
Function to toggle between light and dark themes
Usage
The
useTheme hook must be used within a ThemeProvider. Theme preference is persisted to localStorage automatically.useOrderDetail
A React hook that fetches and manages the state of a single order’s detailed information.Signature
Parameters
The unique identifier of the order to fetch. Pass
null to skip fetching.Returns
The order detail data, or
null if not yet loaded or if orderId is nulltrue while the order details are being fetched, false otherwiseError message if the fetch failed, or
null if no error occurredOrderDetailDto Structure
Name of the customer who placed the order
Phone number of the customer
Delivery address for the order
Special instructions or notes for the order
Name of the assigned courier
Usage Examples
Basic Usage
Conditional Fetching
With Loading States
Displaying Total Price
Behavior Details
The hook implements automatic cleanup to prevent state updates on unmounted components. This prevents React warnings about memory leaks.
Automatic Refetch
The hook automatically refetches order details when theorderId parameter changes:
Skipping Fetch
Passingnull as the orderId skips the fetch entirely:
Error Handling
The hook catches errors during fetch and provides them via theerror field:
Implementation Notes
Dependencies
The hook depends on:orderOrchestrator.getOrderDetail()from@/services/order.service- React’s
useStateanduseEffecthooks
State Management
The hook manages three pieces of state:- detail: The fetched order data
- isLoading: Loading indicator
- error: Error message if fetch fails
Cleanup
The hook implements proper cleanup using a cancellation flag:This cleanup mechanism ensures that if the component unmounts or the
orderId changes before the fetch completes, no state updates occur. This prevents “Can’t perform a React state update on an unmounted component” warnings.Type Safety
Related
- OrderRepository - Repository interface for order data
- OrderRealtime - Real-time order updates