| Total Complexity | 10 |
| Complexity/F | 1.25 |
| Lines of Code | 42 |
| Function Count | 8 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 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 |