Advertisement
monstrasitix

Untitled

Nov 25th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * 2 reducers which are used for composition.
  3.  */
  4. const reduceUpdateEntity = state => state; // Ignore this one
  5. const reduceReplaceEntity = (state, { payload, meta }) => ({
  6.     ...state,
  7.     [meta.model]: {
  8.         ...state[meta.model]
  9.         [meta.entity]: payload,
  10.     },
  11. });
  12.  
  13. /**
  14.  * Reducer's managed state.
  15.  */
  16. const mainReducerInitialState = {
  17.     collection: {
  18.         animals: [],
  19.         people: [],
  20.         books: [],
  21.     },
  22. };
  23.  
  24. /**
  25.  * Typical reducer.
  26.  */
  27. const mainReducer = (state = mainReducerInitialState, action) => {
  28.     switch (action.type) {
  29.         case constant.REPLACE_ENTITY:
  30.             return reduceReplaceEntity(state, action);
  31.         case constant.UPDATE_ENTITY:
  32.             return reduceUpdateEntity(state, action);
  33.     }
  34. };
  35.  
  36. /**
  37.  * Creating Redux store with one main reducer. - Instead of using
  38.  * plain switch statement I'm using 'redux-actions' module from GitHub.
  39.  */
  40. const store = Redux.createStore(
  41.     Redux.CombineReducers({
  42.         main: mainReducer,     
  43.     }),
  44. );
  45.  
  46.  
  47. store.dispatch({
  48.     type: constant.REPLACE_ENTITY,
  49.     payload: 'replacing animals',
  50.     meta: {
  51.         model: 'collection', // <-- This is where I'd like to reuse my constants
  52.         entity: 'animals', // <-- This is where I'd like to reuse my constants
  53.     },
  54. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement