From c5a2c4574e7aaa78eb6dacadf350979015384c2c Mon Sep 17 00:00:00 2001 From: GreenWizard2015 Date: Mon, 1 Jan 2024 14:39:18 +0000 Subject: [PATCH] extract NotificationsSystem --- ui/src/App.js | 21 +++++++++------------ ui/src/components/NotificationsArea.js | 19 +++++++++++++++++++ ui/src/contexts/NotificationsContext.js | 23 +++++++++++++++++++++++ ui/src/index.js | 6 +++++- 4 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 ui/src/components/NotificationsArea.js create mode 100644 ui/src/contexts/NotificationsContext.js diff --git a/ui/src/App.js b/ui/src/App.js index dcbbb2f..d88e43a 100644 --- a/ui/src/App.js +++ b/ui/src/App.js @@ -1,7 +1,9 @@ import React, { useState, useEffect } from 'react'; -import { Container, Row, Col, Form, Button, Alert } from 'react-bootstrap'; +import { Container, Row, Col, Form, Button } from 'react-bootstrap'; import { CWaterPumpAPI } from './api/CWaterPumpAPI.js'; +import { useNotificationsSystem } from './contexts/NotificationsContext.js'; import './App.css'; +import NotificationsArea from './components/NotificationsArea.js'; const STORE_API = 'apiAddress'; const STORE_RUNTIME = 'runTime'; @@ -9,8 +11,7 @@ const STORE_RUNTIME = 'runTime'; function App() { const [apiAddress, setApiAddress] = useState(''); const [runTime, setRunTime] = useState(1000); - const [alertMessage, setAlertMessage] = useState(''); - const [showAlert, setShowAlert] = useState(false); + const NotificationsSystem = useNotificationsSystem(); useEffect(() => { const storedApiAddress = localStorage.getItem(STORE_API); @@ -36,22 +37,18 @@ function App() { const handleStart = async () => { try { await waterPumpAPI.start(runTime); - setAlertMessage('Water pump started successfully!'); - setShowAlert(true); + NotificationsSystem.alert('Water pump started successfully!'); } catch (error) { - setAlertMessage('Error starting water pump: ' + error.message); - setShowAlert(true); + NotificationsSystem.alert('Error starting water pump: ' + error.message); } }; const handleStop = async () => { try { await waterPumpAPI.stop(); - setAlertMessage('Water pump stopped successfully!'); - setShowAlert(true); + NotificationsSystem.alert('Water pump stopped successfully!'); } catch (error) { - setAlertMessage('Error stopping water pump: ' + error.message); - setShowAlert(true); + NotificationsSystem.alert('Error stopping water pump: ' + error.message); } }; @@ -70,7 +67,7 @@ function App() { return (

Tea System UI

- {showAlert && setShowAlert(false)} dismissible>{alertMessage}} +
diff --git a/ui/src/components/NotificationsArea.js b/ui/src/components/NotificationsArea.js new file mode 100644 index 0000000..afb38c0 --- /dev/null +++ b/ui/src/components/NotificationsArea.js @@ -0,0 +1,19 @@ +import React from 'react'; +import { Alert } from 'react-bootstrap'; +import { useNotificationsSystem } from '../contexts/NotificationsContext'; + +function NotificationsArea() { + const NotificationsSystem = useNotificationsSystem(); + const { currentNotifications } = NotificationsSystem; + if(!currentNotifications) return null; + + const hideNotifications = () => { NotificationsSystem.clear(); }; + + return ( + + {currentNotifications.message} + + ); +} + +export default NotificationsArea; diff --git a/ui/src/contexts/NotificationsContext.js b/ui/src/contexts/NotificationsContext.js new file mode 100644 index 0000000..6685a68 --- /dev/null +++ b/ui/src/contexts/NotificationsContext.js @@ -0,0 +1,23 @@ +import React from 'react'; + +const NotificationsContext = React.createContext(); + +export function useNotificationsSystem() { + return React.useContext(NotificationsContext); +} + +export function NotificationsProvider({ children }) { + const [notifications, setNotifications] = React.useState(null); + + const value = { + alert: (message) => { setNotifications({ message }); }, + clear: () => { setNotifications(null); }, + currentNotifications: notifications, + }; + + return ( + + {children} + + ); +} \ No newline at end of file diff --git a/ui/src/index.js b/ui/src/index.js index d08c80a..9ea6ccb 100644 --- a/ui/src/index.js +++ b/ui/src/index.js @@ -3,9 +3,13 @@ import ReactDOM from 'react-dom'; import App from './App.js'; import 'bootstrap/dist/css/bootstrap.min.css'; // Importing Bootstrap CSS +import { NotificationsProvider } from './contexts/NotificationsContext.js'; + ReactDOM.render( - + + + , document.getElementById('root') );