API Integration Guide
This guide covers data loading patterns, API client usage, and refresh mechanisms for integrating with the Waldur MasterMind backend.
API Data Loading and Refresh Patterns
The application uses multiple approaches for loading data from REST APIs in forms and handling data refresh operations, showing evolution from legacy Redux patterns to modern React Query implementations.
Data Loading Patterns
React Query/TanStack Query (Modern Approach)
The preferred pattern for new components uses React Query for efficient data fetching:
1 2 3 4 5 6 7 8 9 10 |
|
Key Features:
- Automatic Caching: 5-minute stale time for most queries
- Built-in Loading States:
isLoading
,error
,data
- Manual Refresh:
refetch()
function for explicit updates - Query Invalidation: Cache invalidation through query keys
- Background Refetching: Automatic background updates
Custom Hook Pattern
Centralized data fetching logic wrapped in reusable hooks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Benefits:
- Business Logic Integration: Transforms data for UI consumption
- Computed Properties: Adds disabled states and tooltips
- Reusability: Shared across multiple components
- Centralized Error Handling: Consistent error management
Redux/Redux Saga Pattern (Legacy)
Used primarily for table data management:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Characteristics:
- Centralized State: Redux store for table data
- Automatic Pagination: Built-in pagination and filtering
- Request Cancellation: AbortController support
- Periodic Polling: Configurable refresh intervals
Data Refresh Mechanisms
CRUD Operations Refresh
Create Operations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Edit Operations:
1 2 3 4 5 6 |
|
Delete Operations:
1 2 3 4 5 6 |
|
Refresh Strategies
Strategy | Implementation | Use Case |
---|---|---|
Explicit Refetch | const { refetch } = useQuery(...); await refetch(); |
Manual refresh after CRUD operations |
Table Refresh Button | <TableRefreshButton onClick={() => props.fetch(true)} /> |
User-initiated refresh |
Automatic Polling | pullInterval in Redux saga |
Real-time data updates |
Query Invalidation | queryClient.invalidateQueries(['queryKey']) |
Cache invalidation |
Error Handling and Loading States
Consistent Error Display
1 2 3 4 5 6 7 8 9 10 |
|
Global Error Handling
1 2 3 4 5 6 7 8 9 |
|
API Integration Patterns
Waldur JS Client Integration
Primary API client with typed endpoints:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Async Form Field Loading
Dynamic data loading for form fields:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Caching Strategies
React Query Cache
- Query-based caching: Uses query keys for cache management
- Automatic background refetching: Keeps data fresh
- Configurable stale time: Typically 5 minutes for most queries
- Request deduplication: Prevents duplicate requests
Redux Store Cache
- Table data cached: In Redux state for tables
- Manual cache invalidation: Explicit cache clearing
- Optimistic updates: Immediate UI updates for CRUD operations
Best Practices
- New Components: Use React Query with custom hooks
- Error Handling: Consistent use of
LoadingErred
component with retry functionality - Caching: 5-minute stale time for most queries, longer for static data
- Refresh Strategy: Always call
refetch()
after successful CRUD operations - Loading States: Use
isLoading
state for UI feedback - API Integration: Prefer
waldur-js-client
over direct fetch calls - Form Validation: Use async validation with API dependency checking
This data loading architecture demonstrates the application's evolution toward modern React patterns while maintaining backward compatibility with existing table infrastructure and Redux-based components.
Migration Patterns
The application shows clear migration from Redux to React Query:
Aspect | Redux Pattern | React Query Pattern |
---|---|---|
Data Loading | Redux actions + sagas | useQuery hooks |
Caching | Redux store | Query cache |
Error Handling | Redux error actions | Query error states |
Loading States | Redux loading flags | isLoading state |
Refresh | Dispatch actions | refetch() function |
Polling | Saga intervals | Query refetch intervals |