forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavbar.jsx
More file actions
265 lines (251 loc) · 9.56 KB
/
Copy pathNavbar.jsx
File metadata and controls
265 lines (251 loc) · 9.56 KB
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
"use client";
import Link from "next/link";
import Image from "next/image";
import { useState, useEffect } from "react";
import { AiOutlineMenu, AiOutlineClose } from "react-icons/ai";
import { useAuth } from "../lib/AuthContext";
import { logout } from "../lib/auth";
import Toast from "../components/Toast";
import { useRouter } from "next/navigation";
import { TfiAngleRight } from "react-icons/tfi";
const navLinkClass =
"text-md font-medium text-mova-ink/80 hover:text-purple-600 transition-colors duration-200";
function BrandMark() {
return (
<Link href="/" className="flex items-center gap-2 group">
<span className="flex h-9 w-9 items-center justify-center rounded-lg bg-gradient-to-br from-purple-600 to-mova-deep text-sm font-bold text-white shadow-mova transition group-hover:scale-105">
M
</span>
<span className="font-display text-xl font-bold tracking-tight text-mova-ink">
Mova <span className="text-purple-600">Store</span>
</span>
</Link>
);
}
function Navbar() {
const router = useRouter();
useEffect(() => {
const handleLinkClick = (event) => {
const href = event.currentTarget.getAttribute("href");
if (!href || !href.startsWith("#")) return;
const targetId = href.substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
event.preventDefault();
targetElement.scrollIntoView({ behavior: "smooth" });
} else {
event.preventDefault();
router.push(`/${href}`);
}
};
const links = document.querySelectorAll('a[href^="#"]');
links.forEach((link) => link.addEventListener("click", handleLinkClick));
return () => {
links.forEach((link) =>
link.removeEventListener("click", handleLinkClick)
);
};
}, [router]);
const [toast, setToast] = useState({ show: false, message: "" });
const showToast = (message) => {
setToast({ show: true, message });
setTimeout(() => setToast({ show: false, message: "" }), 3000);
};
const [showNav, setShowNav] = useState(false);
const { user } = useAuth();
const toggleNav = () => setShowNav(!showNav);
const closeNavOnClick = () => setShowNav(false);
const handleLogout = async () => {
try {
await logout();
localStorage.clear();
router.push("/");
showToast("Logged out successfully");
} catch (error) {
showToast(error.message);
}
};
const handleProtectedLinkClick = (e, path) => {
if (!user) {
e.preventDefault();
closeNavOnClick();
showToast("Kindly login first");
router.push("/profile/login");
} else {
e.preventDefault();
closeNavOnClick();
router.push(path);
}
};
return (
<>
<nav className="fixed z-10 w-full border-b border-purple-100/80 bg-white/85 backdrop-blur-md">
<div className="hidden items-center justify-between px-3 py-2 sm:px-6 md:flex">
<BrandMark />
<div className="hidden md:flex md:gap-6">
<Link href="/" className={navLinkClass}>
Home
</Link>
<Link
href={user ? "/shop" : "/profile/login"}
className={navLinkClass}
onClick={(e) => handleProtectedLinkClick(e, "/shop")}
>
Shop
</Link>
<Link
href={user ? "/collections" : "/profile/login"}
className={navLinkClass}
onClick={(e) => handleProtectedLinkClick(e, "/collections")}
>
Collections
</Link>
<Link href="#aboutus" className={navLinkClass}>
About Us
</Link>
<Link href="/blog" className={navLinkClass}>
Blog
</Link>
<Link href="#contact" className={navLinkClass}>
Contact Us
</Link>
</div>
<div className="flex items-center gap-4">
{user ? (
<>
<div className="flex items-center gap-2">
{user.photoURL ? (
<Image
src={user.photoURL}
alt={user.displayName}
width={32}
height={32}
className="rounded-full"
/>
) : (
<div className="h-8 w-8 rounded-full bg-mova-mist" />
)}
<span className="text-md font-medium text-mova-ink">
{user.displayName}
</span>
</div>
<button
onClick={handleLogout}
className="rounded-md bg-purple-700 px-4 py-2 text-md font-medium text-white hover:bg-purple-600"
>
Logout
</button>
</>
) : (
<>
<Link href="/profile/login">
<button className="rounded-md bg-purple-700 px-4 py-2 text-md font-medium text-white hover:bg-purple-600 focus:outline-none">
Login
</button>
</Link>
<Link href="/profile/login">
<button className="rounded-md border border-purple-500 px-4 py-2 text-md font-medium text-purple-700 transition hover:bg-purple-700 hover:text-white focus:outline-none">
SignUp
</button>
</Link>
</>
)}
</div>
</div>
<div className="flex items-center justify-between px-3 sm:px-6 md:hidden">
<BrandMark />
{showNav ? (
<AiOutlineClose className="h-9 w-10 pr-2 text-mova-ink" onClick={toggleNav} />
) : (
<AiOutlineMenu className="h-9 w-10 pr-2 text-mova-ink" onClick={toggleNav} />
)}
</div>
{showNav && (
<div className="fixed inset-y-0 right-0 z-50 flex h-screen w-1/2 flex-col items-center bg-white py-6 shadow-mova">
<button className="mb-4 mr-4 self-end" onClick={toggleNav}>
<AiOutlineClose className="h-10 w-8" />
</button>
<div className="w-full divide-y-2 divide-dashed divide-purple-200">
{[
{ href: "/", label: "Home", onClick: closeNavOnClick },
{
href: user ? "/shop" : "/profile/login",
label: "Shop",
onClick: (e) => handleProtectedLinkClick(e, "/shop"),
},
{
href: user ? "/collections" : "/profile/login",
label: "Collections",
onClick: (e) => handleProtectedLinkClick(e, "/collections"),
},
{ href: "#aboutus", label: "About Us", onClick: closeNavOnClick },
{ href: "/blog", label: "Blog", onClick: closeNavOnClick },
{ href: "#contact", label: "Contact Us", onClick: closeNavOnClick },
].map((item) => (
<Link
key={item.label}
href={item.href}
className="flex w-full items-center justify-between py-2 pr-4 text-md font-medium text-mova-ink transition-colors hover:text-purple-600"
onClick={item.onClick}
>
<span className="pl-2">{item.label}</span>
<TfiAngleRight size={20} />
</Link>
))}
</div>
<div className="mt-8 flex flex-col gap-4 text-center">
{user ? (
<>
<div className="mx-2 flex items-center gap-2">
{user.photoURL ? (
<Image
src={user.photoURL}
alt={user.displayName}
width={32}
height={32}
className="rounded-full"
/>
) : (
<div className="h-8 w-8 rounded-full bg-mova-mist" />
)}
<span className="text-sm font-medium text-mova-ink">
{user.displayName}
</span>
</div>
<button
onClick={() => {
handleLogout();
closeNavOnClick();
}}
className="rounded-md bg-purple-700 px-4 py-2 text-md font-medium text-white hover:bg-purple-600 focus:outline-none"
>
Logout
</button>
</>
) : (
<div className="flex space-x-2">
<Link href="/profile/login" onClick={closeNavOnClick}>
<button className="rounded-md bg-purple-700 px-4 py-2 text-md font-medium text-white hover:bg-purple-600 focus:outline-none">
Login
</button>
</Link>
<Link href="/profile/login" onClick={closeNavOnClick}>
<button className="rounded-md border border-purple-500 px-4 py-2 text-md font-medium text-purple-700 transition hover:bg-purple-700 hover:text-white focus:outline-none">
SignUp
</button>
</Link>
</div>
)}
</div>
</div>
)}
<Toast
message={toast.message}
show={toast.show}
onClose={() => setToast({ show: false, message: "" })}
/>
</nav>
</>
);
}
export default Navbar;