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

const PrivateRoute = ({ component, isAuthenticated, ...rest }) => (
	<Route {...rest} render={
		(props) => isAuthenticated ?
			(React.createElement(component, { ...props })) :
			(<Redirect to={{ pathname: "/login", state: { from: props.location } }} />)
	} />
);

export default connect(
	(state, ownProps) => {
		const  isAuthenticated  = state.auth.isAuthenticated || false;
		return Object.assign({}, { isAuthenticated: isAuthenticated }, ownProps);
	}
)(PrivateRoute);
