feat: Select article list in sight
This commit is contained in:
@ -9,34 +9,57 @@ import {
|
||||
import { authStore } from "@shared";
|
||||
import { Layout } from "@widgets";
|
||||
|
||||
import { Navigate, Outlet, Route, Routes } from "react-router-dom";
|
||||
import { Navigate, Outlet, Route, Routes, useLocation } from "react-router-dom";
|
||||
|
||||
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
|
||||
const PublicRoute = ({ children }: { children: React.ReactNode }) => {
|
||||
const { isAuthenticated } = authStore;
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/login" />;
|
||||
if (isAuthenticated) {
|
||||
return <Navigate to="/sight" />;
|
||||
}
|
||||
return children;
|
||||
};
|
||||
|
||||
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
|
||||
const { isAuthenticated } = authStore;
|
||||
const pathname = useLocation();
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/login" />;
|
||||
}
|
||||
if (pathname.pathname === "/") {
|
||||
return <Navigate to="/sight" />;
|
||||
}
|
||||
return children;
|
||||
};
|
||||
|
||||
export const Router = () => {
|
||||
return (
|
||||
<Routes>
|
||||
<Route
|
||||
path="/login"
|
||||
element={
|
||||
<PublicRoute>
|
||||
<LoginPage />
|
||||
</PublicRoute>
|
||||
}
|
||||
/>
|
||||
|
||||
{/* Protected routes with layout */}
|
||||
<Route
|
||||
path="/"
|
||||
element={
|
||||
<Layout>
|
||||
<Outlet />
|
||||
</Layout>
|
||||
<ProtectedRoute>
|
||||
<Layout>
|
||||
<Outlet />
|
||||
</Layout>
|
||||
</ProtectedRoute>
|
||||
}
|
||||
>
|
||||
<Route path="/" element={<MainPage />} />
|
||||
<Route path="/sight" element={<SightPage />} />
|
||||
<Route path="/sight/:id" element={<EditSightPage />} />
|
||||
<Route path="/sight/create" element={<CreateSightPage />} />
|
||||
<Route path="/devices" element={<DevicesPage />} />
|
||||
<Route index element={<MainPage />} />
|
||||
<Route path="sight" element={<SightPage />} />
|
||||
<Route path="sight/:id" element={<EditSightPage />} />
|
||||
<Route path="sight/create" element={<CreateSightPage />} />
|
||||
<Route path="devices" element={<DevicesPage />} />
|
||||
</Route>
|
||||
|
||||
<Route path="/login" element={<LoginPage />} />
|
||||
</Routes>
|
||||
);
|
||||
};
|
||||
|
Reference in New Issue
Block a user