Initial commit
This commit is contained in:
75
src/App.jsx
Normal file
75
src/App.jsx
Normal file
@@ -0,0 +1,75 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Routes, Route, Navigate } from 'react-router-dom';
|
||||
import Header from './components/Header';
|
||||
import Footer from './components/Footer';
|
||||
import Home from './pages/Home';
|
||||
import Shop from './pages/Shop';
|
||||
import About from './pages/About';
|
||||
import Contact from './pages/Contact';
|
||||
import Auth from './pages/Auth';
|
||||
import VerifyEmail from './pages/VerifyEmail';
|
||||
import VerifyEmailSuccess from './pages/VerifyEmailSuccess';
|
||||
import ResetPassword from './pages/ResetPassword';
|
||||
import authService from './services/authService';
|
||||
import './App.css';
|
||||
// import admin from './pages/Admin';
|
||||
import Product from './pages/ProductDetails';
|
||||
import ProductDetails from './pages/ProductDetails';
|
||||
import Cart from './pages/Cart';
|
||||
import Admin from './pages/Admin';
|
||||
|
||||
function App() {
|
||||
const [user, setUser] = useState(null);
|
||||
const [isAdmin, setIsAdmin] = useState(false);
|
||||
|
||||
const fetchProfile = async () => {
|
||||
try {
|
||||
const response = await authService.getProfile();
|
||||
setUser(response.user);
|
||||
setIsAdmin(response.user.role === 'admin');
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch profile:', error);
|
||||
setUser(null);
|
||||
setIsAdmin(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchProfile();
|
||||
}, []);
|
||||
|
||||
const handleLogin = async () => {
|
||||
await fetchProfile();
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
authService.logout();
|
||||
setUser(null);
|
||||
setIsAdmin(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<Header user={user} onLogout={handleLogout} isAdmin={isAdmin} />
|
||||
<main>
|
||||
<Routes>
|
||||
<Route path="/" element={<Home />} />
|
||||
<Route path="/shop" element={<Shop />} />
|
||||
<Route path="/about" element={<About />} />
|
||||
<Route path="/contact" element={<Contact />} />
|
||||
<Route path="/auth" element={user ? <Navigate to="/" replace /> : <Auth onLogin={handleLogin} />} />
|
||||
<Route path="/verify-email" element={<VerifyEmail />} />
|
||||
<Route path="/verify-email-success" element={<VerifyEmailSuccess />} />
|
||||
<Route path="/reset-password" element={<ResetPassword />} />
|
||||
{/* <Route path="/admin" element={<admin />} /> */}
|
||||
<Route path="/productdetail" element={<ProductDetails/>}/>
|
||||
<Route path="/cart" element={<Cart/>} />
|
||||
<Route path="/admin" element={isAdmin ? <Admin/> : <Navigate to="/auth" replace />} />
|
||||
</Routes>
|
||||
</main>
|
||||
<Footer />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
Reference in New Issue
Block a user