import { useEffect } from 'react' import { useSelector, useDispatch } from 'react-redux' import { HashRouter, Switch, Route, Redirect } from 'react-router-dom' import PropTypes from 'prop-types' import CircularProgress from '@material-ui/core/CircularProgress' import Navbar from './components/Shared/Navbar' import Dashboard from './pages/Dashboard' import Gallery from './pages/Gallery' import Home from './pages/Home' import Login from './pages/Login' import NotFound from './pages/NotFound' import SchematicEditor from './pages/SchematicEditor' import SignUp from './pages/signUp' import { loadUser } from './redux/authSlice' // Controls Private routes, this are accessible for authenticated users. [ e.g : dashboard ] // and restricted routes disabled for authenticated users. [ e.g : login , signup ] const PrivateRoute = ({ component: Component, ...rest }) => { const isAuthenticated = useSelector(state => state.auth.isAuthenticated) const isLoading = useSelector(state => state.auth.isLoading) const dispatch = useDispatch() useEffect(() => { dispatch(loadUser()) }, [dispatch]) return ( { if (isLoading) { return } else if (!isAuthenticated) { return } else { return } }} /> ) } PrivateRoute.propTypes = { component: PropTypes.func } // Public routes accessible to all users. [ e.g. editor, gallery ] const PublicRoute = ({ component: Component, restricted, nav, ...rest }) => { const isAuthenticated = useSelector(state => state.auth.isAuthenticated) const isLoading = useSelector(state => state.auth.isLoading) const dispatch = useDispatch() useEffect(() => { dispatch(loadUser()) }, [dispatch]) return ( { if (isLoading) { return } else if (isAuthenticated && restricted) { return } else if (nav) { return (<>) } else { return } }} /> ) } PublicRoute.propTypes = { component: PropTypes.func, nav: PropTypes.bool, restricted: PropTypes.bool } const App = () => { return ( // Handles Routing for an application {localStorage.getItem(process.env.REACT_APP_NAME + '_token') !== null ? : } ) } export default App