Linking backend data to frontend

This commit is contained in:
iv_vuytsik
2025-10-15 19:49:19 +03:00
parent ea1f50c1b8
commit 2b19ed246b
28 changed files with 959 additions and 385 deletions

View File

@@ -1,6 +1,6 @@
'use client'
import React from 'react'
import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import Sidebar from '../ui/Sidebar'
import useNavigationStore from '../../app/store/navigationStore'
@@ -8,14 +8,34 @@ import ChartCard from './ChartCard'
import AreaChart from './AreaChart'
import BarChart from './BarChart'
import DetectorChart from './DetectorChart'
import detectorsData from '../../data/detectors.json'
const Dashboard: React.FC = () => {
const router = useRouter()
const { currentObject, setCurrentSubmenu, closeMonitoring, closeFloorNavigation, closeNotifications } = useNavigationStore()
const objectId = currentObject?.id
const objectTitle = currentObject?.title
const [detectorsArray, setDetectorsArray] = useState<any[]>([])
useEffect(() => {
const loadDetectors = async () => {
try {
const res = await fetch('/api/get-detectors', { cache: 'no-store' })
if (!res.ok) return
const payload = await res.json()
console.log('[Dashboard] GET /api/get-detectors', { status: res.status, payload })
const detectorsData = payload?.data?.detectors ?? {}
const arr = Object.values(detectorsData).filter(
(detector: any) => (objectId ? detector.object === objectId : true)
)
setDetectorsArray(arr as any[])
} catch (e) {
console.error('Failed to load detectors:', e)
}
}
loadDetectors()
}, [objectId])
const handleBackClick = () => {
router.push('/objects')
}
@@ -39,10 +59,6 @@ const Dashboard: React.FC = () => {
}>
}
const detectorsArray = Object.values(detectorsData.detectors).filter(
(detector: DetectorData) => objectId ? detector.object === objectId : true
)
// Статусы
const statusCounts = detectorsArray.reduce((acc: { critical: number; warning: number; normal: number }, detector: DetectorData) => {
if (detector.status === '#b3261e') acc.critical++