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

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

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

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

export default SwitchRoute;
