-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5a2c45
commit 5b46699
Showing
4 changed files
with
90 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { Row, Col, Form } from 'react-bootstrap'; | ||
import { useWaterPumpAPI } from '../contexts/WaterPumpAPIContext'; | ||
|
||
function APIAddressField() { | ||
const waterPumpCtx = useWaterPumpAPI(); | ||
|
||
const handleApiAddressChange = (event) => { | ||
const apiAddress = event.target.value; | ||
waterPumpCtx.bindApiHost(apiAddress); | ||
}; | ||
|
||
return ( | ||
<Form.Group as={Row} className="mb-3"> | ||
<Form.Label column sm="2"> | ||
API Address: | ||
</Form.Label> | ||
<Col sm="10"> | ||
<Form.Control type="text" value={waterPumpCtx.apiHost} onChange={handleApiAddressChange} /> | ||
</Col> | ||
</Form.Group> | ||
); | ||
} | ||
|
||
export default APIAddressField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { CWaterPumpAPI } from '../api/CWaterPumpAPI.js'; | ||
|
||
const STORE_API = 'apiAddress'; | ||
const WaterPumpAPIContext = React.createContext(); | ||
|
||
export function useWaterPumpAPI() { | ||
return React.useContext(WaterPumpAPIContext); | ||
} | ||
|
||
function preprocessApiHost(apiHost) { | ||
let url = apiHost; | ||
if (!url.startsWith('http://') && !url.startsWith('https://')) { | ||
url = 'http://' + url; | ||
} | ||
if (!url.endsWith('/')) url += '/'; | ||
return url; | ||
} | ||
|
||
export function WaterPumpAPIProvider({ children }) { | ||
const [apiHost, setApiHost] = React.useState(''); | ||
React.useEffect(() => { | ||
const storedApiHost = localStorage.getItem(STORE_API); | ||
if (storedApiHost) setApiHost(storedApiHost); | ||
}, []); // on mount only | ||
|
||
const apiObject = React.useMemo(() => { | ||
if (!apiHost) return null; // not ready yet | ||
|
||
const url = preprocessApiHost(apiHost); | ||
localStorage.setItem(STORE_API, url); | ||
return new CWaterPumpAPI({ URL: url }); | ||
}, [apiHost] | ||
); | ||
|
||
const value = { | ||
API: apiObject, | ||
apiHost: apiHost, | ||
bindApiHost: setApiHost, | ||
}; | ||
|
||
return ( | ||
<WaterPumpAPIContext.Provider value={value}> | ||
{children} | ||
</WaterPumpAPIContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters