Skip to content

Commit

Permalink
belindas-closet-nextjs_7_392_nav-bar-drop-down-menu (#393)
Browse files Browse the repository at this point in the history
* Condense "Profile" and "My account"

* Route users from My Account button to proper pages
  • Loading branch information
heosman authored May 23, 2024
1 parent 60f6e07 commit 45c8242
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions components/AuthProfileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import React, { Fragment } from "react";

export default function AuthProfileMenu() {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const [userRole, setUserRole] = React.useState("");
const token = localStorage.getItem("token");
React.useEffect(() => {
if (token) {
const role = JSON.parse(atob(token.split(".")[1])).role;
setUserRole(role);
}
}, [token, userRole]);

const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
Expand All @@ -25,6 +34,16 @@ export default function AuthProfileMenu() {

const router = useRouter();

const handleMyAccount = () => {
if (userRole == "admin") {
router.push("/admin-page")
} else if (userRole == "creator") {
router.push("/creator-page")
} else {
router.push("/profile")
}
};

const handleLogOut = () => {
localStorage.removeItem("token");
window.location.reload();
Expand Down Expand Up @@ -81,11 +100,8 @@ export default function AuthProfileMenu() {
transformOrigin={{ horizontal: "right", vertical: "top" }}
anchorOrigin={{ horizontal: "right", vertical: "bottom" }}
>
<MenuItem onClick={handleClose}>
<Avatar /> Profile
</MenuItem>
<MenuItem onClick={handleClose}>
<Avatar /> My account
<MenuItem onClick={handleMyAccount}>
<Avatar /> My Account
</MenuItem>
<Divider />
<MenuItem onClick={handleClose}>
Expand Down

0 comments on commit 45c8242

Please sign in to comment.