From 0a071706886a1181182d77bb8f5deca5cb4ea645 Mon Sep 17 00:00:00 2001 From: GreenWizard2015 Date: Mon, 1 Jan 2024 21:54:11 +0000 Subject: [PATCH] PourTimeField component --- ui/src/App.js | 38 ++++++------------------------ ui/src/components/PourTimeField.js | 38 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 ui/src/components/PourTimeField.js diff --git a/ui/src/App.js b/ui/src/App.js index 0cfbeae..4b7c247 100644 --- a/ui/src/App.js +++ b/ui/src/App.js @@ -1,38 +1,21 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState } from 'react'; import './App.css'; -import { Container, Row, Col, Form, Button } from 'react-bootstrap'; +import { Container, Form, Button } from 'react-bootstrap'; import { useWaterPumpAPI } from './contexts/WaterPumpAPIContext'; import { useNotificationsSystem } from './contexts/NotificationsContext.js'; import NotificationsArea from './components/NotificationsArea.js'; import APIAddressField from './components/APIAddressField'; - -const STORE_RUNTIME = 'runTime'; +import PourTimeField from './components/PourTimeField'; function App() { const waterPump = useWaterPumpAPI().API; - const [runTime, setRunTime] = useState(1000); - const NotificationsSystem = useNotificationsSystem(); - - useEffect(() => { - let storedRunTime = localStorage.getItem(STORE_RUNTIME); - if (storedRunTime) { - if (typeof storedRunTime === 'string') { - storedRunTime = parseInt(storedRunTime, 10); - } - setRunTime(storedRunTime); - } - }, []); - - const handleRunTimeChange = (event) => { - const runTime = parseInt(event.target.value, 10); - setRunTime(runTime); - localStorage.setItem(STORE_RUNTIME, runTime); - }; + const NotificationsSystem = useNotificationsSystem(); + const [pourTime, setPourTime] = useState(1000); const handleStart = async () => { try { - await waterPump.start(runTime); + await waterPump.start(pourTime); NotificationsSystem.alert('Water pump started successfully!'); } catch (error) { NotificationsSystem.alert('Error starting water pump: ' + error.message); @@ -54,14 +37,7 @@ function App() {
- - - Run Time (ms): - - - - - + {' '} diff --git a/ui/src/components/PourTimeField.js b/ui/src/components/PourTimeField.js new file mode 100644 index 0000000..51120fd --- /dev/null +++ b/ui/src/components/PourTimeField.js @@ -0,0 +1,38 @@ +import React, { useState, useEffect } from 'react'; +import { Form, Row, Col } from 'react-bootstrap'; + +const STORE_POURRTIME = 'pourTime'; + +function PourTimeField({ onChange, min, max }) { + const [pourTime, setPourTime] = useState(1000); + + useEffect(() => { + const time = localStorage.getItem(STORE_POURRTIME); + if (time) setPourTime(parseInt(time, 10)); + }, []); // on mount + // call onChange when pourTime changes + useEffect(() => onChange(pourTime), [pourTime, onChange]); + + const handleChange = (event) => { + let newTime = parseInt(event.target.value, 10); + if (isNaN(newTime)) return; + if (newTime < min) newTime = min; + if (max < newTime) newTime = max; + + setPourTime(newTime); + localStorage.setItem(STORE_POURRTIME, newTime); + }; + + return ( + + + Run Time (ms): + + + + + + ); +} + +export default PourTimeField; \ No newline at end of file