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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
import { useState } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { Link as RouterLink, useHistory } from 'react-router-dom'
import {
AppBar,
Avatar,
Button,
Fade,
IconButton,
Link,
ListItemText,
Menu,
MenuItem,
Toolbar,
Typography
} from '@material-ui/core'
import { deepPurple } from '@material-ui/core/colors'
import { makeStyles } from '@material-ui/core/styles'
import { logout } from '../../redux/authSlice'
import logo from '../../static/favicon.ico'
import { getUppercaseInitial } from '../../utils/GalleryUtils'
const useStyles = makeStyles((theme) => ({
appBar: {
borderBottom: `1px solid ${theme.palette.divider}`
},
toolbar: {
flexWrap: 'wrap'
},
toolbarTitle: {
flexGrow: 1
},
link: {
margin: theme.spacing(1, 1.5)
},
button: {
marginRight: theme.spacing(0.7)
},
small: {
width: theme.spacing(3.7),
height: theme.spacing(3.7),
borderRadius: '10%'
},
purple: {
width: theme.spacing(3.75),
height: theme.spacing(3.75),
color: theme.palette.getContrastText(deepPurple[500]),
backgroundColor: deepPurple[500],
fontSize: '17px'
}
}))
// Common navbar for Dashboard, Home, Gallery, etc.
export function Header () {
const history = useHistory()
const classes = useStyles()
const [anchorEl, setAnchorEl] = useState(null)
const isAuthenticated = useSelector(state => state.auth.isAuthenticated)
const user = useSelector(state => state.auth.user)
const dispatch = useDispatch()
const handleClick = (event) => {
setAnchorEl(event.currentTarget)
}
const handleClose = () => {
setAnchorEl(null)
}
const link = process.env.REACT_APP_NAME
const altImage = process.env.REACT_APP_NAME + ' logo'
const typography = 'My ' + process.env.REACT_APP_DIAGRAMS_NAME
return (
<>
{/* Display logo */}
<IconButton edge='start' className={classes.button} color='primary'>
<Avatar alt={altImage} src={logo} className={classes.small} />
</IconButton>
<Typography
variant='h6'
color='inherit'
noWrap
className={classes.toolbarTitle}
>
<Link color='inherit' to='/' component={RouterLink}>
{link}
</Link>
</Typography>
{/* Display relative link to other pages */}
<nav>
{
(isAuthenticated
? (<>
<Link
variant='button'
color='textPrimary'
to='/'
component={RouterLink}
className={classes.link}
>
Home
</Link>
<Link
variant='button'
color='textPrimary'
to='/editor'
component={RouterLink}
className={classes.link}
>
Editor
</Link>
<Link
variant='button'
color='textPrimary'
to='/gallery'
component={RouterLink}
className={classes.link}
>
Gallery
</Link>
<Link
variant='button'
color='textPrimary'
to='/dashboard'
component={RouterLink}
className={classes.link}
>
Dashboard
</Link>
</>)
: (<>
<Link
variant='button'
color='textPrimary'
to='/editor'
component={RouterLink}
style={{ marginRight: '20px' }}
>
Editor
</Link>
<Link
variant='button'
color='textPrimary'
to='/gallery'
component={RouterLink}
style={{ marginRight: '20px' }}
>
Gallery
</Link>
</>
)
)
}
</nav>
{/* Display login option or user menu as per authenticated status */}
{
(!isAuthenticated
? <Button
size='small'
component={RouterLink}
to='/login'
color='primary'
variant='outlined'
>
Login
</Button>
: <>
<IconButton
edge='start'
style={{ marginLeft: 'auto' }}
color='primary'
aria-controls='simple-menu'
aria-haspopup='true'
onClick={handleClick}
>
<Avatar className={classes.purple}>
{getUppercaseInitial(user.username)}
</Avatar>
</IconButton>
<Menu
id='simple-menu'
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
TransitionComponent={Fade}
style={{ marginTop: '25px' }}
>
<MenuItem
component={RouterLink}
to='/dashboard'
onClick={handleClose}
>
<ListItemText primary={user.username} secondary={user.email} />
</MenuItem>
<MenuItem
component={RouterLink}
to='/dashboard/schematics'
onClick={handleClose}
>
{typography}
</MenuItem>
<MenuItem onClick={() => {
dispatch(logout(history))
}}
>
Logout
</MenuItem>
</Menu>
</>
)
}
</>
)
}
export default function Navbar () {
const classes = useStyles()
return (
<AppBar
position='static'
color='default'
elevation={0}
className={classes.appBar}
>
<Toolbar variant='dense' color='default' className={classes.toolbar}>
<Header />
</Toolbar>
</AppBar>
)
}
|