File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -7,12 +7,28 @@ import NavBar from './components/NavBar';
7
7
import routes from './routes' ;
8
8
import Toast from './components/Toast' ;
9
9
import useToast from './hooks/toast' ;
10
- import { useSelector } from 'react-redux' ;
10
+ import { useDispatch , useSelector } from 'react-redux' ;
11
11
import ProtectedRoute from './ProtectedRoute' ;
12
+ import { useEffect , useState } from 'react' ;
13
+ import { login } from './store/authSlice' ;
14
+ import LoadingSpinner from './components/LoadingSpinner' ;
12
15
13
16
function App ( ) {
14
17
const toasts = useSelector ( state => state . toast . toasts ) ;
15
18
const { deleteToast } = useToast ( ) ;
19
+ const [ loading , setLoading ] = useState ( true ) ;
20
+ const dispatch = useDispatch ( ) ;
21
+ useEffect ( ( ) => {
22
+ if ( localStorage . getItem ( 'isLoggedIn' ) ) {
23
+ dispatch ( login ( ) ) ;
24
+ }
25
+ setLoading ( false ) ;
26
+ } , [ ] ) ;
27
+
28
+ if ( loading ) {
29
+ return < LoadingSpinner />
30
+ }
31
+
16
32
return (
17
33
< Router >
18
34
< NavBar />
Original file line number Diff line number Diff line change @@ -9,9 +9,11 @@ const authSlice = createSlice({
9
9
initialState,
10
10
reducers : {
11
11
login : ( state ) => {
12
+ localStorage . setItem ( 'isLoggedIn' , 'yes' ) ;
12
13
state . isLoggedIn = true ;
13
14
} ,
14
15
logout : ( state ) => {
16
+ localStorage . removeItem ( 'isLoggedIn' ) ;
15
17
state . isLoggedIn = false ;
16
18
}
17
19
}
You can’t perform that action at this time.
0 commit comments