diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-30 01:47:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-30 01:47:27 +0300 |
commit | 720a32c4cb1697f3a8f90973f28c334551ab87ff (patch) | |
tree | c9028ea3ff850774d33cbc510eb19dd0e9f7aade /src/pages/ProfilePage | |
parent | b301bf24c5037403a1e5fc32fc8c10794941b528 (diff) | |
parent | e170d04d5d2c8c86d2683f3accb4feb2d94c881a (diff) | |
download | which-ui-720a32c4cb1697f3a8f90973f28c334551ab87ff.tar.gz |
Merge pull request #54 from which-ecosystem/hooks
Use hooks logic
Diffstat (limited to 'src/pages/ProfilePage')
-rw-r--r-- | src/pages/ProfilePage/MoreMenu.tsx | 18 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfileInfo.tsx | 24 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfilePage.tsx | 52 |
3 files changed, 47 insertions, 47 deletions
diff --git a/src/pages/ProfilePage/MoreMenu.tsx b/src/pages/ProfilePage/MoreMenu.tsx index bf3347b..4e681f5 100644 --- a/src/pages/ProfilePage/MoreMenu.tsx +++ b/src/pages/ProfilePage/MoreMenu.tsx @@ -4,10 +4,8 @@ import Menu from '@material-ui/core/Menu'; import MenuItem from '@material-ui/core/MenuItem'; import MoreHorizIcon from '@material-ui/icons/MoreHoriz'; import { makeStyles } from '@material-ui/core'; - -interface PropTypes { - logOut: () => void; -} +import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from '../../hooks/useNavigate'; const ITEM_HEIGHT = 48; @@ -19,15 +17,23 @@ const useStyles = makeStyles({ } }); -const MoreMenu: React.FC<PropTypes> = ({ logOut }) => { +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); const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { setAnchorEl(event.currentTarget); }; + const handleLogout = () => { + logout(); + navigate('auth'); + }; + const handleClose = () => { setAnchorEl(null); }; @@ -56,7 +62,7 @@ const MoreMenu: React.FC<PropTypes> = ({ logOut }) => { } }} > - <MenuItem onClick={logOut}>Log out</MenuItem> + <MenuItem onClick={handleLogout}>Log out</MenuItem> </Menu> </div> </div> diff --git a/src/pages/ProfilePage/ProfileInfo.tsx b/src/pages/ProfilePage/ProfileInfo.tsx index 9fe5912..9557a5e 100644 --- a/src/pages/ProfilePage/ProfileInfo.tsx +++ b/src/pages/ProfilePage/ProfileInfo.tsx @@ -8,15 +8,14 @@ import MoreMenu from './MoreMenu'; import Highlight from './Highlight'; import UploadImage from '../../components/UploadImage/UploadImage'; import { patch } from '../../requests'; +import { useAuth } from '../../hooks/useAuth'; interface PropTypes { - user: User | undefined; - logOut: () => void; savedPolls: number; totalVotes: number; - setUserInfo: (a: User) => void; - setUser: (a:User) => void; + userInfo: User | undefined; + setUserInfo: (userInfo: User) => void; } const useStyles = makeStyles(theme => ({ @@ -78,13 +77,14 @@ const useStyles = makeStyles(theme => ({ const ProfileInfo: React.FC<PropTypes> = ({ - user, logOut, savedPolls, totalVotes, setUserInfo, setUser + savedPolls, totalVotes, setUserInfo, userInfo }) => { const classes = useStyles(); const [input, setInput] = useState(false); + const { setUser } = useAuth(); - const dateSince = new Date(user?.createdAt || '').toLocaleDateString(); + const dateSince = new Date(userInfo?.createdAt || '').toLocaleDateString(); const handleClick = () => { setInput(!input); @@ -101,10 +101,10 @@ const ProfileInfo: React.FC<PropTypes> = ({ return ( <div className={classes.root}> { - user?._id === localStorage.getItem('userId') + userInfo?._id === localStorage.getItem('userId') ? ( <div> - <MoreMenu logOut={logOut} /> + <MoreMenu /> <div className={classes.avatarContainer}> <Badge overlap="circle" @@ -118,17 +118,17 @@ const ProfileInfo: React.FC<PropTypes> = ({ </div> )} > - <Avatar className={classes.avatar} src={user?.avatarUrl} /> + <Avatar className={classes.avatar} src={userInfo?.avatarUrl} /> </Badge> </div> <UploadImage isOpen={input} setIsOpen={setInput} callback={patchAvatar} /> </div> ) - : <Avatar className={classes.avatar} src={user?.avatarUrl} /> + : <Avatar className={classes.avatar} src={userInfo?.avatarUrl} /> } <Typography variant="h5" className={classes.name}> - {user?.username} - {user?.verified && <VerifiedIcon className={classes.verified} color="primary" />} + {userInfo?.username} + {userInfo?.verified && <VerifiedIcon className={classes.verified} color="primary" />} </Typography> <div className={classes.profileMenu}> <Highlight text="Polls" value={savedPolls} /> diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index b0ac103..2c18466 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -4,50 +4,44 @@ import { User, Poll } from 'which-types'; import ProfileInfo from './ProfileInfo'; import Feed from '../../components/Feed/Feed'; import { get } from '../../requests'; +import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from '../../hooks/useNavigate'; -interface PropTypes { - logOut: () => void; - navigate: (prefix: string, id: string) => void; - id: string; - setUser:(a:User)=>void; -} -const ProfilePage: React.FC<PropTypes> = ({ - logOut, id, navigate, setUser -}) => { +const ProfilePage: React.FC = () => { const [userInfo, setUserInfo] = useState<User>(); const [polls, setPolls] = useState<Poll[]>([]); const [totalVotes, setTotalVotes] = useState<number>(0); + const { page, navigate } = useNavigate(); + const { user } = useAuth(); 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 ( <> <ProfileInfo - user={userInfo} + userInfo={userInfo} setUserInfo={setUserInfo} - setUser={setUser} - logOut={logOut} savedPolls={polls.length} totalVotes={totalVotes} /> - <Feed polls={[...polls]} navigate={navigate} /> + <Feed polls={[...polls]} /> </> ); }; |