Skip to content

Commit

Permalink
Add logout logic (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Jan 19, 2024
1 parent 65157f4 commit cbd38ea
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
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

0 comments on commit cbd38ea

Please sign in to comment.