Enhance farm dashboard components by refactoring to accept data props, improving API integration for dynamic data rendering. Updated FarmDashboardWrapper to fetch and manage card data, ensuring components like EconomicOverview, AnomalyDetectionCard, and others utilize the new data structure. Removed hardcoded values and added error handling for better resilience.

This commit is contained in:
2026-02-19 16:58:30 +03:30
parent 9f1de2166c
commit 51175ffac2
18 changed files with 333 additions and 313 deletions
+41 -3
View File
@@ -1,11 +1,13 @@
/**
* Farm Dashboard Config Service
* Handles API calls for dashboard customization (disabled cards, row order).
* Authenticated user required.
* Farm Dashboard Service
* Handles API calls for dashboard config and card data.
* - Config: disabled cards, row order, drag reorder
* - Cards: all 15 card payloads from /api/farm-dashboard/
*/
import { apiClient } from '../client'
import type { FarmDashboardConfig } from '@/views/dashboards/farm/farmDashboardConfig'
import type { CardId } from '@/views/dashboards/farm/farmDashboardConfig'
export interface ApiResponse<T> {
code: number
@@ -19,6 +21,25 @@ export interface FarmDashboardConfigResponse {
enable_drag_reorder?: boolean
}
/** API response shape for /api/farm-dashboard/ - each key matches CardId */
export interface FarmDashboardCardsResponse {
farmOverviewKpis?: Record<string, unknown>
farmWeatherCard?: Record<string, unknown>
farmAlertsTracker?: Record<string, unknown>
sensorValuesList?: Record<string, unknown>
sensorRadarChart?: Record<string, unknown>
sensorComparisonChart?: Record<string, unknown>
anomalyDetectionCard?: Record<string, unknown>
farmAlertsTimeline?: Record<string, unknown>
waterNeedPrediction?: Record<string, unknown>
harvestPredictionCard?: Record<string, unknown>
yieldPredictionChart?: Record<string, unknown>
soilMoistureHeatmap?: Record<string, unknown>
ndviHealthCard?: Record<string, unknown>
recommendationsList?: Record<string, unknown>
economicOverview?: Record<string, unknown>
}
const STORAGE_KEY = 'farm_dashboard_config'
/**
@@ -114,5 +135,22 @@ export const farmDashboardService = {
}
throw err
}
},
/**
* Get all dashboard card data from API
* Response: { code: 200, msg: "OK", data: { farmOverviewKpis, farmWeatherCard, ... } }
*/
async getAllCards(): Promise<Partial<Record<CardId, Record<string, unknown>>>> {
try {
const response = await apiClient.get<ApiResponse<FarmDashboardCardsResponse>>('/api/farm-dashboard/')
const raw = response?.data ?? response
if (raw && typeof raw === 'object') {
return raw as Partial<Record<CardId, Record<string, unknown>>>
}
return {}
} catch {
return {}
}
}
}