import React from 'react';
import { Route } from "react-router-dom";
import { connect } from "react-redux";
import jsCookie from 'js-cookie'
import Loading from './loading'


const PrivateRoute = ({component: Component, path, user, ...rest}) => {

    let authenticated = jsCookie.get('token') && jsCookie.get('uid')

    return (
        <Route {...rest} render={props => {
            return authenticated
                ? <Component {...props}/>
                : <Loading state={{from: props.location}}/>
        }}/>
    );
};

export default connect(
    state => ({user: state.user}),
    null
)(PrivateRoute);