src/fetch-data.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.17

Size

Lines of Code 62
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 46
mnd 1
bc 1
fnc 6
dl 0
loc 62
bpm 0.1666
cpm 1.1666
noi 0
c 0
b 0
f 0
rs 10

6 Functions

Rating   Name   Duplication   Size   Complexity  
A FetchData.componentWillReceiveProps 0 3 1
A fetch-data.js ➔ mapStateToProps 0 4 1
A fetch-data.js ➔ mapDispatchToProps 0 4 1
A FetchData.render 0 3 1
A FetchData.componentWillMount 0 3 2
A FetchData.fetchData 0 10 1
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