diff options
Diffstat (limited to 'src/pages/ProfilePage')
-rw-r--r-- | src/pages/ProfilePage/MoreMenu.tsx | 9 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfilePage.tsx | 36 |
2 files changed, 25 insertions, 20 deletions
diff --git a/src/pages/ProfilePage/MoreMenu.tsx b/src/pages/ProfilePage/MoreMenu.tsx index 7c17f1e..4e681f5 100644 --- a/src/pages/ProfilePage/MoreMenu.tsx +++ b/src/pages/ProfilePage/MoreMenu.tsx @@ -5,6 +5,7 @@ import MenuItem from '@material-ui/core/MenuItem'; import MoreHorizIcon from '@material-ui/icons/MoreHoriz'; import { makeStyles } from '@material-ui/core'; import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from '../../hooks/useNavigate'; const ITEM_HEIGHT = 48; @@ -20,6 +21,7 @@ const MoreMenu: React.FC = () => { const classes = useStyles(); const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null); const { logout } = useAuth(); + const { navigate } = useNavigate(); const open = Boolean(anchorEl); @@ -27,6 +29,11 @@ const MoreMenu: React.FC = () => { setAnchorEl(event.currentTarget); }; + const handleLogout = () => { + logout(); + navigate('auth'); + }; + const handleClose = () => { setAnchorEl(null); }; @@ -55,7 +62,7 @@ const MoreMenu: React.FC = () => { } }} > - <MenuItem onClick={logout}>Log out</MenuItem> + <MenuItem onClick={handleLogout}>Log out</MenuItem> </Menu> </div> </div> diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index d354a45..ca39746 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -12,28 +12,26 @@ const ProfilePage: React.FC = () => { const [userInfo, setUserInfo] = useState<User>(); const [polls, setPolls] = useState<Poll[]>([]); const [totalVotes, setTotalVotes] = useState<number>(0); - const { page } = useNavigate(); + const { page, navigate } = useNavigate(); const { user } = useAuth(); - const id = page?.id || user?._id; - - useEffect(() => { - get(`/users/${id}`).then(response => { - setUserInfo(response.data); - }); - }, [id]); - useEffect(() => { - get(`/profiles/${id}`).then(response => { - setPolls(response.data); - setTotalVotes(response.data.reduce( - (total: number, current: Poll) => { - const { left, right } = current.contents; - return total + left.votes + right.votes; - }, 0 - )); - }); - }, [id, userInfo]); + const id = page?.id || user?._id + if (id) { + get(`/users/${id}`).then(response => { + setUserInfo(response.data); + }); + get(`/profiles/${id}`).then(response => { + setPolls(response.data); + setTotalVotes(response.data.reduce( + (total: number, current: Poll) => { + const { left, right } = current.contents; + return total + left.votes + right.votes; + }, 0 + )); + }); + } else navigate('auth'); + }, [navigate, page, user]); return ( <> |