-
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
835ffd4
commit 2b66c7b
Showing
8 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,52 @@ | ||
import { configureStore } from "@reduxjs/toolkit"; | ||
import { Provider } from "react-redux"; | ||
|
||
// slices | ||
import { ALL_APP_SLICES } from "./slices"; | ||
|
||
// listeners | ||
// import { eventsListener } from "./listeners"; | ||
|
||
function buildAppStore(preloadedState) { | ||
const slices = ALL_APP_SLICES; | ||
const reducers = {}; | ||
const state = {}; | ||
Object.keys(slices).forEach(key => { | ||
reducers[key] = slices[key].reducer; | ||
const defaultState = slices[key].getInitialState(); | ||
state[key] = defaultState; | ||
|
||
if (key in preloadedState) { | ||
// if default state is an object, merge it with the preloaded state | ||
if (typeof defaultState === 'object') { | ||
const preloadedStateForKey = preloadedState[key] || {}; | ||
state[key] = { ...defaultState, ...preloadedStateForKey }; | ||
} else { // otherwise, just use the preloaded state | ||
state[key] = preloadedState[key]; | ||
} | ||
} | ||
}); | ||
|
||
return { reducers, state }; | ||
} | ||
|
||
// AppStore is a wrapper component that provides the Redux store to the rest of the application. | ||
// preloadedState is an optional parameter that allows you to pass in an initial state for the store. | ||
const AppStore = ({ children, preloadedState = {}, returnStore = false }) => { | ||
const { reducers, state } = buildAppStore(preloadedState); | ||
const store = configureStore({ | ||
reducer: reducers, | ||
preloadedState: state, | ||
// middleware: (getDefaultMiddleware) => getDefaultMiddleware().prepend(eventsListener.middleware), | ||
}); | ||
const provider = ( | ||
<Provider store={store}> | ||
{children} | ||
</Provider> | ||
); | ||
|
||
if (returnStore) return { store, provider }; | ||
return provider; | ||
}; | ||
|
||
export { AppStore }; |
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,28 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
// TODO: Replace this with a real system status | ||
// replace "water threshold" with "waterThreshold" | ||
// replace "time left" with "timeLeft" | ||
// add field "updated" | ||
const systemStatus = { | ||
waterThreshold: 1234, | ||
pump: { | ||
running: false, | ||
timeLeft: 0, | ||
}, | ||
updated: new Date(), | ||
}; | ||
|
||
// slice for system status | ||
export const SystemStatusSlice = createSlice({ | ||
name: 'systemStatus', | ||
initialState: systemStatus, | ||
reducers: { | ||
updateSystemStatus(state, action) { | ||
return action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const actions = SystemStatusSlice.actions; | ||
export const { updateSystemStatus } = actions; |
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,8 @@ | ||
import { SystemStatusSlice } from "./SystemStatus"; | ||
|
||
const slices = [ SystemStatusSlice ]; | ||
// export all slices as an object { [sliceName]: slice } | ||
export const ALL_APP_SLICES = slices.reduce((acc, slice) => { | ||
acc[slice.name] = slice; | ||
return acc; | ||
}, {}); |