Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 2 reducers which are used for composition.
- */
- const reduceUpdateEntity = state => state; // Ignore this one
- const reduceReplaceEntity = (state, { payload, meta }) => ({
- ...state,
- [meta.model]: {
- ...state[meta.model]
- [meta.entity]: payload,
- },
- });
- /**
- * Reducer's managed state.
- */
- const mainReducerInitialState = {
- collection: {
- animals: [],
- people: [],
- books: [],
- },
- };
- /**
- * Typical reducer.
- */
- const mainReducer = (state = mainReducerInitialState, action) => {
- switch (action.type) {
- case constant.REPLACE_ENTITY:
- return reduceReplaceEntity(state, action);
- case constant.UPDATE_ENTITY:
- return reduceUpdateEntity(state, action);
- }
- };
- /**
- * Creating Redux store with one main reducer. - Instead of using
- * plain switch statement I'm using 'redux-actions' module from GitHub.
- */
- const store = Redux.createStore(
- Redux.CombineReducers({
- main: mainReducer,
- }),
- );
- store.dispatch({
- type: constant.REPLACE_ENTITY,
- payload: 'replacing animals',
- meta: {
- model: 'collection', // <-- This is where I'd like to reuse my constants
- entity: 'animals', // <-- This is where I'd like to reuse my constants
- },
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement