Admin: Allow reducers to be injected async into the store

Co-authored-by: Josh Ellis <josh.ellis@strapi.io>
This commit is contained in:
Gustav Hansen 2023-01-20 17:51:59 +01:00
parent e66609ec27
commit d252d9da2d
2 changed files with 22 additions and 9 deletions

View File

@ -1,5 +1,4 @@
import { createStore, applyMiddleware, compose } from 'redux'; import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import createReducer from './createReducer';
const configureStore = (appMiddlewares, appReducers) => { const configureStore = (appMiddlewares, appReducers) => {
let composeEnhancers = compose; let composeEnhancers = compose;
@ -19,11 +18,30 @@ const configureStore = (appMiddlewares, appReducers) => {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}); composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
} }
return createStore( const store = createStore(
createReducer(appReducers), createReducer(appReducers, {}),
{}, {},
composeEnhancers(applyMiddleware(...middlewares)) composeEnhancers(applyMiddleware(...middlewares))
); );
// Add a dictionary to keep track of the registered async reducers
store.asyncReducers = {};
// Create an inject reducer function
// This function adds the async reducer, and creates a new combined reducer
store.injectReducer = (key, asyncReducer) => {
store.asyncReducers[key] = asyncReducer;
store.replaceReducer(createReducer(appReducers, store.asyncReducers));
};
return store;
};
const createReducer = (appReducers, asyncReducers) => {
return combineReducers({
...appReducers,
...asyncReducers,
});
}; };
export default configureStore; export default configureStore;

View File

@ -1,5 +0,0 @@
import { combineReducers } from 'redux';
const createReducer = (reducers) => combineReducers(reducers);
export default createReducer;