Skip to content

Commit

Permalink
Adding Redux for state management
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlocph committed Oct 22, 2023
1 parent 61ad403 commit 4d369b4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
52 changes: 40 additions & 12 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"redux": "^4.2.1",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
9 changes: 7 additions & 2 deletions web/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import './App.css';
import APIBackend from "./apiBackend";
import StreamingBackend from "./streamingBackend";
import CapacitorClient from "./client";
import { createStore } from 'redux'
import { rootReducer } from './redux';

function App() {
const capacitorClient = new CapacitorClient(
Expand All @@ -11,10 +13,13 @@ function App() {
}
);

const store = createStore(rootReducer);
store.subscribe(() => console.log(store.getState()))

return (
<>
<APIBackend capacitorClient={capacitorClient}/>
<StreamingBackend capacitorClient={capacitorClient}/>
<APIBackend capacitorClient={capacitorClient} store={store}/>
<StreamingBackend capacitorClient={capacitorClient} store={store}/>
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
Expand Down
10 changes: 9 additions & 1 deletion web/src/apiBackend.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Component } from 'react';
import {
ACTION_FLUX_STATE_RECEIVED,
} from "./redux";

export default class APIBackend extends Component {

componentDidMount() {
this.props.capacitorClient.getFluxState()
.then(data => console.log(data), () => {/* Generic error handler deals with it */ });
.then(
data => this.props.store.dispatch(
{type: ACTION_FLUX_STATE_RECEIVED, payload: data}
),
() => {/* Generic error handler deals with it */ }
);
}

render() {
Expand Down
20 changes: 20 additions & 0 deletions web/src/redux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const initialState = {
fluxState: {}
}

export const ACTION_FLUX_STATE_RECEIVED = 'ACTION_FLUX_STATE_RECEIVED';

export function rootReducer(state = initialState, action) {
switch (action.type) {
case ACTION_FLUX_STATE_RECEIVED:
return fluxStateReceived(state, action.payload)
default:
console.log('Could not process redux event: ' + JSON.stringify(action));
return state;
}
}

function fluxStateReceived(state, payload) {
state.fluxState = payload
return state
}
2 changes: 1 addition & 1 deletion web/src/streamingBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class StreamingBackend extends Component {
onMessage = (evt) => {
evt.data.split('\n').forEach((line) => {
const message = JSON.parse(line);
console.log(message)
this.props.store.dispatch(message);
});
}
}

0 comments on commit 4d369b4

Please sign in to comment.