11import Link from 'next/link' ;
2- import React , { useEffect , useState } from 'react' ;
2+ import React from 'react' ;
33
4+ import { connect } from 'react-redux' ;
5+ import { removeAuthenticatedUser } from '../../redux/actions/index' ;
6+ import { ReducerType } from '../../redux/reducers/rootReducer' ;
47import { removedUserFromLocalStorage } from '../../utils/functions/helpers' ;
58
6- const Navbar : React . FunctionComponent = ( ) => {
7- const [ user , setUser ] = useState < any > ( false ) ;
8-
9- useEffect ( ( ) => {
10- if ( typeof window !== 'undefined' ) {
11- setUser ( ( ) => localStorage . getItem ( 'user' ) ) ;
12- }
13- } , [ user ] ) ;
9+ const Navbar : React . FunctionComponent = ( props : any ) => {
10+ const { isAuthenticated, isADmin } = props . auth ;
1411
1512 const signedOutHandler = ( ) => {
13+ props . removeAuthenticatedUser ( ) ;
1614 removedUserFromLocalStorage ( ) ;
17- setUser ( false ) ;
1815 } ;
1916
2017 return (
@@ -30,31 +27,36 @@ const Navbar: React.FunctionComponent = () => {
3027 < Link href = "/cart" >
3128 < a className = "customlink" > Cart</ a >
3229 </ Link >
33- < Link href = "/add-product" >
34- < a className = "customlink" > Add Product</ a >
35- </ Link >
30+ { isADmin === 'admin' && (
31+ < Link href = "/add-product" >
32+ < a className = "customlink" > Add Product</ a >
33+ </ Link >
34+ ) }
3635 < Link href = "/order" >
3736 < a className = "customlink" > Oder</ a >
3837 </ Link >
39- < Link href = "/admin/products" >
40- < a className = "customlink" > Admin Products</ a >
41- </ Link >
42- < Link href = "/admin/users-ui" >
43- < a className = "customlink" > Admin Users UI</ a >
44- </ Link >
45- < Link href = "/admin/users-table" >
46- < a className = "customlink" > Admin Users Table</ a >
47- </ Link >
38+ { isADmin === 'admin' && (
39+ < >
40+ < Link href = "/admin/products" >
41+ < a className = "customlink" > Admin Products</ a >
42+ </ Link >
43+ < Link href = "/admin/users-ui" >
44+ < a className = "customlink" > Admin Users UI</ a >
45+ </ Link >
46+ < Link href = "/admin/users-table" >
47+ < a className = "customlink" > Admin Users Table</ a >
48+ </ Link >
49+ </ >
50+ ) }
4851 </ div >
49- { user && (
52+ { isAuthenticated && (
5053 < div >
5154 < button className = " customlink" onClick = { signedOutHandler } >
5255 SignOut
5356 </ button >
5457 </ div >
5558 ) }
56-
57- { ! user && (
59+ { ! isAuthenticated && (
5860 < div className = " flex justify-between items-center space-x-8" >
5961 < Link href = "/login" >
6062 < a className = "customlink cursor-pointer" > Login</ a >
@@ -70,4 +72,17 @@ const Navbar: React.FunctionComponent = () => {
7072 ) ;
7173} ;
7274
73- export default Navbar ;
75+ const mapStateToProps = ( state : ReducerType ) => {
76+ return {
77+ auth : state . auth
78+ } ;
79+ } ;
80+
81+ const mapDispatchToProps = {
82+ removeAuthenticatedUser
83+ } ;
84+
85+ export default connect (
86+ mapStateToProps ,
87+ mapDispatchToProps
88+ ) ( Navbar ) ;
0 commit comments