diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-25 14:41:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-25 14:41:14 +0300 |
commit | 9dfc98d5014f91afb45ad4eebbe9f0f704ddfdf5 (patch) | |
tree | b5a47cce16b224dd74625db72f721e5e797a1611 /src/components | |
parent | 190916329611704b1035158363302f09152794ee (diff) | |
parent | a3afe917442fb29627aa9deade1baefbea9a60c5 (diff) | |
download | which-ui-9dfc98d5014f91afb45ad4eebbe9f0f704ddfdf5.tar.gz |
Merge pull request #41 from which-ecosystem/votes
Votes
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Feed/Feed.tsx | 4 | ||||
-rw-r--r-- | src/components/PollCard/PercentageBar.tsx | 12 | ||||
-rw-r--r-- | src/components/PollCard/PollCard.tsx | 79 |
3 files changed, 68 insertions, 27 deletions
diff --git a/src/components/Feed/Feed.tsx b/src/components/Feed/Feed.tsx index 3b8e16f..d81da99 100644 --- a/src/components/Feed/Feed.tsx +++ b/src/components/Feed/Feed.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; -import { Poll } from '../../types'; +import { Poll } from 'which-types'; import PollCard from '../PollCard/PollCard'; interface PropTypes { @@ -21,7 +21,7 @@ const Feed: React.FC<PropTypes> = ({ polls, navigate }) => { return ( <div className={classes.root}> - {polls.map(poll => <PollCard poll={poll} key={poll._id} navigate={navigate} />)} + {polls.map(poll => <PollCard initialPoll={poll} key={poll._id} navigate={navigate} />)} </div> ); }; diff --git a/src/components/PollCard/PercentageBar.tsx b/src/components/PollCard/PercentageBar.tsx index 6a50a9e..a93d7b4 100644 --- a/src/components/PollCard/PercentageBar.tsx +++ b/src/components/PollCard/PercentageBar.tsx @@ -1,9 +1,11 @@ import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; +import LikeIcon from '@material-ui/icons/Favorite'; interface PropTypes { value: number; which: 'left' | 'right'; + like: boolean; } const useStyles = makeStyles({ @@ -12,7 +14,9 @@ const useStyles = makeStyles({ color: 'white', top: '86%', fontSize: 20, - textShadow: '0 0 3px black' + textShadow: '0 0 3px black', + display: 'flex', + alignItems: 'center' }, left: { left: 30 @@ -22,13 +26,13 @@ const useStyles = makeStyles({ } }); -const PercentageBar: React.FC<PropTypes> = ({ value, which }) => { +const PercentageBar: React.FC<PropTypes> = ({ value, which, like }) => { const classes = useStyles(); return ( <div className={`${classes.root} ${classes[which]}`}> - {value} - % + {like && <LikeIcon />} + {`${value}%`} </div> ); }; diff --git a/src/components/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx index baf896f..40f5fd7 100644 --- a/src/components/PollCard/PollCard.tsx +++ b/src/components/PollCard/PollCard.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Card, @@ -7,43 +7,73 @@ import { Avatar, CardHeader } from '@material-ui/core/'; -import { Poll } from '../../types'; +import { Which, Poll } from 'which-types'; + import PercentageBar from './PercentageBar'; +import { post } from '../../requests'; interface PropTypes { - poll: Poll; + initialPoll: Poll; navigate: (prefix: string, id: string) => void; } const useStyles = makeStyles(theme => ({ root: { maxWidth: theme.spacing(75), - height: theme.spacing(63), - margin: '20px auto' + height: 488, + margin: '40px auto' }, images: { height: theme.spacing(50), - width: theme.spacing(38) + width: 300 }, imagesBlock: { display: 'flex' }, avatar: { cursor: 'pointer' + }, + rateLine: { + position: 'relative', + width: '100%', + height: theme.spacing(2), + backgroundColor: theme.palette.primary.light + }, + fillRateLine: { + height: theme.spacing(2), + backgroundColor: theme.palette.primary.main, + transitionDuration: '0.5s' } })); - -const PollCard: React.FC<PropTypes> = ({ poll, navigate }) => { +const PollCard: React.FC<PropTypes> = ({ initialPoll, navigate }) => { + const [poll, setPoll] = useState<Poll>(initialPoll); const classes = useStyles(); - const { author, contents } = poll; + const { author, contents: { left, right }, userChoice } = poll; const handleNavigate = () => { navigate('profile', poll.author._id); }; - const leftPercentage = Math.round(100 * (contents.left.votes / (contents.left.votes + contents.right.votes))); - const rightPercentage = 100 - leftPercentage; + const vote = (which: Which) => { + if (userChoice) return; + post('votes/', { which, pollId: poll._id }).then(() => { + poll.contents[which].votes += 1; + poll.userChoice = which; + setPoll({ ...poll }); + }); + }; + + const handleLeft = () => vote('left'); + const handleRight = () => vote('right'); + + const leftPercentage = Math.round(100 * (left.votes / (left.votes + right.votes))); + + const percentage = { + left: leftPercentage, + right: 100 - leftPercentage + }; + const dominant: Which = left.votes >= right.votes ? 'left' : 'right'; return ( <Card className={classes.root}> @@ -52,33 +82,40 @@ const PollCard: React.FC<PropTypes> = ({ poll, navigate }) => { <Avatar aria-label="avatar" src={author.avatarUrl} - alt={author.name[0].toUpperCase()} + alt={author.username[0].toUpperCase()} onClick={handleNavigate} className={classes.avatar} /> )} - title={author.name} + title={author.username} /> <div className={classes.imagesBlock}> - <CardActionArea> + <CardActionArea onDoubleClick={handleLeft}> <CardMedia className={classes.images} - image={contents.left.url} + image={left.url} /> - <PercentageBar value={leftPercentage} which="left" /> + <PercentageBar value={percentage.left} which="left" like={userChoice === 'left'} /> </CardActionArea> - <CardActionArea> + <CardActionArea onDoubleClick={handleRight}> <CardMedia className={classes.images} - image={contents.right.url} + image={right.url} /> - <PercentageBar value={rightPercentage} which="right" /> + <PercentageBar value={percentage.right} which="right" like={userChoice === 'right'} /> </CardActionArea> </div> + <div className={classes.rateLine}> + <div + className={classes.fillRateLine} + style={{ + width: `${percentage[dominant]}%`, + float: dominant + }} + /> + </div> </Card> ); }; - export default PollCard; - |