diff options
-rw-r--r-- | src/components/Avatar/Avatar.tsx | 36 | ||||
-rw-r--r-- | src/components/Header/Header.tsx | 8 | ||||
-rw-r--r-- | src/components/UserStrip/UserStrip.tsx | 26 | ||||
-rw-r--r-- | src/containers/Profile/ProfileInfo.tsx | 11 |
4 files changed, 53 insertions, 28 deletions
diff --git a/src/components/Avatar/Avatar.tsx b/src/components/Avatar/Avatar.tsx new file mode 100644 index 0000000..e445891 --- /dev/null +++ b/src/components/Avatar/Avatar.tsx @@ -0,0 +1,36 @@ +import React from 'react'; +import { useHistory } from 'react-router-dom'; +import { Avatar as AvatarBase } from '@material-ui/core'; +import AccountCircle from '@material-ui/icons/AccountCircle'; +import { User } from 'which-types'; + +interface PropTypes { + user: User; + className?: string; +} + +const Avatar: React.FC<PropTypes> = ({ user, className }) => { + const history = useHistory(); + const { username, avatarUrl } = user; + + const handleClick = () => { + history.push(`/profile/${username}`); + }; + + return avatarUrl ? ( + <AvatarBase + src={avatarUrl} + alt={username[0].toUpperCase()} + onClick={handleClick} + className={className} + style={{ cursor: 'pointer' }} + /> + ) : ( + <AccountCircle + className={className} + onClick={handleClick} + /> + ); +}; + +export default Avatar; diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index 344b839..5621dbf 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -3,7 +3,6 @@ import { useHistory } from 'react-router-dom'; import { IconButton, Typography, - Avatar, useMediaQuery } from '@material-ui/core'; import { @@ -19,6 +18,7 @@ import { useAuth } from '../../hooks/useAuth'; import MobileHeader from './MobileHeader'; import BottomBar from './BottomBar'; import BrowserHeader from './BrowserHeader'; +import Avatar from '../Avatar/Avatar'; const useStyles = makeStyles(theme => ({ @@ -90,9 +90,9 @@ const Header: React.FC = React.memo(() => { const profile = ( <IconButton onClick={handleProfile}> { - user?.avatarUrl - ? <Avatar className={classes.avatar} src={user?.avatarUrl} /> - : <AccountCircle /> + user + ? <Avatar className={classes.avatar} user={user} /> + : <AccountCircle className={classes.avatar} /> } </IconButton> ); diff --git a/src/components/UserStrip/UserStrip.tsx b/src/components/UserStrip/UserStrip.tsx index 73d9363..a837961 100644 --- a/src/components/UserStrip/UserStrip.tsx +++ b/src/components/UserStrip/UserStrip.tsx @@ -1,10 +1,11 @@ import React from 'react'; -import { useHistory } from 'react-router-dom'; import { makeStyles } from '@material-ui/core/styles'; import VerifiedIcon from '@material-ui/icons/CheckCircleOutline'; -import { Avatar, CardHeader } from '@material-ui/core/'; +import { CardHeader } from '@material-ui/core/'; import { User } from 'which-types'; +import Avatar from '../Avatar/Avatar'; + interface PropTypes { user: User; @@ -21,30 +22,15 @@ const useStyles = makeStyles(theme => ({ marginLeft: theme.spacing(0.5), width: theme.spacing(2), height: theme.spacing(2) - }, - avatar: { - cursor: 'pointer' } })); const UserStrip: React.FC<PropTypes> = ({ user, info }) => { const classes = useStyles(); - const history = useHistory(); - const { username, avatarUrl, verified } = user; - - const handleNavigate = () => { - history.push(`/profile/${username}`); - }; - - const avatar = ( - <Avatar - src={avatarUrl} - alt={username[0].toUpperCase()} - onClick={handleNavigate} - className={classes.avatar} - /> - ); + const { username, verified } = user; + + const avatar = <Avatar user={user} />; const title = ( <div className={classes.root}> diff --git a/src/containers/Profile/ProfileInfo.tsx b/src/containers/Profile/ProfileInfo.tsx index 9eee4c1..a01c222 100644 --- a/src/containers/Profile/ProfileInfo.tsx +++ b/src/containers/Profile/ProfileInfo.tsx @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { Avatar, Badge, Typography } from '@material-ui/core/'; +import { Badge, Typography } from '@material-ui/core/'; import { makeStyles } from '@material-ui/core/styles'; import { User } from 'which-types'; import CameraAltIcon from '@material-ui/icons/CameraAlt'; @@ -8,9 +8,11 @@ import Skeleton from '@material-ui/lab/Skeleton'; import MoreMenu from './MoreMenu'; import Highlight from './Highlight'; import UploadImage from '../../components/UploadImage/UploadImage'; +import Avatar from '../../components/Avatar/Avatar'; import { patch } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; + interface PropTypes { savedPolls: number; totalVotes: number; @@ -116,19 +118,20 @@ const ProfileInfo: React.FC<PropTypes> = ({ vertical: 'bottom', horizontal: 'right' }} + onClick={handleClick} badgeContent={( <div className={classes.badge}> - <CameraAltIcon onClick={handleClick} /> + <CameraAltIcon /> </div> )} > - <Avatar className={classes.avatar} src={userInfo?.avatarUrl} /> + <Avatar className={classes.avatar} user={userInfo} /> </Badge> </div> <UploadImage isOpen={input} setIsOpen={setInput} callback={patchAvatar} /> </div> ) - : <Avatar className={classes.avatar} src={userInfo?.avatarUrl} /> + : <Avatar className={classes.avatar} user={userInfo} /> } { !userInfo |