export default ((namespace, initial = null) => {
const initialState = initial;
var namespace;
const handler = (state = initialState, action) => {
const [action_namespace, action_type] = action.type.split('/');
if (namespace != action_namespace) {
return state;
}
switch (action_type) {
case 'set':
return action.payload;
default:
return state;
}
};
let store;
handler.__init = (_store, _namespace) => {
store = _store;
namespace = _namespace;
};
handler.__actions = {
'set': (value) => {
store.dispatch({
type: namespace+'/set',
payload: value
});
}
};
return handler;
});
import {
createStore,
combineReducers,
} from 'redux';
import {each} from 'lodash-es';
import {pluralize, camelize} from 'inflection';
import singletonReducer from '../reducers/singletonReducer';
import mapActions from './mapActions';
// This allows us to easily call actions on reducers, like so:
// Actions.task.set() and
const mapActions = (map, store, actions) => {
// set actions for each store
each(map, (v, k) => {
if (v.__init) {
v.__init(store, k);
actions[k] = v.__actions;
}
});
};
const configureStore = (map, initialState = {}, actions = {}) => {
const reducer = combineReducers(map);
// pre-fill computer values
initialState = wrappedReducer(initialState, {type: 'initComputed'});
const store = createStore(
reducer,
initialState,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
mapActions(map, store, actions);
return store;
}
export default ({
actions = {},
state = window.__REDUX_INIT_CONTEXT,
} = {}) => {
const map = {
currentProjectId: singletonReducer(),
};
const store = configureStore(
map,
state,
actions,
globalReducers
);
return {
redux: store,
store: new Store(store, actions),
actions: actions,
};
};
When I do that, I have a globally available map of actions, that is constructed by the reducers that I setup, so there is zero config, no need to remember or lookup action names.
This is what I came up with in the first weeks and its super easy to use. You have far more experience so I bet you'll be able to come up with something smarter and cleaner if it inspires you.