Total Complexity | 7 |
Complexity/F | 1.17 |
Lines of Code | 62 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import React, { Component, PropTypes } from 'react'; |
||
2 | import { bindActionCreators } from 'redux'; |
||
3 | import { connect } from 'react-redux'; |
||
4 | import { RouterContext } from 'react-router'; |
||
5 | import { doneFetching } from './module'; |
||
6 | import { grabPromises } from './utils'; |
||
7 | |||
8 | export class FetchData extends Component { |
||
9 | componentWillMount() { |
||
10 | if (!this.props.isFetched) { |
||
11 | this.fetchData(this.props); |
||
12 | } |
||
13 | } |
||
14 | |||
15 | componentWillReceiveProps(nextProps) { |
||
16 | this.fetchData(nextProps); |
||
17 | } |
||
18 | |||
19 | fetchData(props) { |
||
20 | const promises = grabPromises( |
||
21 | props.components, |
||
22 | props.params, |
||
23 | this.context.store |
||
24 | ); |
||
25 | |||
26 | Promise.all(promises).then(() => { |
||
27 | this.props.actions.doneFetching(); |
||
28 | }); |
||
29 | } |
||
30 | |||
31 | render() { |
||
32 | return <RouterContext {...this.props}/>; |
||
33 | } |
||
34 | } |
||
35 | |||
36 | FetchData.propTypes = { |
||
37 | isFetched: PropTypes.bool.isRequired, |
||
38 | actions: PropTypes.object |
||
39 | }; |
||
40 | |||
41 | FetchData.defaultProps = { |
||
42 | isFetched: false |
||
43 | }; |
||
44 | |||
45 | FetchData.contextTypes = { |
||
46 | store: PropTypes.object.isRequired |
||
47 | }; |
||
48 | |||
49 | function mapStateToProps(state) { |
||
50 | return { |
||
51 | isFetched: state.fetching.fetched |
||
52 | }; |
||
53 | } |
||
54 | |||
55 | function mapDispatchToProps(dispatch) { |
||
56 | return { |
||
57 | actions: bindActionCreators({ doneFetching }, dispatch) |
||
58 | }; |
||
59 | } |
||
60 | |||
61 | export default connect(mapStateToProps, mapDispatchToProps)(FetchData); |
||
62 |