11// src/app/page.jsx
22"use client" ;
3- import { useEffect } from "react" ;
3+ import { useState , useEffect } from "react" ;
44import { useRouter } from "next/navigation" ;
55import { useSelector , useDispatch } from "react-redux" ;
66import { fetchEvents } from "@/redux/slices/eventSlice" ;
@@ -9,19 +9,27 @@ export default function Home() {
99 const router = useRouter ( ) ;
1010 const { isAuthenticated } = useSelector ( ( state ) => state . auth ) ;
1111 const dispatch = useDispatch ( ) ;
12+ const [ isLoading , setIsLoading ] = useState ( true ) ;
1213
1314 // Fetch all events on component mount
1415 useEffect ( ( ) => {
1516 dispatch ( fetchEvents ( ) ) ;
1617 } , [ dispatch ] ) ;
1718
1819 useEffect ( ( ) => {
19- // If authenticated, go to dashboard, otherwise to login
20- if ( isAuthenticated ) {
21- router . push ( "/dashboard" ) ;
22- } else {
23- router . push ( "/Login" ) ;
24- }
20+ // Add a delay before redirecting
21+ const redirectTimer = setTimeout ( ( ) => {
22+ // If authenticated, go to dashboard, otherwise to login
23+ if ( isAuthenticated ) {
24+ router . push ( "/dashboard" ) ;
25+ } else {
26+ router . push ( "/Login" ) ;
27+ }
28+ setIsLoading ( false ) ;
29+ } , 3000 ) ; // 3 seconds delay
30+
31+ // Cleanup function to clear the timeout if component unmounts
32+ return ( ) => clearTimeout ( redirectTimer ) ;
2533 } , [ isAuthenticated , router ] ) ;
2634
2735 // Show loading state while redirecting
@@ -47,7 +55,7 @@ export default function Home() {
4755 < h1 className = "text-3xl font-bold text-gray-800 mb-2" >
4856 Event Management System
4957 </ h1 >
50- < p className = "text-gray-600 mb-6" > Redirecting ...</ p >
58+ < p className = "text-gray-600 mb-6" > Loading ...</ p >
5159 < div className = "flex justify-center" >
5260 < div className = "animate-spin rounded-full h-6 w-6 border-t-2 border-b-2 border-indigo-500" > </ div >
5361 </ div >
0 commit comments