import React, { Component } from 'react';
import Header from 'staff-legacy/components/header';
import Nav from 'staff-legacy/components/nav';

import UI from 'staff-legacy/components/ui';

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as AuthActions from 'core/actions/auth';
import { history } from 'staff-legacy/config/routes';

const {
  Grid: { Col, Container, Row },
} = UI;

class RootContainer extends Component {
  componentWillReceiveProps(props) {
    if (!props.auth.token) {
      history.push(`/login`);
    }
  }

  render() {
    return (
      <main>
        <Header actions={this.props.actions} />

        <Container>
          <Row>
            <Col md="2">
              <Nav />
            </Col>
            <Col md="10">
              {this.props.children}
            </Col>
          </Row>
        </Container>
      </main>
    );
  }
}

const mapState = ({ auth }) => ({
  auth,
});

const mapDispatch = (dispatch) => ({
  actions: bindActionCreators(AuthActions, dispatch),
});


export default connect(mapState, mapDispatch)(RootContainer);
