1
1
import React , { useState , useEffect } from 'react' ;
2
+ import './App.css' ;
2
3
import { Container , Row , Col , Form , Button } from 'react-bootstrap' ;
3
- import { CWaterPumpAPI } from './api/CWaterPumpAPI.js' ;
4
+
5
+ import { useWaterPumpAPI } from './contexts/WaterPumpAPIContext' ;
4
6
import { useNotificationsSystem } from './contexts/NotificationsContext.js' ;
5
- import './App.css' ;
6
7
import NotificationsArea from './components/NotificationsArea.js' ;
8
+ import APIAddressField from './components/APIAddressField' ;
7
9
8
- const STORE_API = 'apiAddress' ;
9
10
const STORE_RUNTIME = 'runTime' ;
10
11
11
12
function App ( ) {
12
- const [ apiAddress , setApiAddress ] = useState ( '' ) ;
13
+ const waterPumpCtx = useWaterPumpAPI ( ) ;
13
14
const [ runTime , setRunTime ] = useState ( 1000 ) ;
14
15
const NotificationsSystem = useNotificationsSystem ( ) ;
15
16
16
17
useEffect ( ( ) => {
17
- const storedApiAddress = localStorage . getItem ( STORE_API ) ;
18
- if ( storedApiAddress ) setApiAddress ( storedApiAddress ) ;
19
-
20
18
let storedRunTime = localStorage . getItem ( STORE_RUNTIME ) ;
21
19
if ( storedRunTime ) {
22
20
if ( typeof storedRunTime === 'string' ) {
@@ -25,18 +23,16 @@ function App() {
25
23
setRunTime ( storedRunTime ) ;
26
24
}
27
25
} , [ ] ) ;
28
-
29
- const waterPumpAPI = React . useMemo ( ( ) => {
30
- let url = apiAddress ;
31
- if ( ! url . startsWith ( 'http://' ) && ! url . startsWith ( 'https://' ) ) {
32
- url = 'http://' + url ;
33
- }
34
- return new CWaterPumpAPI ( { URL : url } ) ;
35
- } , [ apiAddress ] ) ;
26
+
27
+ const handleRunTimeChange = ( event ) => {
28
+ const runTime = parseInt ( event . target . value , 10 ) ;
29
+ setRunTime ( runTime ) ;
30
+ localStorage . setItem ( STORE_RUNTIME , runTime ) ;
31
+ } ;
36
32
37
33
const handleStart = async ( ) => {
38
34
try {
39
- await waterPumpAPI . start ( runTime ) ;
35
+ await waterPumpCtx . API . start ( runTime ) ;
40
36
NotificationsSystem . alert ( 'Water pump started successfully!' ) ;
41
37
} catch ( error ) {
42
38
NotificationsSystem . alert ( 'Error starting water pump: ' + error . message ) ;
@@ -45,38 +41,19 @@ function App() {
45
41
46
42
const handleStop = async ( ) => {
47
43
try {
48
- await waterPumpAPI . stop ( ) ;
44
+ await waterPumpCtx . API . stop ( ) ;
49
45
NotificationsSystem . alert ( 'Water pump stopped successfully!' ) ;
50
46
} catch ( error ) {
51
47
NotificationsSystem . alert ( 'Error stopping water pump: ' + error . message ) ;
52
48
}
53
49
} ;
54
50
55
- const handleRunTimeChange = ( event ) => {
56
- const runTime = parseInt ( event . target . value , 10 ) ;
57
- setRunTime ( runTime ) ;
58
- localStorage . setItem ( STORE_RUNTIME , runTime ) ;
59
- } ;
60
-
61
- const handleApiAddressChange = ( event ) => {
62
- const apiAddress = event . target . value ;
63
- setApiAddress ( apiAddress ) ;
64
- localStorage . setItem ( STORE_API , apiAddress ) ;
65
- } ;
66
-
67
51
return (
68
52
< Container className = "App" >
69
53
< h1 > Tea System UI</ h1 >
70
54
< NotificationsArea />
71
55
< Form >
72
- < Form . Group as = { Row } className = "mb-3" >
73
- < Form . Label column sm = "2" >
74
- API Address:
75
- </ Form . Label >
76
- < Col sm = "10" >
77
- < Form . Control type = "text" value = { apiAddress } onChange = { handleApiAddressChange } />
78
- </ Col >
79
- </ Form . Group >
56
+ < APIAddressField />
80
57
< Form . Group as = { Row } className = "mb-3" >
81
58
< Form . Label column sm = "2" >
82
59
Run Time (ms):
0 commit comments