Source: reducer.js

  1. /** @module reducer */
  2. /**
  3. * An Immutable Map.
  4. * @typedef {Object} Map
  5. */
  6. import {fromJS, Iterable, Map} from 'immutable';
  7. /**
  8. * Create a reducer using a provided reducer combined with Nux's internal reducer. An initial vDOM UI object
  9. * can be passed in to initialize the store with, as well as any options to further configure the reducer.
  10. * The reducer returned is intended for use with a Redux store.
  11. *
  12. * @author Mark Nutter <marknutter@gmail.com>
  13. * @summary Generate a Nux reducer function given a custom reducer function.
  14. *
  15. * @param {Object} [initialUI] The initial UI vDOM object which will become the first state to be rendered.
  16. * @return {Function} The core reducer function to be used to initialize a Redux store
  17. */
  18. export function reducer(initialUI) {
  19. const initialState = fromJS(initialUI, function (key, value) {
  20. var isIndexed = Iterable.isIndexed(value);
  21. return isIndexed ? value.toList() : value.toOrderedMap();
  22. });
  23. return function(state = initialState, action) {
  24. let nextState;
  25. switch (action.type) {
  26. case '_UPDATE_INPUT_VALUE':
  27. nextState = state.setIn(action.pathArray.concat(['props', 'value']), action.val);
  28. break;
  29. default:
  30. nextState = state;
  31. break;
  32. }
  33. return nextState;
  34. }
  35. }