|
| 1 | +import React, { Component, PropTypes } from 'react'; |
| 2 | +import { bindActionCreators } from 'redux'; |
| 3 | +import { |
| 4 | + LOCAL, |
| 5 | + LOCAL_ACTION |
| 6 | +} from './actionTypes'; |
| 7 | + |
| 8 | +// TODO: move in an utilities module |
| 9 | +function getDisplayName(Comp) { |
| 10 | + return Comp.displayName || Comp.name || 'Component'; |
| 11 | +} |
| 12 | + |
| 13 | +function createComponentStateDecorator(componentStoreConfig) { |
| 14 | + // TODO: add validation of shape fo the storeConfig |
| 15 | + |
| 16 | + return (DecoratedComponent) => |
| 17 | + class ReduxComponentState extends Component { |
| 18 | + |
| 19 | + // **************************************************************** |
| 20 | + // Component initialization |
| 21 | + // **************************************************************** |
| 22 | + |
| 23 | + static displayName = `ReduxComponentState(${getDisplayName(DecoratedComponent)})`; |
| 24 | + static DecoratedComponent = DecoratedComponent; |
| 25 | + |
| 26 | + static contextTypes = { |
| 27 | + store: PropTypes.shape({ |
| 28 | + componentState: PropTypes.shape({ |
| 29 | + subscribe: PropTypes.func, |
| 30 | + unsubscribe: PropTypes.func, |
| 31 | + getState: PropTypes.func |
| 32 | + }).isRequired |
| 33 | + }) |
| 34 | + }; |
| 35 | + |
| 36 | + // static propTypes = { |
| 37 | + // componentStoreConfig: PropTypes.shape({ |
| 38 | + // getKey: PropTypes.func, |
| 39 | + // reducers: PropTypes.object, |
| 40 | + // actions: PropTypes.object, |
| 41 | + // getInitialState: PropTypes.func, |
| 42 | + // shared: PropTypes.bool |
| 43 | + // }).isRequired |
| 44 | + // }; |
| 45 | + |
| 46 | + constructor(props, context) { |
| 47 | + super(props, context); |
| 48 | + this.state = this.getComponentStoreState(); |
| 49 | + } |
| 50 | + |
| 51 | + // **************************************************************** |
| 52 | + // Component LifeCycle |
| 53 | + // **************************************************************** |
| 54 | + |
| 55 | + componentDidMount() { |
| 56 | + const { |
| 57 | + getKey, |
| 58 | + reducers, |
| 59 | + actions, |
| 60 | + getInitialState, |
| 61 | + shared |
| 62 | + } = componentStoreConfig; |
| 63 | + |
| 64 | + let initialState = (getInitialState || (() => undefined))(this.props); |
| 65 | + this.unsubscribe = this.context.store.componentState.subscribe({ |
| 66 | + key: getKey(this.props), |
| 67 | + reducers, |
| 68 | + initialState, |
| 69 | + shared, |
| 70 | + onChange: this.handleChange |
| 71 | + }); |
| 72 | + this.handleChange(); |
| 73 | + } |
| 74 | + |
| 75 | + componentWillUnmount() { |
| 76 | + this.unsubscribe(); |
| 77 | + } |
| 78 | + |
| 79 | + // **************************************************************** |
| 80 | + // Internal API |
| 81 | + // **************************************************************** |
| 82 | + |
| 83 | + getComponentStateKey(){ |
| 84 | + return componentStoreConfig.getKey(this.props); |
| 85 | + } |
| 86 | + |
| 87 | + getComponentStoreState(){ |
| 88 | + return this.context.store.componentState.getState(this.getComponentStateKey()); |
| 89 | + } |
| 90 | + |
| 91 | + handleChange = () => { |
| 92 | + // TODO: add conditions to prevent rendering if the |
| 93 | + // state is not changed |
| 94 | + this.setState(this.getComponentStoreState()); |
| 95 | + } |
| 96 | + |
| 97 | + dispatchLocal = (action) => { |
| 98 | + this.context.store.dispatch({ |
| 99 | + type: LOCAL, |
| 100 | + subType: LOCAL_ACTION, |
| 101 | + key: this.getComponentStateKey(), |
| 102 | + data: action |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + // **************************************************************** |
| 107 | + // Render component |
| 108 | + // **************************************************************** |
| 109 | + |
| 110 | + render() { |
| 111 | + const { ...stuff } = this.props; |
| 112 | + let actions = componentStoreConfig.actions || {}; |
| 113 | + const boundActionCreators = bindActionCreators(actions, this.dispatchLocal); |
| 114 | + |
| 115 | + // FIXME: temporary fix because first render of the component fires |
| 116 | + // before the component store is initialized |
| 117 | + if(!this.state) return <span />; |
| 118 | + |
| 119 | + // const renderDecoratedComp = () => |
| 120 | + // <DecoratedComponent |
| 121 | + // dispatch={this.dispatchLocal} |
| 122 | + // {...this.stuff} |
| 123 | + // {...this.state} |
| 124 | + // {...boundActionCreators} |
| 125 | + // />; |
| 126 | + |
| 127 | + return ( |
| 128 | + <div> |
| 129 | + <DecoratedComponent ref="ReduxComponentStateWrapper" |
| 130 | + dispatch={this.dispatchLocal} |
| 131 | + {...this.stuff} |
| 132 | + {...this.state} |
| 133 | + {...boundActionCreators} |
| 134 | + /> |
| 135 | + </div> |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +export default function reduxComponentState(componentStoreConfig) { |
| 143 | + const decorator = createComponentStateDecorator(componentStoreConfig); |
| 144 | + // TODO: is required this intermediate function? |
| 145 | + // there's need to manipulate the decorator here? |
| 146 | + return decorator; |
| 147 | +} |
0 commit comments