Dashboard
The dashboard is a key component of the front-end application, providing users with an overview of tracker statistics, map visualization, and a table of trackers with filtering and pagination.
Features
- Status Filtering: Filter trackers by status (In Transit, In Storage, Delivered, Created)
- Statistics Display: View counts of trackers by status
- Map Visualization: Interactive map showing tracker locations
- Recent Activity: Display of recent tracker activity
- Tracker Table: Table of trackers with search, filtering, and pagination
Architecture
The dashboard was refactored to use a feature-based approach, with the following structure:
/features
/dashboard
/components
StatusFilter.tsx
StatsGrid.tsx
TrackerMap.tsx
RecentActivity.tsx
TrackerTable/
index.tsx
SearchBar.tsx
TableHeader.tsx
TableRow.tsx
Pagination.tsx
/hooks
useTrackerFilters.tsx
useTrackerStats.tsx
useTrackerQueries.tsx
/context
DashboardContext.tsx
index.tsx // Main DashboardPage
Implementation Details
Context
The dashboard uses React Context to manage state and provide it to all components. The context includes:
- State: Selected statuses, current page, items per page, search terms
- Data: Trackers, filtered trackers, paginated trackers, status counts, filtered stats
- Loading/Error States: Loading and error states for data fetching
- Actions: Functions to toggle status, set current page, set items per page, set search terms
Components
StatusFilter
The StatusFilter component displays a set of status filter buttons that allow users to filter trackers by status. It uses the context to get the selected statuses and toggle them.
StatsGrid
The StatsGrid component displays statistics about the trackers, including total count, in transit count, delivered count, and in storage count. It uses the context to get the filtered stats.
TrackerMap
The TrackerMap component displays an interactive map showing tracker locations. It uses Leaflet to render the map and markers for each tracker. The map is responsive and can be zoomed and panned.
RecentActivity
The RecentActivity component displays a list of recent tracker activity, such as status changes and location updates. It uses the context to get the trackers and their recent activity.
TrackerTable
The TrackerTable component displays a table of trackers with search, filtering, and pagination. It includes:
- SearchBar: Search for trackers by name, advertisement key, brand, or status
- TableHeader: Column headers with sorting functionality
- TableRow: Row for each tracker with details and actions
- Pagination: Controls for navigating between pages of trackers
Refactoring Benefits
The dashboard was refactored from a large, monolithic component (600+ lines) to a set of smaller, focused components. This refactoring provides several benefits:
- Separation of Concerns: Each component has a single responsibility
- Improved Maintainability: Smaller files are easier to understand and modify
- Better Testability: Components can be tested in isolation
- Enhanced Reusability: Components like StatusFilter could be reused elsewhere
- Centralized State Management: Context API provides a single source of truth
- Reduced Prop Drilling: Components access state directly through context
- Simplified Main Component: DashboardPage becomes a simple composition of components
Implementation Strategy
The refactoring was implemented using the following strategy:
- Create the context and provider
- Implement one component at a time, starting with simpler ones
- Test each component as it's implemented
- Gradually replace parts of the original DashboardPage with the new components
- Once all components are working, replace the entire DashboardPage
This incremental approach minimized risk and allowed for easier debugging if issues arose.
Future Improvements
Potential future improvements to the dashboard include:
- Real-time Updates: Implement WebSocket or polling for real-time tracker updates
- Advanced Filtering: Add more advanced filtering options, such as date ranges and custom fields
- Customizable Layout: Allow users to customize the dashboard layout
- Export Functionality: Add the ability to export tracker data to CSV or PDF
- Saved Filters: Allow users to save and reuse filter configurations