diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-07 16:51:42 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-07 16:51:42 +0300 |
commit | 4e4c9d529e6f70e7dac84aaecb5b6a25769c3290 (patch) | |
tree | 7cf4ded95eec70201217552aa6dca7b35a2aa16d /src/PollCard/PollCard.tsx | |
parent | ce8764580983e0008300bcc88d463da44d94c455 (diff) | |
parent | d549053615ce308b906ca406f8d6623c0730d636 (diff) | |
download | which-ui-4e4c9d529e6f70e7dac84aaecb5b6a25769c3290.tar.gz |
Merge pull request #11 from ilyayudovin/poll-functionality
Add functionality to Poll component
Diffstat (limited to 'src/PollCard/PollCard.tsx')
-rw-r--r-- | src/PollCard/PollCard.tsx | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/src/PollCard/PollCard.tsx b/src/PollCard/PollCard.tsx index 05e941c..07449fb 100644 --- a/src/PollCard/PollCard.tsx +++ b/src/PollCard/PollCard.tsx @@ -7,6 +7,12 @@ import { Avatar, CardHeader } from '@material-ui/core/'; +import { Poll } from '../types'; + +interface PercentageBarPropTypes { + value: number; + which: 'left' | 'right'; +} const useStyles = makeStyles({ root: { @@ -21,55 +27,69 @@ const useStyles = makeStyles({ imagesBlock: { display: 'flex' }, - percentageLeft: { + percentage: { position: 'absolute', color: 'white', top: '86%', - left: 30, fontSize: 20 }, + percentageLeft: { + left: 30 + }, percentageRight: { - position: 'absolute', - color: 'white', - top: '86%', - right: 30, - fontSize: 20 + right: 30 } - }); -const PollCard: React.FC = () => { + +const PercentageBar: React.FC<PercentageBarPropTypes> = ({ value, which }) => { const classes = useStyles(); + const positionClassName = which === 'left' ? 'percentageLeft' : 'percentageRight'; + + return ( + <div className={`${classes.percentage} ${classes[positionClassName]}`}> + {value} + % + </div> + ); +}; + + +const PollCard: React.FC<Poll> = ({ author, contents: { left, right } }) => { + const classes = useStyles(); + + const leftPercentage = Math.round(100 * (left.votes / (left.votes + right.votes))); + const rightPercentage = 100 - leftPercentage; return ( <Card className={classes.root}> <CardHeader avatar={( <Avatar aria-label="avatar"> - R + <img src={author.avatarUrl} alt={author.name[0].toUpperCase()} /> </Avatar> )} - title="Nick Name" + title={author.name} /> <div className={classes.imagesBlock}> <CardActionArea> <CardMedia className={classes.images} - // eslint-disable-next-line max-len - image="https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" + image={left.url} /> - <div className={classes.percentageLeft}>25%</div> + <PercentageBar value={leftPercentage} which="left" /> </CardActionArea> <CardActionArea> <CardMedia className={classes.images} - // eslint-disable-next-line max-len - image="https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg" + image={right.url} /> - <div className={classes.percentageRight}>75%</div> + <PercentageBar value={rightPercentage} which="right" /> </CardActionArea> </div> </Card> ); }; + export default PollCard; + |