import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import { connect } from 'react-redux';

const PrivateRoute = ({ component: Component, auth, ...rest }) => (
  <Route {...rest} render={props => {
    if (!auth.isLoggedIn) {
      return <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
    }

    return <Component {...props} />
  }} />
);

export default connect(
  (state) => ({ auth: state.auth }),
)(PrivateRoute)
