Linked backend data to detectors' meshes.
This commit is contained in:
@@ -109,6 +109,26 @@ const AlertsPage: React.FC = () => {
|
||||
}
|
||||
}
|
||||
|
||||
const handleAcknowledgeToggle = async (alertId: number) => {
|
||||
try {
|
||||
const res = await fetch(`/api/update-alert/${alertId}`, { method: 'PATCH' })
|
||||
const payload = await res.json().catch(() => null)
|
||||
console.log('[AlertsPage] PATCH /api/update-alert', { id: alertId, status: res.status, payload })
|
||||
if (!res.ok) {
|
||||
throw new Error(typeof payload?.error === 'string' ? payload.error : `Update failed (${res.status})`)
|
||||
}
|
||||
// Обновить алерты
|
||||
const params = new URLSearchParams()
|
||||
if (currentObject.id) params.set('objectId', currentObject.id)
|
||||
const listRes = await fetch(`/api/get-alerts?${params.toString()}`, { cache: 'no-store' })
|
||||
const listPayload = await listRes.json().catch(() => null)
|
||||
const data = Array.isArray(listPayload?.data) ? listPayload.data : (listPayload?.data?.alerts || [])
|
||||
setAlerts(data as AlertItem[])
|
||||
} catch (e) {
|
||||
console.error('Failed to update alert:', e)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex h-screen bg-[#0e111a]">
|
||||
<Sidebar
|
||||
@@ -218,6 +238,12 @@ const AlertsPage: React.FC = () => {
|
||||
}`}>
|
||||
{item.acknowledged ? 'Да' : 'Нет'}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => handleAcknowledgeToggle(item.id)}
|
||||
className="ml-2 inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-[#2a2e3e] text-white hover:bg-[#353a4d]"
|
||||
>
|
||||
{item.acknowledged ? 'Снять' : 'Подтвердить'}
|
||||
</button>
|
||||
</td>
|
||||
<td className="py-4">
|
||||
<div className="text-sm text-gray-300">{new Date(item.timestamp).toLocaleString('ru-RU')}</div>
|
||||
|
||||
@@ -10,19 +10,21 @@ import DetectorMenu from '../../../components/navigation/DetectorMenu'
|
||||
import Notifications from '../../../components/notifications/Notifications'
|
||||
import NotificationDetectorInfo from '../../../components/notifications/NotificationDetectorInfo'
|
||||
import dynamic from 'next/dynamic'
|
||||
import type { ModelViewerProps } from '../../../components/model/ModelViewer'
|
||||
|
||||
const ModelViewer = dynamic(() => import('../../../components/model/ModelViewer'), {
|
||||
ssr: false,
|
||||
loading: () => (
|
||||
<div className="w-full h-full flex items-center justify-center bg-[#0e111a]">
|
||||
<div className="text-gray-300 animate-pulse">Загрузка 3D-модуля…</div>
|
||||
</div>
|
||||
),
|
||||
})
|
||||
const ModelViewer = dynamic<ModelViewerProps>(() => import('../../../components/model/ModelViewer'), {
|
||||
ssr: false,
|
||||
loading: () => (
|
||||
<div className="w-full h-full flex items-center justify-center bg-[#0e111a]">
|
||||
<div className="text-gray-300 animate-pulse">Загрузка 3D-модуля…</div>
|
||||
</div>
|
||||
),
|
||||
})
|
||||
|
||||
interface DetectorType {
|
||||
detector_id: number
|
||||
name: string
|
||||
serial_number: string
|
||||
object: string
|
||||
status: string
|
||||
checked: boolean
|
||||
@@ -122,6 +124,13 @@ const NavigationPage: React.FC = () => {
|
||||
}
|
||||
|
||||
const handleDetectorMenuClick = (detector: DetectorType) => {
|
||||
// Для тестов. Выбор детектора.
|
||||
console.log('[NavigationPage] Selected detector click:', {
|
||||
detector_id: detector.detector_id,
|
||||
name: detector.name,
|
||||
serial_number: detector.serial_number,
|
||||
})
|
||||
|
||||
if (selectedDetector?.detector_id === detector.detector_id && showDetectorMenu) {
|
||||
setShowDetectorMenu(false)
|
||||
setSelectedDetector(null)
|
||||
@@ -226,12 +235,7 @@ const NavigationPage: React.FC = () => {
|
||||
})()}
|
||||
|
||||
{showFloorNavigation && showDetectorMenu && selectedDetector && (
|
||||
<DetectorMenu
|
||||
detector={selectedDetector}
|
||||
isOpen={showDetectorMenu}
|
||||
onClose={closeDetectorMenu}
|
||||
getStatusText={getStatusText}
|
||||
/>
|
||||
null
|
||||
)}
|
||||
|
||||
<header className="bg-[#161824] border-b border-gray-700 px-6 py-4">
|
||||
@@ -263,6 +267,19 @@ const NavigationPage: React.FC = () => {
|
||||
modelPath='/static-models/AerBIM_Monitor_ASM_HT_Viewer_Expo2017Astana_Level_+1430_custom_prop.glb'
|
||||
onModelLoaded={handleModelLoaded}
|
||||
onError={handleModelError}
|
||||
focusSensorId={selectedDetector?.serial_number ?? null}
|
||||
renderOverlay={({ anchor }) => (
|
||||
selectedDetector && showDetectorMenu && anchor ? (
|
||||
<DetectorMenu
|
||||
detector={selectedDetector}
|
||||
isOpen={true}
|
||||
onClose={closeDetectorMenu}
|
||||
getStatusText={getStatusText}
|
||||
compact={true}
|
||||
anchor={anchor}
|
||||
/>
|
||||
) : null
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
73
frontend/app/api/get-dashboard/route.ts
Normal file
73
frontend/app/api/get-dashboard/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextResponse, NextRequest } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import { getToken } from 'next-auth/jwt'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
const authHeader = req.headers.get('authorization') || req.headers.get('Authorization')
|
||||
const bearer = authHeader && authHeader.toLowerCase().startsWith('bearer ') ? authHeader.slice(7) : undefined
|
||||
const secret = process.env.NEXTAUTH_SECRET
|
||||
const token = await getToken({ req, secret }).catch(() => null)
|
||||
|
||||
let accessToken = session?.accessToken || bearer || (token as any)?.accessToken
|
||||
const refreshToken = session?.refreshToken || (token as any)?.refreshToken
|
||||
|
||||
if (!accessToken && refreshToken) {
|
||||
try {
|
||||
const refreshRes = await fetch(`${process.env.BACKEND_URL}/auth/refresh/`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ refresh: refreshToken }),
|
||||
})
|
||||
if (refreshRes.ok) {
|
||||
const refreshed = await refreshRes.json()
|
||||
accessToken = refreshed.access
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
if (!accessToken) {
|
||||
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const backendUrl = process.env.BACKEND_URL
|
||||
if (!backendUrl) {
|
||||
return NextResponse.json({ success: false, error: 'BACKEND_URL is not configured' }, { status: 500 })
|
||||
}
|
||||
|
||||
const url = new URL(req.url)
|
||||
const timePeriod = url.searchParams.get('time_period')
|
||||
const qs = timePeriod ? `?time_period=${encodeURIComponent(timePeriod)}` : ''
|
||||
|
||||
const res = await fetch(`${backendUrl}/account/get-dashboard/${qs}`, {
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
cache: 'no-store',
|
||||
})
|
||||
|
||||
const text = await res.text()
|
||||
let payload: any
|
||||
try { payload = JSON.parse(text) } catch { payload = text }
|
||||
|
||||
if (!res.ok) {
|
||||
const err = typeof payload === 'string' ? payload : JSON.stringify(payload)
|
||||
return NextResponse.json({ success: false, error: `Backend dashboard error: ${err}` }, { status: res.status })
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, data: payload })
|
||||
} catch (error) {
|
||||
console.error('Error fetching dashboard data:', error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: 'Failed to fetch dashboard data',
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,7 @@ export async function GET() {
|
||||
object: objectId,
|
||||
checked: sensor.checked ?? false,
|
||||
location: sensor.zone ?? '',
|
||||
serial_number: sensor.serial_number ?? sensor.name ?? '',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
71
frontend/app/api/update-alert/[id]/route.ts
Normal file
71
frontend/app/api/update-alert/[id]/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextResponse, NextRequest } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import { getToken } from 'next-auth/jwt'
|
||||
|
||||
export async function PATCH(req: NextRequest, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
const authHeader = req.headers.get('authorization') || req.headers.get('Authorization')
|
||||
const bearer = authHeader && authHeader.toLowerCase().startsWith('bearer ') ? authHeader.slice(7) : undefined
|
||||
const secret = process.env.NEXTAUTH_SECRET
|
||||
const token = await getToken({ req, secret }).catch(() => null)
|
||||
|
||||
let accessToken = session?.accessToken || bearer || (token as any)?.accessToken
|
||||
const refreshToken = session?.refreshToken || (token as any)?.refreshToken
|
||||
|
||||
if (!accessToken && refreshToken) {
|
||||
try {
|
||||
const refreshRes = await fetch(`${process.env.BACKEND_URL}/auth/refresh/`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ refresh: refreshToken }),
|
||||
})
|
||||
if (refreshRes.ok) {
|
||||
const refreshed = await refreshRes.json()
|
||||
accessToken = refreshed.access
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
if (!accessToken) {
|
||||
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const backendUrl = process.env.BACKEND_URL
|
||||
if (!backendUrl) {
|
||||
return NextResponse.json({ success: false, error: 'BACKEND_URL is not configured' }, { status: 500 })
|
||||
}
|
||||
|
||||
const { id } = await context.params
|
||||
const res = await fetch(`${backendUrl}/account/update-alert/${id}/`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
cache: 'no-store',
|
||||
})
|
||||
|
||||
const text = await res.text()
|
||||
let payload: any
|
||||
try { payload = JSON.parse(text) } catch { payload = text }
|
||||
|
||||
if (!res.ok) {
|
||||
const err = typeof payload === 'string' ? payload : JSON.stringify(payload)
|
||||
return NextResponse.json({ success: false, error: `Backend update-alert error: ${err}` }, { status: res.status })
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, data: payload })
|
||||
} catch (error) {
|
||||
console.error('Error updating alert status:', error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: 'Failed to update alert status',
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { persist } from 'zustand/middleware'
|
||||
export interface DetectorType {
|
||||
detector_id: number
|
||||
name: string
|
||||
serial_number: string
|
||||
object: string
|
||||
status: string
|
||||
type: string
|
||||
|
||||
Reference in New Issue
Block a user