import { useEffect, useState } from "react";
import { UseRoute } from "../route";
import { jwtDecode } from "../utils";
import { PrivateRoute } from "../route";

const SwitchRoute = () => {
  const [userName, setUserName] = useState('user');

  useEffect( () => {
    const token = localStorage.getItem('authToken'); 
    //
    if (token) { 
      const name = jwtDecode(token)['sub']['acl']; //console.log('name', jwtDecode(token)['sub']);
      name.includes('admin') ? setUserName(name ):  setUserName('user'); 
     
    }
  }, [localStorage.getItem('authToken')])

  return ( 
    < >
    {userName === 'user' ? <UseRoute/>: <PrivateRoute/>}
    </>
  )
}

export default SwitchRoute; 