-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.jsx
501 lines (474 loc) · 21.4 KB
/
Navigation.jsx
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import { Fragment, useEffect, useState } from "react";
import { Dialog, Popover, Tab, Transition } from "@headlessui/react";
import {
Bars3Icon,
MagnifyingGlassIcon,
ShoppingBagIcon,
XMarkIcon,
} from "@heroicons/react/24/outline";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { Avatar, Button, Menu, MenuItem } from "@mui/material";
import { navigation } from "../../../config/navigationMenu";
import AuthModal from "../Auth/AuthModal";
import { useDispatch, useSelector } from "react-redux";
import { deepPurple } from "@mui/material/colors";
import { getUser, logout } from "../../../Redux/Auth/Action";
import { getCart } from "../../../Redux/Customers/Cart/Action";
import TextField from "@mui/material/TextField";
function classNames(...classes) {
return classes.filter(Boolean).join(" ");
}
export default function Navigation() {
const [open, setOpen] = useState(false);
const navigate = useNavigate();
const dispatch = useDispatch();
const { auth, cart } = useSelector((store) => store);
const [openAuthModal, setOpenAuthModal] = useState(false);
const [anchorEl, setAnchorEl] = useState(null);
const openUserMenu = Boolean(anchorEl);
const jwt = localStorage.getItem("jwt");
const location = useLocation();
useEffect(() => {
if (jwt) {
dispatch(getUser(jwt));
dispatch(getCart(jwt));
}
}, [jwt]);
const handleUserClick = (event) => {
setAnchorEl(event.currentTarget);
};
const handleCloseUserMenu = (event) => {
setAnchorEl(null);
};
const handleOpen = () => {
setOpenAuthModal(true);
};
const handleClose = () => {
setOpenAuthModal(false);
};
const handleCategoryClick = (category, section, item, close) => {
navigate(`/${category.id}/${section.id}/${item.id}`);
close();
};
useEffect(() => {
if (auth.user) {
handleClose();
}
if (location.pathname === "/login" || location.pathname === "/register") {
navigate(-1);
}
}, [auth.user]);
const handleLogout = () => {
handleCloseUserMenu();
dispatch(logout());
};
const handleMyOrderClick = () => {
handleCloseUserMenu();
auth.user?.role === "ROLE_ADMIN"
? navigate("/admin")
: navigate("/account/order");
};
return (
<div className="bg-white pb-10">
{/* Mobile menu */}
<Transition.Root show={open} as={Fragment}>
<Dialog as="div" className="relative z-40 lg:hidden" onClose={setOpen}>
<Transition.Child
as={Fragment}
enter="transition-opacity ease-linear duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition-opacity ease-linear duration-300"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-black bg-opacity-25" />
</Transition.Child>
<div className="fixed inset-0 z-40 flex">
<Transition.Child
as={Fragment}
enter="transition ease-in-out duration-300 transform"
enterFrom="-translate-x-full"
enterTo="translate-x-0"
leave="transition ease-in-out duration-300 transform"
leaveFrom="translate-x-0"
leaveTo="-translate-x-full"
>
<Dialog.Panel className="relative flex w-full max-w-xs flex-col overflow-y-auto bg-white pb-12 shadow-xl">
<div className="flex px-4 pb-2 pt-5">
<button
type="button"
className="-m-2 inline-flex items-center justify-center rounded-md p-2 text-gray-400"
onClick={() => setOpen(false)}
>
<span className="sr-only">Close menu</span>
<XMarkIcon className="h-6 w-6" aria-hidden="true" />
</button>
</div>
{/* Links */}
<Tab.Group as="div" className="mt-2">
<div className="border-b border-gray-200">
<Tab.List className="-mb-px flex space-x-8 px-4">
{navigation.categories.map((category) => (
<Tab
key={category.name}
className={({ selected }) =>
classNames(
selected
? "border-indigo-600 text-indigo-600"
: "border-transparent text-gray-900",
"flex-1 whitespace-nowrap border-b-2 px-1 py-4 text-base font-medium border-none"
)
}
>
{category.name}
</Tab>
))}
</Tab.List>
</div>
<Tab.Panels as={Fragment}>
{navigation.categories.map((category) => (
<Tab.Panel
key={category.name}
className="space-y-10 px-4 pb-8 pt-10"
>
<div className="grid grid-cols-2 gap-x-4">
{category.featured.map((item) => (
<div
key={item.name}
className="group relative text-sm"
>
<div className="aspect-h-1 aspect-w-1 overflow-hidden rounded-lg bg-gray-100 group-hover:opacity-75">
<img
src={item.imageSrc}
alt={item.imageAlt}
className="object-cover object-center"
/>
</div>
<a
href={item.href}
className="mt-6 block font-medium text-gray-900"
>
<span
className="absolute inset-0 z-10"
aria-hidden="true"
/>
{item.name}
</a>
<p aria-hidden="true" className="mt-1">
Shop now
</p>
</div>
))}
</div>
{category.sections.map((section) => (
<div key={section.name}>
<p
id={`${category.id}-${section.id}-heading-mobile`}
className="font-medium text-gray-900"
>
{section.name}
</p>
{/* eslint-disable-next-line jsx-a11y/no-redundant-roles */}
<ul
role="list"
aria-labelledby={`${category.id}-${section.id}-heading-mobile`}
className="mt-6 flex flex-col space-y-6"
>
{section.items.map((item) => (
<li key={item.name} className="flow-root">
<p className="-m-2 block p-2 text-gray-500">
{"item.name"}
</p>
</li>
))}
</ul>
</div>
))}
</Tab.Panel>
))}
</Tab.Panels>
</Tab.Group>
<div className="space-y-6 border-t border-gray-200 px-4 py-6">
{navigation.pages.map((page) => (
<div key={page.name} className="flow-root">
<a
href={page.href}
className="-m-2 block p-2 font-medium text-gray-900"
>
{page.name}
</a>
</div>
))}
</div>
<div className="space-y-6 border-t border-gray-200 px-4 py-6">
<div className="flow-root">
<a
href="/"
className="-m-2 block p-2 font-medium text-gray-900"
>
Sign in
</a>
</div>
</div>
<div className="border-t border-gray-200 px-4 py-6">
<a href="/" className="-m-2 flex items-center p-2">
<img
src="https://tailwindui.com/img/flags/flag-canada.svg"
alt=""
className="block h-auto w-5 flex-shrink-0"
/>
<span className="ml-3 block text-base font-medium text-gray-900">
CAD
</span>
<span className="sr-only">, change currency</span>
</a>
</div>
</Dialog.Panel>
</Transition.Child>
</div>
</Dialog>
</Transition.Root>
<header className="relative bg-white">
<p className="flex h-10 items-center justify-center bg-indigo-600 px-4 text-sm font-medium text-white sm:px-6 lg:px-8">
Get free delivery on orders over $100
</p>
<nav aria-label="Top" className="mx-auto">
<div className="border-b border-gray-200">
<div className="flex h-16 items-center px-11">
<button
type="button"
className="rounded-md bg-white p-2 text-gray-400 lg:hidden"
onClick={() => setOpen(true)}
>
<span className="sr-only">Open menu</span>
<Bars3Icon className="h-6 w-6" aria-hidden="true" />
</button>
{/* Logo */}
<div className="ml-4 flex lg:ml-0">
<Link to="/">
<span className="sr-only">Your Company</span>
<img
src="https://res.cloudinary.com/ddkso1wxi/image/upload/v1675919455/Logo/Copy_of_Zosh_Academy_nblljp.png"
alt="Shopwithzosh"
className="h-8 w-8 mr-2"
/>
</Link>
</div>
{/* Flyout menus */}
<Popover.Group className="hidden lg:ml-8 lg:block lg:self-stretch z-10">
<div className="flex h-full space-x-8">
{navigation.categories.map((category) => (
<Popover key={category.name} className="flex">
{({ open, close }) => (
<>
<div className="relative flex">
<Popover.Button
className={classNames(
open
? "border-indigo-600 text-indigo-600"
: "border-transparent text-gray-700 hover:text-gray-800",
"relative z-10 -mb-px flex items-center border-b-2 pt-px text-sm font-medium transition-colors duration-200 ease-out"
)}
>
{category.name}
</Popover.Button>
</div>
<Transition
as={Fragment}
enter="transition ease-out duration-200"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition ease-in duration-150"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Popover.Panel className="absolute inset-x-0 top-full text-sm text-gray-500">
{/* Presentational element used to render the bottom shadow, if we put the shadow on the actual panel it pokes out the top, so we use this shorter element to hide the top of the shadow */}
<div
className="absolute inset-0 top-1/2 bg-white shadow"
aria-hidden="true"
/>
<div className="relative bg-white">
<div className="mx-auto max-w-7xl px-8">
<div className="grid grid-cols-2 gap-x-8 gap-y-10 py-16">
<div className="col-start-2 grid grid-cols-2 gap-x-8">
{category.featured.map((item) => (
<div
key={item.name}
className="group relative text-base sm:text-sm"
>
<div className="aspect-h-1 aspect-w-1 overflow-hidden rounded-lg bg-gray-100 group-hover:opacity-75">
<img
src={item.imageSrc}
alt={item.imageAlt}
className="object-cover object-center"
/>
</div>
<a
href={item.href}
className="mt-6 block font-medium text-gray-900"
>
<span
className="absolute inset-0 z-10"
aria-hidden="true"
/>
{item.name}
</a>
<p
aria-hidden="true"
className="mt-1"
>
Shop now
</p>
</div>
))}
</div>
<div className="row-start-1 grid grid-cols-3 gap-x-8 gap-y-10 text-sm">
{category.sections.map((section) => (
<div key={section.name}>
<p
id={`${section.name}-heading`}
className="font-medium text-gray-900"
>
{section.name}
</p>
{/* eslint-disable-next-line jsx-a11y/no-redundant-roles */}
<ul
role="list"
aria-labelledby={`${section.name}-heading`}
className="mt-6 space-y-6 sm:mt-4 sm:space-y-4"
>
{section.items.map((item) => (
<li
key={item.name}
className="flex"
>
<p
onClick={() =>
handleCategoryClick(
category,
section,
item,
close
)
}
className="cursor-pointer hover:text-gray-800"
>
{item.name}
</p>
</li>
))}
</ul>
</div>
))}
</div>
</div>
</div>
</div>
</Popover.Panel>
</Transition>
</>
)}
</Popover>
))}
{navigation.pages.map((page) => (
<a
key={page.name}
href={page.href}
className="flex items-center text-sm font-medium text-gray-700 hover:text-gray-800"
>
{page.name}
</a>
))}
</div>
</Popover.Group>
<div className="ml-auto flex items-center">
<div className="hidden lg:flex lg:flex-1 lg:items-center lg:justify-end lg:space-x-6">
{auth.user ? (
<div>
<Avatar
className="text-white"
onClick={handleUserClick}
aria-controls={open ? "basic-menu" : undefined}
aria-haspopup="true"
aria-expanded={open ? "true" : undefined}
// onClick={handleUserClick}
sx={{
bgcolor: deepPurple[500],
color: "white",
cursor: "pointer",
}}
>
{auth.user?.firstName[0].toUpperCase()}
</Avatar>
{/* <Button
id="basic-button"
aria-controls={open ? "basic-menu" : undefined}
aria-haspopup="true"
aria-expanded={open ? "true" : undefined}
onClick={handleUserClick}
>
Dashboard
</Button> */}
<Menu
id="basic-menu"
anchorEl={anchorEl}
open={openUserMenu}
onClose={handleCloseUserMenu}
MenuListProps={{
"aria-labelledby": "basic-button",
}}
>
<MenuItem onClick={handleMyOrderClick}>
{auth.user?.role === "ROLE_ADMIN"
? "Admin Dashboard"
: "My Orders"}
</MenuItem>
<MenuItem onClick={handleLogout}>Logout</MenuItem>
</Menu>
</div>
) : (
<Button
onClick={handleOpen}
className="text-sm font-medium text-gray-700 hover:text-gray-800"
>
Signin
</Button>
)}
</div>
{/* Search */}
<div className="flex items-center lg:ml-6">
<p onClick={()=>navigate("/products/search")} className="p-2 text-gray-400 hover:text-gray-500">
<span className="sr-only">Search</span>
<MagnifyingGlassIcon
className="h-6 w-6"
aria-hidden="true"
/>
</p>
</div>
{/* Cart */}
<div className="ml-4 flow-root lg:ml-6">
<Button
onClick={() => navigate("/cart")}
className="group -m-2 flex items-center p-2"
>
<ShoppingBagIcon
className="h-6 w-6 flex-shrink-0 text-gray-400 group-hover:text-gray-500"
aria-hidden="true"
/>
<span className="ml-2 text-sm font-medium text-gray-700 group-hover:text-gray-800">
{cart.cart?.totalItem}
</span>
<span className="sr-only">items in cart, view bag</span>
</Button>
</div>
</div>
</div>
</div>
</nav>
</header>
<AuthModal handleClose={handleClose} open={openAuthModal} />
</div>
);
}