Skip to content

Commit b8816b6

Browse files
Refactored navbar by seperating mobile menu
1 parent f56cc8e commit b8816b6

File tree

2 files changed

+93
-75
lines changed

2 files changed

+93
-75
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import log from "loglevel";
3+
import {Grid, IconButton, Menu} from "@material-ui/core";
4+
import MenuItem from "@material-ui/core/MenuItem";
5+
import BagButton from "./bagButton";
6+
7+
export default function MobileMenu(props) {
8+
9+
log.info(`[MobileMenu]: Rendering MobileMenu Component`)
10+
return (
11+
<Menu
12+
anchorEl={props.mobileMoreAnchorEl}
13+
anchorOrigin={{vertical: 'top', horizontal: 'right'}}
14+
id={props.mobileMenuId}
15+
keepMounted
16+
transformOrigin={{vertical: 'top', horizontal: 'right'}}
17+
open={props.isMobileMenuOpen}
18+
onClose={props.handleMobileMenuClose}>
19+
<MenuItem onClick={props.authBtnHandler} style={{padding: "0 0.7rem 0 0"}}>
20+
<Grid container alignItems="center">
21+
<Grid item>
22+
<IconButton aria-label="account of current user"
23+
aria-controls="primary-search-account-menu"
24+
aria-haspopup="true"
25+
color="inherit">
26+
{props.authIcon}
27+
</IconButton>
28+
</Grid>
29+
<Grid item>
30+
<p>{props.authLabel}</p>
31+
</Grid>
32+
</Grid>
33+
</MenuItem>
34+
<MenuItem onClick={props.bagBtnHandler} style={{padding: "0 0.7rem 0 0"}}>
35+
<Grid container alignItems="center">
36+
<Grid item xs={7}>
37+
<IconButton color="inherit">
38+
<BagButton/>
39+
</IconButton>
40+
</Grid>
41+
<Grid item>
42+
<p>Bag</p>
43+
</Grid>
44+
</Grid>
45+
</MenuItem>
46+
</Menu>
47+
);
48+
};

client/src/components/routes/navbar/navBar.js

Lines changed: 45 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import React, {useEffect} from "react";
22

33
import SearchIcon from '@material-ui/icons/Search';
44
import AccountCircle from '@material-ui/icons/AccountCircle';
5-
import MenuItem from '@material-ui/core/MenuItem';
6-
import {Menu, Grid} from '@material-ui/core';
5+
import {Grid} from '@material-ui/core';
76
import MenuIcon from '@material-ui/icons/Menu';
87
import MoreIcon from '@material-ui/icons/MoreVert';
98
import Cookies from 'js-cookie';
@@ -35,23 +34,25 @@ import {TABS_DATA_API} from "../../../constants/api_routes";
3534
import {TABS_API_OBJECT_LEN} from "../../../constants/constants"
3635
import Avatar from '@material-ui/core/Avatar';
3736
import history from "../../../history";
37+
import MobileMenu from "./mobileMenu";
3838

3939
const NavBar = props => {
4040
const classes = useNavBarStyles();
4141

4242
const [mobileSearchState, setMobileSearchState] = React.useState(false);
43-
const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null);
4443
const [hamburgerBtnState, setHamburgerBtnState] = React.useState(false);
4544

4645
const {isSignedIn, tokenId, firstName} = useSelector(state => state.signInReducer)
4746
const googleAuthReducer = useSelector(state => state.googleAuthReducer)
4847
const tabsAPIData = useSelector(state => state.tabsDataReducer)
4948

50-
const isMobileMenuOpen = Boolean(mobileMoreAnchorEl);
5149
const dispatch = useDispatch()
5250

5351
let authIcon = null
5452
let authLabel = null
53+
const mobileMenuId = 'primary-search-account-menu-mobile';
54+
const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null);
55+
const isMobileMenuOpen = Boolean(mobileMoreAnchorEl);
5556

5657
/**
5758
* set the cart from saved Cookie
@@ -97,7 +98,7 @@ const NavBar = props => {
9798
oAuth: auth
9899
}
99100
})
100-
}).catch(function (e) {
101+
}).catch(function () {
101102
log.error(`[Navbar] Failed to load google OAuth`)
102103
})
103104
});
@@ -173,14 +174,6 @@ const NavBar = props => {
173174
authLabel = "Sign In"
174175
}
175176

176-
const handleMobileMenuClose = () => {
177-
setMobileMoreAnchorEl(null);
178-
};
179-
180-
const handleMobileMenuOpen = (event) => {
181-
setMobileMoreAnchorEl(event.currentTarget);
182-
};
183-
184177
const changeAuthStatusHandler = () => {
185178
log.info(`[Navbar] handleSignOutClick isSignedIn = ${googleAuthReducer.isSignedInUsingOAuth}`)
186179
if (googleAuthReducer.isSignedInUsingOAuth) {
@@ -190,7 +183,6 @@ const NavBar = props => {
190183
} else {
191184
history.push("/signin")
192185
}
193-
194186
handleMobileMenuClose();
195187
}
196188

@@ -199,66 +191,34 @@ const NavBar = props => {
199191
setMobileMoreAnchorEl(null);
200192
}
201193

202-
const mobileMenuId = 'primary-search-account-menu-mobile';
203-
const renderMobileMenu = (
204-
<Menu
205-
anchorEl={mobileMoreAnchorEl}
206-
anchorOrigin={{vertical: 'top', horizontal: 'right'}}
207-
id={mobileMenuId}
208-
keepMounted
209-
transformOrigin={{vertical: 'top', horizontal: 'right'}}
210-
open={isMobileMenuOpen}
211-
onClose={handleMobileMenuClose}>
212-
<MenuItem onClick={changeAuthStatusHandler} style={{padding: "0 0.7rem 0 0"}}>
213-
<Grid container alignItems="center">
214-
<Grid item>
215-
<IconButton aria-label="account of current user"
216-
aria-controls="primary-search-account-menu"
217-
aria-haspopup="true"
218-
color="inherit">
219-
{authIcon}
220-
</IconButton>
221-
</Grid>
222-
<Grid item>
223-
<p>{authLabel}</p>
224-
</Grid>
225-
</Grid>
226-
</MenuItem>
227-
<MenuItem onClick={changePageToShoppingBagHandler} style={{padding: "0 0.7rem 0 0"}}>
228-
<Grid container alignItems="center">
229-
<Grid item xs={7}>
230-
<IconButton color="inherit">
231-
<BagButton/>
232-
</IconButton>
233-
</Grid>
234-
<Grid item>
235-
<p>Bag</p>
236-
</Grid>
237-
</Grid>
238-
</MenuItem>
239-
</Menu>
240-
);
194+
const handleMobileMenuClose = () => {
195+
setMobileMoreAnchorEl(null);
196+
};
197+
198+
const handleMobileMenuOpen = (event) => {
199+
setMobileMoreAnchorEl(event.currentTarget);
200+
};
241201

242202
const handleMobileSearchClose = () => {
243203
setMobileSearchState(false)
244204
}
245205

206+
const handleMobileSearchOpen = () => {
207+
setMobileSearchState(true)
208+
}
209+
246210
const renderMobileSearchInputField = () => {
247211
if (mobileSearchState) {
248212
return <SearchBar size="medium" handleClose={handleMobileSearchClose}/>
249213
}
250214
}
251215

252-
const handleMobileSearchOpen = () => {
253-
setMobileSearchState(true)
254-
}
255-
256-
const handleHamburgerBtnClick = () => {
216+
const handleSidebarOpen = () => {
257217
log.info(`[NavBar] opening sidebar`)
258218
setHamburgerBtnState(true)
259219
}
260220

261-
const sidebarCloseHandler = () => {
221+
const handleSidebarClose = () => {
262222
log.info(`[NavBar] clickAwayListener is triggered`)
263223
setHamburgerBtnState(false)
264224
}
@@ -282,7 +242,7 @@ const NavBar = props => {
282242
log.info(`[NavBar]: Rendering NavBar Component`)
283243
return (
284244
<>
285-
<SideBar open={hamburgerBtnState} closeHandler={sidebarCloseHandler}/>
245+
<SideBar open={hamburgerBtnState} closeHandler={handleSidebarClose}/>
286246

287247
<div style={{paddingBottom: 80}}>
288248
<AppBar color="default" className={classes.appBarRoot}>
@@ -295,7 +255,7 @@ const NavBar = props => {
295255
className={classes.menuButton}
296256
color="inherit"
297257
aria-label="open drawer"
298-
onClick={handleHamburgerBtnClick}>
258+
onClick={handleSidebarOpen}>
299259
<MenuIcon fontSize="large"/>
300260
</IconButton>
301261
</Grid>
@@ -335,9 +295,9 @@ const NavBar = props => {
335295
{renderMobileSearchInputField()}
336296
</Hidden>
337297

338-
<div className={classes.growHalf}/>
339-
340298
<Hidden xsDown>
299+
<div className={classes.growHalf}/>
300+
341301
{renderIndependentElem(changeAuthStatusHandler, authIcon, authLabel,
342302
2)}
343303

@@ -348,22 +308,32 @@ const NavBar = props => {
348308
</Hidden>
349309

350310

351-
<div className={classes.sectionMobile}>
352-
<IconButton
353-
aria-label="show more"
354-
aria-controls={mobileMenuId}
355-
aria-haspopup="true"
356-
onClick={handleMobileMenuOpen}
357-
color="inherit"
358-
edge="end">
359-
<MoreIcon fontSize="large"/>
360-
</IconButton>
361-
</div>
311+
<Hidden smUp>
312+
<Grid item>
313+
<IconButton
314+
aria-label="show more"
315+
aria-controls={mobileMenuId}
316+
aria-haspopup="true"
317+
onClick={handleMobileMenuOpen}
318+
color="inherit"
319+
edge="end">
320+
<MoreIcon fontSize="large"/>
321+
</IconButton>
322+
</Grid>
323+
</Hidden>
362324
</Grid>
363325
</Toolbar>
364326
</AppBar>
365327

366-
{renderMobileMenu}
328+
<MobileMenu mobileMenuId={mobileMenuId}
329+
authIcon={authIcon}
330+
authLabel={authLabel}
331+
authBtnHandler={changeAuthStatusHandler}
332+
bagBtnHandler={changePageToShoppingBagHandler}
333+
mobileMoreAnchorEl={mobileMoreAnchorEl}
334+
isMobileMenuOpen={isMobileMenuOpen}
335+
handleMobileMenuClose={handleMobileMenuClose}
336+
/>
367337
</div>
368338
</>
369339
);

0 commit comments

Comments
 (0)