forked from basir/next-amazona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Layout.js
166 lines (165 loc) · 4.8 KB
/
Layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React, { useContext } from 'react';
import Head from 'next/head';
import NextLink from 'next/link';
import {
AppBar,
Toolbar,
Typography,
Container,
Link,
createMuiTheme,
ThemeProvider,
CssBaseline,
Switch,
Badge,
Button,
Menu,
MenuItem,
} from '@material-ui/core';
import useStyles from '../utils/styles';
import { Store } from '../utils/Store';
import Cookies from 'js-cookie';
import { useState } from 'react';
import { useRouter } from 'next/router';
export default function Layout({ title, description, children }) {
const router = useRouter();
const { state, dispatch } = useContext(Store);
const { darkMode, cart, userInfo } = state;
const theme = createMuiTheme({
typography: {
h1: {
fontSize: '1.6rem',
fontWeight: 400,
margin: '1rem 0',
},
h2: {
fontSize: '1.4rem',
fontWeight: 400,
margin: '1rem 0',
},
},
palette: {
type: darkMode ? 'dark' : 'light',
primary: {
main: '#f0c000',
},
secondary: {
main: '#208080',
},
},
});
const classes = useStyles();
const darkModeChangeHandler = () => {
dispatch({ type: darkMode ? 'DARK_MODE_OFF' : 'DARK_MODE_ON' });
const newDarkMode = !darkMode;
Cookies.set('darkMode', newDarkMode ? 'ON' : 'OFF');
};
const [anchorEl, setAnchorEl] = useState(null);
const loginClickHandler = (e) => {
setAnchorEl(e.currentTarget);
};
const loginMenuCloseHandler = (e, redirect) => {
setAnchorEl(null);
if (redirect) {
router.push(redirect);
}
};
const logoutClickHandler = () => {
setAnchorEl(null);
dispatch({ type: 'USER_LOGOUT' });
Cookies.remove('userInfo');
Cookies.remove('cartItems');
router.push('/');
};
return (
<div>
<Head>
<title>{title ? `${title} - Next Amazona` : 'Next Amazona'}</title>
{description && <meta name="description" content={description}></meta>}
</Head>
<ThemeProvider theme={theme}>
<CssBaseline />
<AppBar position="static" className={classes.navbar}>
<Toolbar>
<NextLink href="/" passHref>
<Link>
<Typography className={classes.brand}>amazona</Typography>
</Link>
</NextLink>
<div className={classes.grow}></div>
<div>
<Switch
checked={darkMode}
onChange={darkModeChangeHandler}
></Switch>
<NextLink href="/cart" passHref>
<Link>
{cart.cartItems.length > 0 ? (
<Badge
color="secondary"
badgeContent={cart.cartItems.length}
>
Cart
</Badge>
) : (
'Cart'
)}
</Link>
</NextLink>
{userInfo ? (
<>
<Button
aria-controls="simple-menu"
aria-haspopup="true"
onClick={loginClickHandler}
className={classes.navbarButton}
>
{userInfo.name}
</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={loginMenuCloseHandler}
>
<MenuItem
onClick={(e) => loginMenuCloseHandler(e, '/profile')}
>
Profile
</MenuItem>
<MenuItem
onClick={(e) =>
loginMenuCloseHandler(e, '/order-history')
}
>
Order Hisotry
</MenuItem>
{userInfo.isAdmin && (
<MenuItem
onClick={(e) =>
loginMenuCloseHandler(e, '/admin/dashboard')
}
>
Admin Dashboard
</MenuItem>
)}
<MenuItem onClick={logoutClickHandler}>Logout</MenuItem>
</Menu>
</>
) : (
<NextLink href="/login" passHref>
<Link>Login</Link>
</NextLink>
)}
</div>
</Toolbar>
</AppBar>
<Container className={classes.main}>{children}</Container>
<footer className={classes.footer}>
<Typography>All rights reserved. Next Amazona.</Typography>
</footer>
</ThemeProvider>
</div>
);
}