Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. /*eslint no-console:0 */
  2. import React from 'react';
  3.  
  4. const DEFAULT_STYLE = 'font-weight:bold; color:';
  5. const PREV_STATE_STYLE = `${DEFAULT_STYLE}gray`;
  6. const ACTION_STYLE = `${DEFAULT_STYLE}blue`;
  7. const NEXT_STATE_STYLE = `${DEFAULT_STYLE}green`;
  8.  
  9. const showLog = (message, params, styles) => console.log(`%c${message}`, styles, params);
  10.  
  11. const defaultSelector = state => state;
  12. const defaultDispatcher = null;
  13. const defaultReducer = defaultSelector;
  14.  
  15. export const TAB_ACTION_SYM_MARK = '$$TabAction';
  16.  
  17. export default (selector = defaultSelector, dispatcher = defaultDispatcher, reducer = defaultReducer) => {
  18.  
  19. return Container => {
  20.  
  21. return class TabConnected extends React.Component {
  22.  
  23. constructor(props) {
  24. super(props);
  25. this.state = reducer();
  26. }
  27.  
  28. simulateReducer = (action) => {
  29.  
  30. const prevState = this.state;
  31. showLog('prev state', prevState, PREV_STATE_STYLE);
  32. showLog('action', action, ACTION_STYLE);
  33. const nextState = reducer(prevState, action);
  34. showLog('next state', nextState, NEXT_STATE_STYLE);
  35. this.setState(nextState);
  36. };
  37.  
  38. render() {
  39.  
  40. const { store, context: { url } } = this.props;
  41.  
  42. const dispatchToProps = dispatcher(action => {
  43.  
  44. if(!action[Symbol.for(TAB_ACTION_SYM_MARK)]) {
  45. store.dispatch(action);
  46. return;
  47. }
  48.  
  49. console.groupCollapsed(`action from tab @ ${action.type}`, url);
  50. this.simulateReducer(action);
  51. console.groupEnd();
  52. });
  53.  
  54. const selectedProps = selector(this.props.store.getState());
  55. return <Container {...selectedProps} {...this.state} {...dispatchToProps} />
  56. }
  57.  
  58. };
  59. }
  60. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement