Skip to content

Commit

Permalink
Feat: 관리자 계정 접속 시 Admin Page로 이동 및 하위 항목 추가 (#28)
Browse files Browse the repository at this point in the history
- 관리자 계정으로 접속한 경우 My Page 대신 Admin Page로 접속하도록 수정
- Admin Page 하위 항목에 게시물 관리 및 채팅방 관리 추가
- 권한에 따른 페이지 전환 로직 및 네비게이션 구조 개선
  • Loading branch information
sunglitter committed Dec 6, 2024
1 parent c32f792 commit 9ab655c
Showing 1 changed file with 97 additions and 6 deletions.
103 changes: 97 additions & 6 deletions src/components/common/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,40 @@ import logo from '../../assets/icons/goodbuyus-logo.svg';
import menu from '../../assets/icons/menu.svg';
const Header = () => {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [isAdmin, setIsAdmin] = useState(false);
const [isUser, setIsUser] = useState(false);
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
const [isAdminDropdownOpen, setIsAdminDropdownOpen] = useState(false);

useEffect(() => {
const token = localStorage.getItem('jwt');
setIsLoggedIn(!!token);

// 관리자 여부 확인 (예: localStorage에서 'role'의 ADMIN 여부 확인)
const adminStatus = localStorage.getItem('role') === 'ROLE_ADMIN';
const userStatus = localStorage.getItem('role') === 'ROLE_USER';
setIsAdmin(adminStatus);
setIsUser(userStatus);
}, []);

const toggleMobileMenu = () => {
setIsMobileMenuOpen(!isMobileMenuOpen);
};

const toggleAdminDropdown = () => {
setIsAdminDropdownOpen((prev) => !prev);
};

const handleCommunityClick = (
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>
) => {
if (!isUser && !isAdmin) {
e.preventDefault();
alert('로그인 후 이용할 수 있는 페이지입니다.');
setIsMobileMenuOpen(!isMobileMenuOpen);
}
};

return (
<HeaderContainer>
<HeaderContent>
Expand Down Expand Up @@ -50,15 +73,44 @@ const Header = () => {
</StyledLink>
</NavItem>
<NavItem>
<StyledLink to="/community/post" onClick={toggleMobileMenu}>
<StyledLink to="/community/post" onClick={handleCommunityClick}>
Community
</StyledLink>
</NavItem>
<NavItem>
<StyledLink to="/mypage/setting" onClick={toggleMobileMenu}>
My Page
</StyledLink>
</NavItem>
{isAdmin ? (
<NavItem>
<StyledLink
to="#"
onMouseEnter={toggleAdminDropdown}
onMouseLeave={toggleAdminDropdown}
>
Admin Page
</StyledLink>
{isAdminDropdownOpen && (
<AdminMenu
onMouseEnter={toggleAdminDropdown}
onMouseLeave={toggleAdminDropdown}
>
<AdminDropdown>
<DropdownItem>
<StyledLink to="/admin/post">게시물 관리</StyledLink>
</DropdownItem>
<DropdownItem>
<StyledLink to="/admin/chatlist">
채팅방 관리
</StyledLink>
</DropdownItem>
</AdminDropdown>
</AdminMenu>
)}
</NavItem>
) : (
<NavItem>
<StyledLink to="/mypage/setting" onClick={toggleMobileMenu}>
My Page
</StyledLink>
</NavItem>
)}
{!isLoggedIn ? (
<Login>
<Link to="/signin" onClick={toggleMobileMenu}>
Expand Down Expand Up @@ -297,3 +349,42 @@ const CartIcon = styled.div`
}
}
`;

const AdminMenu = styled.li`
position: relative;
margin-left: 12.5px;
a {
font-weight: bold;
padding: 5px 5px;
border-radius: 5px;
text-decoration: none;
color: black;
}
&:hover {
background-color: #f4f4f4;
}
`;

const AdminDropdown = styled.ul`
position: absolute;
top: 100%;
left: 0;
background-color: white;
list-style: none;
margin: 0;
padding: 10px 0;
border-radius: 5px;
transform: translateY(-10%); /* 추가로 위치 조정 */
z-index: 10; /* 다른 요소 위로 표시 */
`;

const DropdownItem = styled.li`
font-size: 0.9rem;
a {
text-decoration: none;
color: black;
display: block;
}
`;

0 comments on commit 9ab655c

Please sign in to comment.