From ec690ebbcb6328f462d6e5c0c3cf818e13ac2609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Z=C3=ADpek?= Date: Wed, 24 Aug 2022 09:31:10 +0200 Subject: [PATCH] Fixes and proper box settings --- .../dashboard/components/BoxGraphContent.tsx | 20 ++++--- .../{ => BoxSettings}/BoxSettings.tsx | 42 +++++-------- .../BoxSettings/components/GraphSettings.tsx | 60 +++++++++++++++++++ .../dashboard/components/EditableBox.tsx | 14 +++-- client/src/utils/parseDashboard.ts | 5 ++ 5 files changed, 99 insertions(+), 42 deletions(-) rename client/src/pages/dashboard/components/{ => BoxSettings}/BoxSettings.tsx (69%) create mode 100644 client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx diff --git a/client/src/pages/dashboard/components/BoxGraphContent.tsx b/client/src/pages/dashboard/components/BoxGraphContent.tsx index 603fd34..ac8eb04 100644 --- a/client/src/pages/dashboard/components/BoxGraphContent.tsx +++ b/client/src/pages/dashboard/components/BoxGraphContent.tsx @@ -1,4 +1,5 @@ import { getSensorValues } from '@/api/sensorValues' +import { DashboardGraphData } from '@/utils/parseDashboard' import { useEffect, useRef } from 'preact/hooks' import { useQuery } from 'react-query' import { useDashboardContext } from '../contexts/DashboardContext' @@ -6,9 +7,10 @@ import { BoxDefinition } from '../types' type Props = { box: BoxDefinition + data: DashboardGraphData } -export const BoxGraphContent = ({ box }: Props) => { +export const BoxGraphContent = ({ box, data }: Props) => { const { filter } = useDashboardContext() const bodyRef = useRef(null) @@ -27,8 +29,8 @@ export const BoxGraphContent = ({ box }: Props) => { // TODO: These should be probably returned by server, could be outdated const from = filter.customFrom const to = filter.customTo - const minValue = parseFloat(box.min ?? '') - const maxValue = parseFloat(box.max ?? '') + const minValue = parseFloat(data.min ?? '') + const maxValue = parseFloat(data.max ?? '') const customRange = !isNaN(minValue) && !isNaN(maxValue) if (bodyRef.current && values.data) { @@ -36,19 +38,19 @@ export const BoxGraphContent = ({ box }: Props) => { bodyRef.current, [ { - ...(box.graphType === 'line' && { + ...(data.graphType === 'line' && { type: 'scatter', mode: 'lines', }), - ...(box.graphType === 'points' && { + ...(data.graphType === 'points' && { type: 'scatter', mode: 'markers', }), - ...(box.graphType === 'lineAndPoints' && { + ...(data.graphType === 'lineAndPoints' && { type: 'scatter', mode: 'lines+markers', }), - ...(box.graphType === 'bar' && { type: 'bar' }), + ...(data.graphType === 'bar' && { type: 'bar' }), x: values.data.map((v) => new Date(v.timestamp * 1000)), y: values.data.map((v) => v.value), line: { @@ -60,7 +62,7 @@ export const BoxGraphContent = ({ box }: Props) => { xaxis: { range: [from, to], type: 'date' }, yaxis: { ...(customRange && { range: [minValue, maxValue] }), - ...(box.unit && { ticksuffix: ` ${box.unit}` }), + ...(data.unit && { ticksuffix: ` ${data.unit}` }), }, margin: { l: 70, @@ -76,7 +78,7 @@ export const BoxGraphContent = ({ box }: Props) => { } ) } - }, [values.data, box]) + }, [values.data, box, data]) return
} diff --git a/client/src/pages/dashboard/components/BoxSettings.tsx b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx similarity index 69% rename from client/src/pages/dashboard/components/BoxSettings.tsx rename to client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx index 23508eb..c770c86 100644 --- a/client/src/pages/dashboard/components/BoxSettings.tsx +++ b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx @@ -1,7 +1,8 @@ import { getSensors } from '@/api/sensors' import { useState } from 'preact/hooks' import { useQuery } from 'react-query' -import { BoxDefinition } from '../types' +import { BoxDefinition } from '../../types' +import { GraphSettings } from './components/GraphSettings' type Props = { value: BoxDefinition @@ -15,12 +16,11 @@ export const BoxSettings = ({ value, onSave, onClose }: Props) => { const [formState, setFormState] = useState(() => ({ sensor: value.sensor, title: value.title, - min: value.min, - max: value.max, - graphType: value.graphType, - unit: value.unit, + type: value.data?.type ?? 'graph', })) + const [data, setData] = useState(() => value.data) + const handleSave = async (e: Event) => { e.preventDefault() e.stopPropagation() @@ -29,7 +29,9 @@ export const BoxSettings = ({ value, onSave, onClose }: Props) => { onSave({ ...value, - ...formState, + sensor: formState.sensor, + title: formState.title, + data: data, }) } @@ -81,33 +83,17 @@ export const BoxSettings = ({ value, onSave, onClose }: Props) => {
-
- - -
-
- - -
-
- - -
+ {formState.type === 'graph' && ( + + )}
- {box.sensor && } + {box.sensor && box.data?.type === 'graph' && ( + + )}