File size: 6,697 Bytes
c2efbe6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | import React, { useState } from "react"
import "./App.css"
import { BrowserRouter as Router, Routes, Route, useLocation, useNavigate } from "react-router-dom"
import { Provider, useDispatch, useSelector } from "react-redux"
import store from "./store/store"
import { ToastContainer } from "react-toastify"
import "react-toastify/dist/ReactToastify.css"
import { motion, AnimatePresence } from "framer-motion"
import { setUser, setLoading, logout } from "./store/slices/authSlice";
import { useEffect } from "react";
import { useLogoutHandler } from "./hooks/useLogoutHandler";
import Header from "./components/Header"
import MenuBar from "./components/MenuBar"
import Footer from "./components/Footer"
import Carousel from "./components/Carousel"
import ItemSection from "./components/ItemSection"
import LoadingScreen from "./components/LoadingScreen"
import ScrollToTop from "./components/ScrollToTop"
import LiveChat from "./components/LiveChat"
import AIChatAssistant from "./components/AIChatAssistant"
import Home from "./pages/Home"
import ProductDetail from "./pages/ProductDetail"
import ProductList from "./pages/ProductList"
import CartPage from "./pages/CartPage"
import LoginPage from "./pages/LoginPage"
import RegisterPage from "./pages/RegisterPage"
import ProfilePage from "./pages/ProfilePage"
import CheckoutPage from "./pages/CheckoutPage"
import WishlistPage from "./pages/WishlistPage"
import OrderPage from "./pages/OrderPage"
import AdminLoginPage from "./pages/AdminLoginPage"
import AdminDashboard from "./pages/AdminDashboard"
import FloatingElements from "./components/FloatingElements"
import ParticleBackground from "./components/ParticleBackground"
function AppContent() {
const [isLoading, setIsLoading] = useState(true)
const [showLiveChat, setShowLiveChat] = useState(false)
const [showAIAssistant, setShowAIAssistant] = useState(false)
const location = useLocation()
const navigate = useNavigate()
const dispatch = useDispatch();
const { isLoggedIn, userData } = useSelector((state) => state.auth);
const authLoading = useSelector((state) => state.auth.loading);
useLogoutHandler();
useEffect(() => {
const hydrateAuth = async () => {
dispatch(setLoading(true));
try {
const existingUserData = localStorage.getItem('userData');
const existingToken = localStorage.getItem('token');
if (existingUserData && existingToken) {
try {
const userData = JSON.parse(existingUserData);
if (userData._id && userData.email) {
dispatch(setUser(userData));
dispatch(setLoading(false));
return;
}
} catch (e) {
console.error('Error parsing stored user data:', e);
localStorage.removeItem('userData');
localStorage.removeItem('token');
}
}
dispatch(setLoading(false));
} catch (e) {
console.error('Auth hydration error:', e);
localStorage.removeItem('userData');
localStorage.removeItem('token');
dispatch(setLoading(false));
}
};
hydrateAuth();
}, [dispatch]);
const shouldHidePublicSections =
[
"/login",
"/register",
"/cart",
"/checkout",
"/search",
"/productlist",
"/profile",
"/wishlist",
"/orders"
].includes(location.pathname.toLowerCase()) ||
location.pathname.startsWith("/product/");
const hideHeader = location.pathname.startsWith("/admin/");
React.useEffect(() => {
const timer = setTimeout(() => setIsLoading(false), 2000)
return () => clearTimeout(timer)
}, [setIsLoading])
useEffect(() => {
if (!authLoading && !isLoggedIn) {
const protectedRoutes = ['/profile', '/wishlist', '/orders', '/checkout', '/cart'];
if (protectedRoutes.includes(location.pathname)) {
window.location.href = '/';
}
}
}, [isLoggedIn, authLoading, location.pathname]);
if (isLoading || authLoading) {
return <LoadingScreen />
}
return (
<div>
<FloatingElements />
<ParticleBackground />
<ToastContainer
position="top-center"
toastClassName="Premium-toast"
progressClassName="Premium-progress"
hideProgressBar={false}
newestOnTop
closeOnClick
rtl={false}
pauseOnFocusLoss
draggable
pauseOnHover
/>
{}
{!hideHeader && (
<Header
onOpenLiveChat={() => setShowLiveChat(true)}
onOpenAIAssistant={() => setShowAIAssistant(true)}
/>
)}
{}
{!hideHeader && !shouldHidePublicSections && (
<>
<Carousel />
<MenuBar />
<ItemSection />
<Footer />
</>
)}
{}
<AnimatePresence mode="wait">
<motion.div
key={location.pathname}
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: -20 }}
transition={{ duration: 0.4, ease: "easeInOut" }}
>
<Routes>
<Route path="/" element={<div />} />
<Route path="/search" element={<Home />} />
<Route
path="/product/:id"
element={
<ProductDetail />
}
/>
<Route
path="/productlist"
element={<ProductList />}
/>
<Route path="/cart" element={<CartPage />} />
<Route path="/checkout" element={<CheckoutPage />} />
<Route
path="/wishlist"
element={<WishlistPage />}
/>
<Route path="/orders" element={<OrderPage />} />
<Route path="/login" element={<LoginPage />} />
<Route path="/register" element={<RegisterPage />} />
<Route path="/profile" element={<ProfilePage />} />
<Route path="/admin/login" element={<AdminLoginPage />} />
<Route path="/admin/dashboard" element={<AdminDashboard />} />
</Routes>
</motion.div>
</AnimatePresence>
{ }
{!(location.pathname.toLowerCase() === "/profile") && <ScrollToTop />}
{ }
<LiveChat key="livechat" isOpen={showLiveChat} setIsOpen={setShowLiveChat} />
<AIChatAssistant key="aiassistant" isOpen={showAIAssistant} setIsOpen={setShowAIAssistant} style={{ bottom: '140px' }} />
</div>
)
}
function App() {
return (
<Provider store={store}>
<Router>
<AppContent />
</Router>
</Provider>
)
}
export default App |