Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FE) Add Logout #71

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions frontend/src/components/common/ProfilePopover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
ListItemIcon,
ListItemText,
MenuItem,
MenuList,
Popover,
PopoverProps,
} from "@mui/material";
import LogoutIcon from "@mui/icons-material/Logout";
import { useDispatch } from "react-redux";
import { setAccessToken } from "../../store/authSlice";
import { setUserData } from "../../store/userSlice";

function ProfilePopover(props: PopoverProps) {
const dispatch = useDispatch();

const handleLogout = () => {
dispatch(setAccessToken(null));
dispatch(setUserData(null));
};

return (
<Popover
anchorOrigin={{
vertical: "top",
horizontal: "right",
}}
transformOrigin={{
vertical: "bottom",
horizontal: "right",
}}
{...props}
>
<MenuList>
<MenuItem onClick={handleLogout}>
<ListItemIcon>
<LogoutIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Logout</ListItemText>
</MenuItem>
</MenuList>
</Popover>
);
}

export default ProfilePopover;
23 changes: 22 additions & 1 deletion frontend/src/components/drawers/WorkspaceDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@ import {
ListItem,
ListItemAvatar,
ListItemButton,
ListItemSecondaryAction,
ListItemText,
} from "@mui/material";
import MoreVertIcon from "@mui/icons-material/MoreVert";
import { useSelector } from "react-redux";
import { selectUser } from "../../store/userSlice";
import { MouseEventHandler, useState } from "react";
import ProfilePopover from "../common/ProfilePopover";

const DRAWER_WIDTH = 240;

function WorkspaceDrawer() {
const userStore = useSelector(selectUser);
const [profileAnchorEl, setProfileAnchorEl] = useState<(EventTarget & Element) | null>(null);

const handleOpenProfilePopover: MouseEventHandler = (event) => {
setProfileAnchorEl(event.currentTarget);
};

const handleCloseProfilePopover = () => {
setProfileAnchorEl(null);
};

return (
<Drawer
Expand All @@ -33,12 +46,20 @@ function WorkspaceDrawer() {
<Box sx={{ mt: "auto" }}>
<Divider />
<ListItem disablePadding>
<ListItemButton>
<ListItemButton onClick={handleOpenProfilePopover}>
<ListItemAvatar>
<Avatar>{userStore.data?.nickname.charAt(0)}</Avatar>
</ListItemAvatar>
<ListItemText primary={userStore.data?.nickname} />
<ListItemSecondaryAction>
<MoreVertIcon />
</ListItemSecondaryAction>
</ListItemButton>
<ProfilePopover
open={Boolean(profileAnchorEl)}
anchorEl={profileAnchorEl}
onClose={handleCloseProfilePopover}
/>
</ListItem>
</Box>
</Drawer>
Expand Down
Loading