projeto-hit/frontend/src/routes/Route.js

43 lines
1.2 KiB
JavaScript

import React, { useContext, Suspense, lazy } from "react";
import { Route as RouterRoute, Redirect } from "react-router-dom";
import { AuthContext } from "../context/Auth/AuthContext";
import BackdropLoading from "../components/BackdropLoading";
// Exemplo de como você carregaria componentes de forma lazy
const Dashboard = lazy(() => import("../pages/Dashboard"));
const Login = lazy(() => import("../pages/Login"));
const Signup = lazy(() => import("../pages/Signup"));
const Route = ({ component: Component, isPrivate = false, ...rest }) => {
const { isAuth, loading } = useContext(AuthContext);
if (!isAuth && isPrivate) {
return (
<>
{loading && <BackdropLoading />}
<Redirect to={{ pathname: "/login", state: { from: rest.location } }} />
</>
);
}
if (isAuth && !isPrivate) {
return (
<>
{loading && <BackdropLoading />}
<Redirect to={{ pathname: "/", state: { from: rest.location } }} />
</>
);
}
return (
<>
{loading && <BackdropLoading />}
<Suspense fallback={<BackdropLoading />}>
<RouterRoute {...rest} component={Component} />
</Suspense>
</>
);
};
export default Route;