File tree Expand file tree Collapse file tree 4 files changed +43
-8
lines changed Expand file tree Collapse file tree 4 files changed +43
-8
lines changed Original file line number Diff line number Diff line change 888
888
"publish" : false ,
889
889
"createdAt" : 1657240162434 ,
890
890
"id" : 127
891
- },
892
- {
893
- "title" : " f" ,
894
- "body" : " f" ,
895
- "publish" : false ,
896
- "createdAt" : 1657240212897 ,
897
- "id" : 130
898
891
}
899
892
]
900
893
}
Original file line number Diff line number Diff line change
1
+ import { useDispatch , useSelector } from 'react-redux' ;
1
2
import { Link , NavLink } from 'react-router-dom' ;
3
+ import { login , logout } from '../store/authSlice' ;
2
4
3
5
const NavBar = ( ) => {
6
+ const isLoggedIn = useSelector ( state => state . auth . isLoggedIn ) ;
7
+ const dispatch = useDispatch ( ) ;
4
8
return (
5
9
< nav className = "navbar navbar-dark bg-dark" >
6
10
< div className = "container" >
@@ -11,6 +15,20 @@ const NavBar = () => {
11
15
} }
12
16
className = "navbar-nav"
13
17
>
18
+ < li className = "nav-item me-2" >
19
+ < button
20
+ className = "text-white btn btn-link text-decoration-none"
21
+ onClick = { ( ) => {
22
+ if ( isLoggedIn ) {
23
+ dispatch ( logout ( ) ) ;
24
+ } else {
25
+ dispatch ( login ( ) ) ;
26
+ }
27
+ } }
28
+ >
29
+ { isLoggedIn ? 'Logout' : 'Login' }
30
+ </ button >
31
+ </ li >
14
32
< li className = "nav-item me-2" >
15
33
< NavLink
16
34
activeClassName = "active"
Original file line number Diff line number Diff line change
1
+ import { createSlice } from '@reduxjs/toolkit' ;
2
+
3
+ const initialState = {
4
+ isLoggedIn : false
5
+ } ;
6
+
7
+ const authSlice = createSlice ( {
8
+ name : 'auth' ,
9
+ initialState,
10
+ reducers : {
11
+ login : ( state ) => {
12
+ state . isLoggedIn = true ;
13
+ } ,
14
+ logout : ( state ) => {
15
+ state . isLoggedIn = false ;
16
+ }
17
+ }
18
+ } )
19
+
20
+ export const { login, logout } = authSlice . actions ;
21
+
22
+ export default authSlice . reducer ;
Original file line number Diff line number Diff line change 1
1
import { configureStore } from '@reduxjs/toolkit'
2
2
import toastReducer from './toastSlice'
3
+ import authReducer from './authSlice'
3
4
4
5
export const store = configureStore ( {
5
6
reducer : {
6
- toast : toastReducer
7
+ toast : toastReducer ,
8
+ auth : authReducer
7
9
} ,
8
10
} )
You can’t perform that action at this time.
0 commit comments