diff --git a/ui/src/App.js b/ui/src/App.js index 9f00e88..dcbbb2f 100644 --- a/ui/src/App.js +++ b/ui/src/App.js @@ -1,6 +1,7 @@ import React, { useState, useEffect } from 'react'; -import './App.css'; +import { Container, Row, Col, Form, Button, Alert } from 'react-bootstrap'; import { CWaterPumpAPI } from './api/CWaterPumpAPI.js'; +import './App.css'; const STORE_API = 'apiAddress'; const STORE_RUNTIME = 'runTime'; @@ -8,6 +9,8 @@ const STORE_RUNTIME = 'runTime'; function App() { const [apiAddress, setApiAddress] = useState(''); const [runTime, setRunTime] = useState(1000); + const [alertMessage, setAlertMessage] = useState(''); + const [showAlert, setShowAlert] = useState(false); useEffect(() => { const storedApiAddress = localStorage.getItem(STORE_API); @@ -15,40 +18,40 @@ function App() { let storedRunTime = localStorage.getItem(STORE_RUNTIME); if (storedRunTime) { - // if string then convert to int if (typeof storedRunTime === 'string') { storedRunTime = parseInt(storedRunTime, 10); } setRunTime(storedRunTime); } - }, []); // on mount + }, []); - const waterPumpAPI = React.useMemo( - () => { - // ensure apiAddress is started with http:// or https:// - let url = apiAddress; - if (!url.startsWith('http://') && !url.startsWith('https://')) { - url = 'http://' + url; - } - return new CWaterPumpAPI({ URL: url }); - }, [apiAddress] // only re-create if apiAddress changes - ); + const waterPumpAPI = React.useMemo(() => { + let url = apiAddress; + if (!url.startsWith('http://') && !url.startsWith('https://')) { + url = 'http://' + url; + } + return new CWaterPumpAPI({ URL: url }); + }, [apiAddress]); const handleStart = async () => { try { await waterPumpAPI.start(runTime); - alert('Water pump started successfully!'); + setAlertMessage('Water pump started successfully!'); + setShowAlert(true); } catch (error) { - alert('Error starting water pump: ' + error.message); + setAlertMessage('Error starting water pump: ' + error.message); + setShowAlert(true); } }; const handleStop = async () => { try { await waterPumpAPI.stop(); - alert('Water pump stopped successfully!'); + setAlertMessage('Water pump stopped successfully!'); + setShowAlert(true); } catch (error) { - alert('Error stopping water pump: ' + error.message); + setAlertMessage('Error stopping water pump: ' + error.message); + setShowAlert(true); } }; @@ -65,23 +68,30 @@ function App() { }; return ( -