import React, { Component } from 'react'
import { Route, Redirect } from 'react-router-dom'

import { token } from '../common/token'

export default class navigationGuards extends Component {
    render() {
        const { routerConfig, location } = this.props
        const { pathname } = location

        const targetRouterConfig = routerConfig.find((item) => {
            return item.path.replace(/\s*/g, '') === pathname
        })

        if (token.value) {
            if (pathname === '/login') {
                return <Redirect to='/' push={false} />
            }
        } else if (!token.value && pathname !== '/login') {
            return <Redirect to='/login' push={false} />
        }

        const { component } = targetRouterConfig;
        return <Route exact path={pathname} component={component} />
    }
}