(ComposedComponent)
| 22 | } |
| 23 | |
| 24 | export const fetchEnhance = (ComposedComponent) => { |
| 25 | class Fetch extends Component { |
| 26 | constructor (props) { |
| 27 | super(props); |
| 28 | |
| 29 | this.state = { |
| 30 | data: undefined, |
| 31 | fetchStatus: FETCH_SUCCESS |
| 32 | } |
| 33 | |
| 34 | this.getSelected = this.getSelected.bind(this) |
| 35 | } |
| 36 | |
| 37 | componentWillMount () { |
| 38 | this._isMounted = true; |
| 39 | let { data, fetch } = this.props; |
| 40 | if (data) { |
| 41 | this.handleData(data); |
| 42 | } |
| 43 | if (fetch) { |
| 44 | this.fetchData(fetch); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | componentDidMount () { |
| 49 | let component = this.component; |
| 50 | Object.keys(component).forEach((key) => { |
| 51 | if (!this.hasOwnProperty(key)) { |
| 52 | let func = component[key]; |
| 53 | if (typeof func === 'function') { |
| 54 | this[key] = func; |
| 55 | } |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | componentWillReceiveProps (nextProps) { |
| 61 | if (!deepEqual(this.props.data, nextProps.data)) { |
| 62 | this.handleData(nextProps.data); |
| 63 | } |
| 64 | if (!deepEqual(this.props.fetch, nextProps.fetch)) { |
| 65 | this.fetchData(nextProps.fetch); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | componentWillUnmount () { |
| 70 | this._isMounted = false; |
| 71 | } |
| 72 | |
| 73 | handleData (data) { |
| 74 | // old dataSource api |
| 75 | if (typeof data === 'function') { |
| 76 | this.setState({ data: undefined, fetchStatus: FETCH_PENDING }); |
| 77 | data.then((res) => { |
| 78 | if (this._isMounted) { |
| 79 | this.setState({ data: clone(res) }); |
| 80 | } |
| 81 | })(); |
no outgoing calls
no test coverage detected
searching dependent graphs…