80 lines
2.1 KiB
React
80 lines
2.1 KiB
React
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
|
|
import Main from "./pages/Main";
|
|
import AddItem from "./pages/AddItem";
|
|
import AuthForm from "./components/AuthForm";
|
|
import About from "./pages/About";
|
|
import Shop from "./pages/Shop";
|
|
import NotFoundPage from "./pages/NotFoundPage";
|
|
import Contact from "./pages/Contact";
|
|
import Cart from "./pages/Cart";
|
|
import Profile from "./pages/Profile";
|
|
import ItemPage from "./pages/ItemPage";
|
|
import AdminPage from "./pages/AdminPage";
|
|
import AdminOrderDetail from "./pages/AdminOrderPage";
|
|
|
|
const PrivateRoute = ({ children }) => {
|
|
const token = localStorage.getItem("token");
|
|
|
|
if (!token) {
|
|
return <Navigate to="/login" replace />;
|
|
}
|
|
return children;
|
|
};
|
|
|
|
const AdminRoute = ({ children }) => {
|
|
const token = localStorage.getItem("token");
|
|
const user = JSON.parse(localStorage.getItem("user") || "{}");
|
|
|
|
if (!token) {
|
|
return <Navigate to="/login" replace />;
|
|
}
|
|
|
|
if (user.role !== "Админ") {
|
|
return <Navigate to="/" replace />;
|
|
}
|
|
|
|
return children;
|
|
};
|
|
|
|
export default function App() {
|
|
return (
|
|
<BrowserRouter>
|
|
<Routes>
|
|
<Route path="/login" element={<AuthForm />} />
|
|
<Route path="/" element={<Main />} />
|
|
<Route path="/add_item" element={<AddItem />} />
|
|
<Route path="/about" element={<About />} />
|
|
<Route path="/shop" element={<Shop />} />
|
|
<Route path="/contact" element={<Contact />} />
|
|
<Route path="/orders" element={<Profile />} />
|
|
<Route
|
|
path="/cart"
|
|
element={
|
|
<PrivateRoute>
|
|
<Cart />
|
|
</PrivateRoute>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/admin"
|
|
element={
|
|
<AdminRoute>
|
|
<AdminPage />
|
|
</AdminRoute>
|
|
}
|
|
/>
|
|
<Route
|
|
path="/admin/orders/:id"
|
|
element={
|
|
<AdminRoute>
|
|
<AdminOrderDetail />
|
|
</AdminRoute>
|
|
}
|
|
/>
|
|
<Route path="items/:id" element={<ItemPage />} />
|
|
<Route path="*" element={<NotFoundPage />} />
|
|
</Routes>
|
|
</BrowserRouter>
|
|
);
|
|
}
|