diff --git a/ui/src/api/CWaterPumpAPI.js b/ui/src/api/CWaterPumpAPI.js
index 341ec20..978eb55 100644
--- a/ui/src/api/CWaterPumpAPI.js
+++ b/ui/src/api/CWaterPumpAPI.js
@@ -1,8 +1,18 @@
import axios from 'axios';
+// helper function to preprocess the API host
+function preprocessApiHost(apiHost) {
+ let url = apiHost;
+ if (!url.startsWith('http://') && !url.startsWith('https://')) {
+ url = 'http://' + url;
+ }
+ if (!url.endsWith('/')) url += '/';
+ return url;
+}
+
class CWaterPumpAPI {
constructor({ client=null, URL }) {
- this._client = client || axios.create({ baseURL: URL });
+ this._client = client || axios.create({ baseURL: preprocessApiHost(URL) });
}
async start(runTimeMs) {
diff --git a/ui/src/components/APIAddressField.js b/ui/src/components/APIAddressField.js
index de85e67..8fd646c 100644
--- a/ui/src/components/APIAddressField.js
+++ b/ui/src/components/APIAddressField.js
@@ -1,13 +1,11 @@
import React from 'react';
import { Row, Col, Form } from 'react-bootstrap';
-import { useWaterPumpAPI } from '../contexts/WaterPumpAPIContext';
-
-function APIAddressField() {
- const waterPumpCtx = useWaterPumpAPI();
+import { connect } from 'react-redux';
+import { updateAPIHost } from '../store/slices/UI';
+export function APIAddressFieldComponent({ apiHost, onChange }) {
const handleApiAddressChange = (event) => {
- const apiAddress = event.target.value;
- waterPumpCtx.bindApiHost(apiAddress);
+ onChange(event.target.value);
};
return (
@@ -16,10 +14,20 @@ function APIAddressField() {
API Address:
-
+
);
}
-export default APIAddressField;
\ No newline at end of file
+export default connect(
+ (state) => ({
+ apiHost: state.UI.apiHost
+ }), {
+ onChange: updateAPIHost
+ }
+)(APIAddressFieldComponent);
\ No newline at end of file
diff --git a/ui/src/contexts/WaterPumpAPIContext.js b/ui/src/contexts/WaterPumpAPIContext.js
index a97e3d8..6f91141 100644
--- a/ui/src/contexts/WaterPumpAPIContext.js
+++ b/ui/src/contexts/WaterPumpAPIContext.js
@@ -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 (
{children}
);
-}
+}
\ No newline at end of file
diff --git a/ui/src/store/slices/UI.js b/ui/src/store/slices/UI.js
index 6a158d1..b462371 100644
--- a/ui/src/store/slices/UI.js
+++ b/ui/src/store/slices/UI.js
@@ -2,6 +2,7 @@ import { createSlice } from '@reduxjs/toolkit';
const INITIAL_STATE = {
pouringTime: 1000,
+ apiHost: '',
};
// slice for system status
export const UISlice = createSlice({
@@ -10,9 +11,12 @@ export const UISlice = createSlice({
reducers: {
updatePouringTime(state, action) {
state.pouringTime = action.payload;
- }
+ },
+ updateAPIHost(state, action) {
+ state.apiHost = action.payload;
+ },
},
});
export const actions = UISlice.actions;
-export const { updatePouringTime } = actions;
\ No newline at end of file
+export const { updatePouringTime, updateAPIHost } = actions;
\ No newline at end of file