import React from 'react'
import { Redirect, Route } from 'react-router-dom'
import { connect } from 'react-redux'
 
const PrivateRoute = ({children, currentUser, ...restOfParams}) => (
      <Route {...restOfParams} render={
            ({location})=>{
                // console.log(location)
                return currentUser != null ? children : 
                    <Redirect to={{ 
                                pathname: '/',
                                state: { from: location }          
                            }}
                    
                    />
            }
        }/>
    )

const mapStateToProps = ({user}) => ({
    currentUser: user.currentUser
})

export default connect(mapStateToProps)(PrivateRoute)
