-
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
76cedc0
commit b05afe5
Showing
4 changed files
with
40 additions
and
41 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
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 |
---|---|---|
@@ -1,47 +1,24 @@ | ||
import React from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
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 apiHost = useSelector((state) => state.UI.apiHost); | ||
const apiObject = React.useMemo( | ||
() => new CWaterPumpAPI({ URL: apiHost }), | ||
[apiHost] | ||
); | ||
|
||
const value = { | ||
API: apiObject, | ||
apiHost: apiHost, | ||
bindApiHost: setApiHost, | ||
}; | ||
|
||
const value = { API: apiObject, }; | ||
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