Completed
Push — master ( 99cfb1...822908 )
by Junior
10s
created

lifecycle.componentDidMount   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 1 1
1
import { connect } from 'react-redux';
2
import {
3
  compose,
4
  branch,
5
  lifecycle,
6
  renderComponent,
7
  renderNothing
8
} from 'recompose';
9
import HomeComponent from 'components/HomeComponent';
10
import LoginContainer from 'containers/LoginContainer';
11
import { setToken } from 'modules/Login';
12
import getToken from 'services/token';
13
14
const mapStateToProps = state => ({
15
  token: state.Login.token,
16
  username: state.Login.username
17
});
18
19
export default compose(
20
  connect(mapStateToProps),
21
  lifecycle({
22
    componentDidMount() {
23
      const { dispatch, token } = this.props;
24
      if (!token) {
25
        getToken().then(data => dispatch(setToken(data)));
26
      }
27
    },
28
    componentWillReceiveProps(nextProps) {
29
      if (nextProps.username) {
30
        window.location.reload();
31
      }
32
    }
33
  }),
34
  branch(({ username }) => !username, renderComponent(LoginContainer)),
35
  branch(({ token }) => !token, renderComponent(renderNothing()))
36
)(HomeComponent);
37