Completed
Push — master ( 822908...45ea40 )
by Junior
25s
created

HomeContainer.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 1
rs 10
1
import { connect } from 'react-redux';
2
import {
3
  compose,
4
  withHandlers,
5
  branch,
6
  lifecycle,
7
  renderComponent,
8
  renderNothing
9
} from 'recompose';
10
import HomeComponent from 'components/HomeComponent';
11
import LoginContainer from 'containers/LoginContainer';
12
import { setToken, setUsername } from 'modules/Login';
13
import getToken from 'services/token';
14
15
const handleLogout = ({ dispatch }) => () => dispatch(setUsername(null));
16
17
const mapStateToProps = state => ({
18
  token: state.Login.token,
19
  username: state.Login.username
20
});
21
22
export default compose(
23
  connect(mapStateToProps),
24
  withHandlers({
25
    handleLogout
26
  }),
27
  lifecycle({
28
    componentDidMount() {
29
      const { dispatch, token } = this.props;
30
      if (!token) {
31
        getToken().then(data => dispatch(setToken(data)));
32
      }
33
    },
34
    componentWillReceiveProps(nextProps) {
35
      if (nextProps.username) {
36
        window.location.reload();
37
      }
38
    }
39
  }),
40
  branch(({ username }) => !username, renderComponent(LoginContainer)),
41
  branch(({ token }) => !token, renderComponent(renderNothing()))
42
)(HomeComponent);
43