Skip to content

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:

  1. Separation of Concerns: Each component has a single responsibility
  2. Improved Maintainability: Smaller files are easier to understand and modify
  3. Better Testability: Components can be tested in isolation
  4. Enhanced Reusability: Components like StatusFilter could be reused elsewhere
  5. Centralized State Management: Context API provides a single source of truth
  6. Reduced Prop Drilling: Components access state directly through context
  7. Simplified Main Component: DashboardPage becomes a simple composition of components

Implementation Strategy

The refactoring was implemented using the following strategy:

  1. Create the context and provider
  2. Implement one component at a time, starting with simpler ones
  3. Test each component as it's implemented
  4. Gradually replace parts of the original DashboardPage with the new components
  5. 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:

  1. Real-time Updates: Implement WebSocket or polling for real-time tracker updates
  2. Advanced Filtering: Add more advanced filtering options, such as date ranges and custom fields
  3. Customizable Layout: Allow users to customize the dashboard layout
  4. Export Functionality: Add the ability to export tracker data to CSV or PDF
  5. Saved Filters: Allow users to save and reuse filter configurations