From f5968412a091842105c101f5ed7ddc9476201cb7 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 11 Aug 2024 10:40:11 +0200 Subject: [PATCH] useAutoRequest tidy up --- interface/src/app/main/Devices.tsx | 30 +++---- interface/src/app/main/Sensors.tsx | 84 ++++++++----------- .../src/app/settings/security/ManageUsers.tsx | 9 +- interface/src/app/status/Activity.tsx | 9 -- mock-api/rest_server.ts | 15 ++-- 5 files changed, 60 insertions(+), 87 deletions(-) diff --git a/interface/src/app/main/Devices.tsx b/interface/src/app/main/Devices.tsx index a3fb2c42d..305d3d81b 100644 --- a/interface/src/app/main/Devices.tsx +++ b/interface/src/app/main/Devices.tsx @@ -55,7 +55,7 @@ import { import { useTheme } from '@table-library/react-table-library/theme'; import type { Action, State } from '@table-library/react-table-library/types/common'; import { dialogStyle } from 'CustomTheme'; -import { useAutoRequest, useRequest } from 'alova/client'; +import { useRequest } from 'alova/client'; import { MessageBox, SectionContent, useLayoutTitle } from 'components'; import { AuthenticatedContext } from 'contexts/authentication'; import { useI18nContext } from 'i18n/i18n-react'; @@ -83,16 +83,12 @@ const Devices = () => { useLayoutTitle(LL.DEVICES()); - const { data: coreData, send: sendCoreData } = useAutoRequest( - () => readCoreData(), - { - initialData: { - connected: true, - devices: [] - }, - pollingTime: 2000 + const { data: coreData, send: sendCoreData } = useRequest(() => readCoreData(), { + initialData: { + connected: true, + devices: [] } - ); + }); const { data: deviceData, send: sendDeviceData } = useRequest( (id: number) => readDeviceData(id), @@ -320,12 +316,6 @@ const Devices = () => { }; }, [escFunction]); - const refreshData = () => { - if (!deviceValueDialogOpen) { - selectedDevice ? void sendDeviceData(selectedDevice) : void sendCoreData(); - } - }; - const customize = () => { if (selectedDevice == 99) { navigate('/customentities'); @@ -436,7 +426,12 @@ const Devices = () => { }; useEffect(() => { - const timer = setInterval(() => refreshData(), 60000); + const timer = setInterval(() => { + if (deviceValueDialogOpen) { + return; + } + selectedDevice ? void sendDeviceData(selectedDevice) : void sendCoreData(); + }, 2000); return () => { clearInterval(timer); }; @@ -591,6 +586,7 @@ const Devices = () => { const deviceValueDialogClose = () => { setDeviceValueDialogOpen(false); + void sendDeviceData(selectedDevice); }; const renderDeviceData = () => { diff --git a/interface/src/app/main/Sensors.tsx b/interface/src/app/main/Sensors.tsx index 07f53abcc..b93b61cb3 100644 --- a/interface/src/app/main/Sensors.tsx +++ b/interface/src/app/main/Sensors.tsx @@ -1,4 +1,4 @@ -import { useContext, useEffect, useState } from 'react'; +import { useContext, useState } from 'react'; import { toast } from 'react-toastify'; import AddCircleOutlineOutlinedIcon from '@mui/icons-material/AddCircleOutlineOutlined'; @@ -59,18 +59,15 @@ const Sensors = () => { const [analogDialogOpen, setAnalogDialogOpen] = useState(false); const [creating, setCreating] = useState(false); - const { data: sensorData, send: sendSensorData } = useAutoRequest( - () => readSensorData(), - { - initialData: { - ts: [], - as: [], - analog_enabled: false, - platform: 'ESP32' - }, - pollingTime: 2000 - } - ); + const { data: sensorData } = useAutoRequest(() => readSensorData(), { + initialData: { + ts: [], + as: [], + analog_enabled: false, + platform: 'ESP32' + }, + pollingTime: 2000 + }); const { send: sendTemperatureSensor } = useRequest( (data: WriteTemperatureSensor) => writeTemperatureSensor(data), @@ -198,13 +195,6 @@ const Sensors = () => { } ); - useEffect(() => { - const timer = setInterval(() => sendSensorData(), 30000); - return () => { - clearInterval(timer); - }; - }); - useLayoutTitle(LL.SENSORS()); const formatDurationMin = (duration_min: number) => { @@ -276,10 +266,9 @@ const Sensors = () => { .catch(() => { toast.error(LL.UPDATE_OF(LL.SENSOR(2)) + ' ' + LL.FAILED(1)); }) - .finally(async () => { + .finally(() => { setTemperatureDialogOpen(false); setSelectedTemperatureSensor(undefined); - await sendSensorData(); }); }; @@ -330,10 +319,9 @@ const Sensors = () => { .catch(() => { toast.error(LL.UPDATE_OF(LL.ANALOG_SENSOR(5)) + ' ' + LL.FAILED(1)); }) - .finally(async () => { + .finally(() => { setAnalogDialogOpen(false); setSelectedAnalogSensor(undefined); - await sendSensorData(); }); }; @@ -477,41 +465,37 @@ const Sensors = () => { )} /> )} - {sensorData?.analog_enabled === true && ( - <> - - {LL.ANALOG_SENSORS()} - - - {selectedAnalogSensor && ( - + + {LL.ANALOG_SENSORS()} + + + {selectedAnalogSensor && ( + + /> )} - - {sensorData?.analog_enabled === true && me.admin && ( + {sensorData?.analog_enabled === true && me.admin && ( + - )} - + + )} ); }; diff --git a/interface/src/app/settings/security/ManageUsers.tsx b/interface/src/app/settings/security/ManageUsers.tsx index 9805d8dcf..0cf5f191c 100644 --- a/interface/src/app/settings/security/ManageUsers.tsx +++ b/interface/src/app/settings/security/ManageUsers.tsx @@ -220,8 +220,8 @@ const ManageUsers = () => { )} - - {changed !== 0 && ( + {changed !== 0 && ( + - )} - - + + )}