Skip to content

Commit

Permalink
move apiHost to redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 3, 2024
1 parent 76cedc0 commit b05afe5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
12 changes: 11 additions & 1 deletion ui/src/api/CWaterPumpAPI.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
24 changes: 16 additions & 8 deletions ui/src/components/APIAddressField.js
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -16,10 +14,20 @@ function APIAddressField() {
API Address:
</Form.Label>
<Col sm="10">
<Form.Control type="text" value={waterPumpCtx.apiHost} onChange={handleApiAddressChange} />
<Form.Control
type="text" placeholder="Enter API Address"
value={apiHost}
onChange={handleApiAddressChange}
/>
</Col>
</Form.Group>
);
}

export default APIAddressField;
export default connect(
(state) => ({
apiHost: state.UI.apiHost
}), {
onChange: updateAPIHost
}
)(APIAddressFieldComponent);
37 changes: 7 additions & 30 deletions ui/src/contexts/WaterPumpAPIContext.js
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>
);
}
}
8 changes: 6 additions & 2 deletions ui/src/store/slices/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSlice } from '@reduxjs/toolkit';

const INITIAL_STATE = {
pouringTime: 1000,
apiHost: '',
};
// slice for system status
export const UISlice = createSlice({
Expand All @@ -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;
export const { updatePouringTime, updateAPIHost } = actions;

0 comments on commit b05afe5

Please sign in to comment.