Skip to content

Commit 4d15c9b

Browse files
committed
chore: adjust menu based on role
1 parent 66f2fb8 commit 4d15c9b

4 files changed

Lines changed: 46 additions & 28 deletions

File tree

src/components/layout/Navbar/UserMenu.tsx

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { Button } from "@/components/ui/Button";
22
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/DropdownMenu";
33
import { Link } from "@/lib/navigation";
44
import { ChevronLeft, LogOut, User } from "lucide-react";
5-
import { USER_LINKS } from "./constant";
65
import { useTranslations } from "next-intl";
76
import { useAuthUser } from "@/components/hooks/UseAuthUser";
87
import { useAuthService } from "@/features/auth/hooks/useAuth";
8+
import { getUserLinks } from "./constant";
99

1010
const DesktopUserMenu = () => {
1111
const t = useTranslations("Layout");
@@ -41,16 +41,12 @@ const DesktopUserMenu = () => {
4141
</div>
4242
</Link>
4343
</DropdownMenuItem>
44-
{user?.role === "admin" && (
45-
<DropdownMenuItem asChild>
46-
<Link href="/admin/events">Dashboard</Link>
47-
</DropdownMenuItem>
48-
)}
49-
{USER_LINKS.map(({ id, href }) => (
50-
<DropdownMenuItem key={id} asChild>
51-
<Link href={href}>{t(`navbar.user.${id}`)}</Link>
52-
</DropdownMenuItem>
53-
))}
44+
{user &&
45+
getUserLinks(user.role).map(({ id, href }) => (
46+
<DropdownMenuItem key={id} asChild>
47+
<Link href={href}>{t(`navbar.user.${id}`)}</Link>
48+
</DropdownMenuItem>
49+
))}
5450
<DropdownMenuItem onClick={logout}>
5551
<span className="text-destructive hover:text-destructive/80 flex cursor-pointer items-center gap-2">
5652
<LogOut size={16} />
@@ -84,13 +80,14 @@ const MobileUserMenu = () => {
8480
<p className="text-muted-foreground text-xs">{user?.email}</p>
8581
</div>
8682
</Link>
87-
{USER_LINKS.map(({ id, href }) => (
88-
<Link key={id} href={href}>
89-
<Button variant="outline" size="sm" className="w-full cursor-pointer justify-start">
90-
{t(`navbar.user.${id}`)}
91-
</Button>
92-
</Link>
93-
))}
83+
{user &&
84+
getUserLinks(user.role).map(({ id, href }) => (
85+
<Link key={id} href={href}>
86+
<Button variant="outline" size="sm" className="w-full cursor-pointer justify-start">
87+
{t(`navbar.user.${id}`)}
88+
</Button>
89+
</Link>
90+
))}
9491
<Button
9592
variant="outline"
9693
size="sm"

src/components/layout/Navbar/constant.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,32 @@ export const LINKS = [
2121
},
2222
];
2323

24-
export const USER_LINKS = [
25-
// {
26-
// id: "my-blogs",
27-
// href: "/my-blogs",
28-
// },
29-
{
30-
id: "my-events",
31-
href: "/my-events",
32-
},
33-
];
24+
export type UserRole = "admin" | "user";
25+
26+
export interface LinkItem {
27+
id: string;
28+
href: string;
29+
}
30+
31+
export const USER_LINKS: Record<UserRole, LinkItem[]> = {
32+
admin: [
33+
{
34+
id: "dashboard",
35+
href: "/admin/events",
36+
},
37+
],
38+
user: [
39+
// {
40+
// id: "my-blogs",
41+
// href: "/my-blogs",
42+
// },
43+
{
44+
id: "my-events",
45+
href: "/my-events",
46+
},
47+
],
48+
};
49+
50+
export const getUserLinks = (role: string): LinkItem[] => {
51+
return USER_LINKS[role as UserRole] || [];
52+
};

src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"sign-out": "Sign Out",
1212
"profile": "Profile",
1313
"user": {
14+
"dashboard": "Dashboard",
1415
"my-blogs": "My Blogs",
1516
"my-events": "My Events"
1617
}

src/locales/id.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"sign-out": "Keluar",
1212
"profile": "Profil",
1313
"user": {
14+
"dashboard": "Dashboard",
1415
"my-blogs": "Blog Saya",
1516
"my-events": "Event Saya"
1617
}

0 commit comments

Comments
 (0)