@@ -8,7 +8,7 @@ import {Menu, Grid} from '@material-ui/core';
8
8
import MenuIcon from '@material-ui/icons/Menu' ;
9
9
import MoreIcon from '@material-ui/icons/MoreVert' ;
10
10
import Cookies from 'js-cookie' ;
11
- import { getDataViaAPI , setTokenFromCookie , signOut } from '../../../actions' ;
11
+ import { getDataViaAPI , setAuthDetailsFromCookie , signOut } from '../../../actions' ;
12
12
import { connect , useDispatch } from 'react-redux'
13
13
14
14
import {
@@ -29,18 +29,21 @@ import {HTTPError} from "../../ui/error/httpError";
29
29
import { BadRequest } from "../../ui/error/badRequest" ;
30
30
import SearchBar from "./searchBar" ;
31
31
import SideBar from "./sideBar" ;
32
- import { SHOPPERS_PRODUCT_INFO_COOKIE , TOKEN_ID_COOKIE } from "../../../constants/cookies" ;
32
+ import { SHOPPERS_PRODUCT_INFO_COOKIE , AUTH_DETAILS_COOKIE } from "../../../constants/cookies" ;
33
33
import { TABS_DATA_API } from "../../../constants/api_routes" ;
34
34
import { TABS_API_OBJECT_LEN } from "../../../constants/constants"
35
+ import Avatar from '@material-ui/core/Avatar' ;
36
+ import history from "../../../history" ;
35
37
36
38
const NavBar = props => {
37
39
const classes = useNavBarStyles ( ) ;
38
40
const [ anchorEl , setAnchorEl ] = React . useState ( null ) ;
39
41
const [ mobileSearchState , setMobileSearchState ] = React . useState ( false ) ;
40
42
const [ mobileMoreAnchorEl , setMobileMoreAnchorEl ] = React . useState ( null ) ;
41
43
const [ hamburgerBtnState , setHamburgerBtnState ] = React . useState ( false ) ;
42
- const { isSignedIn, tokenId} = useSelector ( state => state . signInReducer )
44
+ const { isSignedIn, tokenId, firstName } = useSelector ( state => state . signInReducer )
43
45
const tabsAPIData = useSelector ( state => state . tabsDataReducer )
46
+ const addToCart = useSelector ( state => state . addToCartReducer )
44
47
45
48
const isMenuOpen = Boolean ( anchorEl ) ;
46
49
const isMobileMenuOpen = Boolean ( mobileMoreAnchorEl ) ;
@@ -79,15 +82,15 @@ const NavBar = props => {
79
82
// account details from the cookie.
80
83
if ( isSignedIn === null ) {
81
84
log . info ( `[NavBar]: isSignedIn is null` )
82
- let tokenIdFromCookie = Cookies . get ( TOKEN_ID_COOKIE )
83
- if ( tokenIdFromCookie ) {
84
- log . info ( `[NavBar]: Token set from Cookie` )
85
- props . setTokenFromCookie ( tokenIdFromCookie )
85
+ let savedAuthDetails = Cookies . get ( AUTH_DETAILS_COOKIE )
86
+ if ( savedAuthDetails ) {
87
+ log . info ( `[NavBar]: setting Auth Details from Cookie` )
88
+ props . setAuthDetailsFromCookie ( JSON . parse ( savedAuthDetails ) )
86
89
}
87
90
}
88
91
89
92
// tabs data is not loaded then load it.
90
- if ( ! tabsAPIData . hasOwnProperty ( "data" ) ) {
93
+ if ( ! tabsAPIData . hasOwnProperty ( "data" ) ) {
91
94
props . getDataViaAPI ( LOAD_TABS_DATA , TABS_DATA_API )
92
95
}
93
96
@@ -155,13 +158,35 @@ const NavBar = props => {
155
158
open = { isMenuOpen }
156
159
onClose = { handleMenuClose }
157
160
>
158
- < Link to = { ! tokenId ? "/signin " : "/" } >
159
- < MenuItem onClick = { handleLoginStatus } > { ! tokenId ? 'SignIn ' : 'SignOut ' } </ MenuItem >
161
+ < Link to = { isSignedIn ? "/" : "/signin " } >
162
+ < MenuItem onClick = { handleLoginStatus } > { isSignedIn ? 'Sign Out ' : 'Sign In ' } </ MenuItem >
160
163
</ Link >
161
164
< MenuItem onClick = { handleMenuClose } > My account</ MenuItem >
162
165
</ Menu >
163
166
) ;
164
167
168
+ const menuProfileBtnHandler = ( ) => {
169
+ if ( isSignedIn ) {
170
+ history . push ( "/" )
171
+ } else {
172
+ history . push ( "/signin" )
173
+ }
174
+ setMobileMoreAnchorEl ( null ) ;
175
+ }
176
+
177
+ const menuBagBtnHandler = ( ) => {
178
+ history . push ( "/shopping-bag" )
179
+ setMobileMoreAnchorEl ( null ) ;
180
+ }
181
+
182
+ const renderAvatar = ( ) => {
183
+ return (
184
+ < Avatar className = { classes . orange } sizes = "small"
185
+ style = { { width : 20 , height : 20 , marginBottom : 3 } } > { firstName ? firstName . charAt ( 0 ) :
186
+ "S" } </ Avatar >
187
+ )
188
+ }
189
+
165
190
const mobileMenuId = 'primary-search-account-menu-mobile' ;
166
191
const renderMobileMenu = (
167
192
< Menu
@@ -173,35 +198,43 @@ const NavBar = props => {
173
198
open = { isMobileMenuOpen }
174
199
onClose = { handleMobileMenuClose }
175
200
>
176
- < MenuItem onClick = { handleProfileMenuOpen } >
177
- < IconButton
178
- aria-label = "account of current user"
179
- aria-controls = "primary-search-account-menu"
180
- aria-haspopup = "true"
181
- color = "inherit"
182
- >
183
- < AccountCircle />
184
- </ IconButton >
185
- < p > Login</ p >
201
+ < MenuItem onClick = { menuProfileBtnHandler } style = { { padding : "0 0.7rem 0 0" } } >
202
+ < Grid container alignItems = "center" >
203
+ < Grid item >
204
+ < IconButton aria-label = "account of current user"
205
+ aria-controls = "primary-search-account-menu"
206
+ aria-haspopup = "true"
207
+ color = "inherit" >
208
+ { isSignedIn ? renderAvatar ( ) : < AccountCircle /> }
209
+ </ IconButton >
210
+ </ Grid >
211
+ < Grid item >
212
+ < p > { isSignedIn ? 'Sign Out' : 'Sign In' } </ p >
213
+ </ Grid >
214
+ </ Grid >
186
215
</ MenuItem >
187
- < MenuItem >
188
- < IconButton aria-label = "show 11 new notifications" color = "inherit" >
189
- < Badge badgeContent = { 11 } color = "secondary" >
190
- < LocalMallIcon />
191
- </ Badge >
192
- </ IconButton >
193
- < p > Bag</ p >
216
+ < MenuItem onClick = { menuBagBtnHandler } style = { { padding : "0 0.7rem 0 0" } } >
217
+ < Grid container alignItems = "center" >
218
+ < Grid item xs = { 7 } >
219
+ < IconButton color = "inherit" >
220
+ < Badge badgeContent = { addToCart . totalQuantity } color = "secondary" >
221
+ < LocalMallIcon />
222
+ </ Badge >
223
+ </ IconButton >
224
+ </ Grid >
225
+ < Grid item >
226
+ < p > Bag</ p >
227
+ </ Grid >
228
+ </ Grid >
194
229
</ MenuItem >
195
230
</ Menu >
196
231
) ;
197
232
198
233
const handleMobileSearchOpen = ( ) => {
199
- log . info ( "Mobile Search is clicked...." )
200
234
setMobileSearchState ( true )
201
235
}
202
236
203
237
const handleMobileSearchClose = ( ) => {
204
- log . info ( "Mobile Search is clicked...." )
205
238
setMobileSearchState ( false )
206
239
}
207
240
@@ -280,7 +313,7 @@ const NavBar = props => {
280
313
< Box display = "flex" justifyContent = "center" alignItems = "center" css = { { width : 90 } } >
281
314
< Box width = "50%" onClick = { handleProfileMenuOpen } css = { { cursor : 'pointer' } } >
282
315
< Box pl = { 1 } pt = { 0.3 } >
283
- < AccountCircle />
316
+ { isSignedIn ? renderAvatar ( ) : < AccountCircle /> }
284
317
</ Box >
285
318
< Box style = { { color : "black" , fontSize : "0.8rem" , fontWeight : 'bold' } } >
286
319
Profile
@@ -321,4 +354,4 @@ const NavBar = props => {
321
354
) ;
322
355
} ;
323
356
324
- export default connect ( null , { setTokenFromCookie , signOut, getDataViaAPI} ) ( NavBar ) ;
357
+ export default connect ( null , { setAuthDetailsFromCookie , signOut, getDataViaAPI} ) ( NavBar ) ;
0 commit comments