import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import { isInRole, Role } from './PrivateContent'

type PrivateRouteProps = {
  children: any
  roles: Role[]
  [other: string]: any
}

export const PrivateRoute: React.FC<PrivateRouteProps> = ({ children, roles, ...other }) => {
  const token = localStorage['token']
  return (
    <Route
      {...other}
      render={({ location }) => token && isInRole(roles) ? children : (
        <Redirect to={{ pathname: "/login", state: { from: location } }} />)
      }
    />
  )
}
