diff options
author | eug-vs <eug-vs@keemail.me> | 2020-06-25 13:52:03 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-06-25 13:52:03 +0300 |
commit | 3e92c59a0a79646f77a6f882d3543ecde7cb2dc8 (patch) | |
tree | 6968f3e702d14d7351596eb355be7febb44bf74c /src/components | |
parent | 0204f48a00543ea876efc83260f244f7c14d8a82 (diff) | |
download | which-ui-3e92c59a0a79646f77a6f882d3543ecde7cb2dc8.tar.gz |
feat: use latest API
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Feed/Feed.tsx | 2 | ||||
-rw-r--r-- | src/components/PollCard/PercentageBar.tsx | 12 | ||||
-rw-r--r-- | src/components/PollCard/PollCard.tsx | 42 |
3 files changed, 31 insertions, 25 deletions
diff --git a/src/components/Feed/Feed.tsx b/src/components/Feed/Feed.tsx index c649935..d81da99 100644 --- a/src/components/Feed/Feed.tsx +++ b/src/components/Feed/Feed.tsx @@ -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 27f2203..82d94e9 100644 --- a/src/components/PollCard/PollCard.tsx +++ b/src/components/PollCard/PollCard.tsx @@ -13,7 +13,7 @@ import PercentageBar from './PercentageBar'; import { post } from '../../requests'; interface PropTypes { - poll: Poll; + initialPoll: Poll; navigate: (prefix: string, id: string) => void; } @@ -44,32 +44,34 @@ const useStyles = makeStyles(theme => ({ 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 [rate, setRate] = useState<{left: number, right: number}>({left: contents.left.votes,right: contents.right.votes}); + const { author, contents: { left, right }, userChoice } = poll; const handleNavigate = () => { navigate('profile', poll.author._id); }; const vote = (which: Which) => { - post(`polls/${ poll._id }/votes/`,{ which }).then((response)=>{ - console.log(response.data); - const leftV = response.data.contents.left.votes; - const rightV = response.data.contents.right.votes; - setRate({left: leftV, right: rightV}); - }) - .catch(error => { + if (userChoice) return; + post('votes/', { which, pollId: poll._id }).then(() => { + poll.contents[which].votes += 1; + poll.userChoice = which; + setPoll({ ...poll}); + }).catch(error => { console.log(error.response) }); }; + + const handleLeft = () => vote('left'); + const handleRight = () => vote('right'); - const leftPercentage = Math.round(100 * (rate.left / (rate.left + rate.right))); - const rightPercentage = 100 - leftPercentage; + const leftPercentage = Math.round(100 * (left.votes / (left.votes + right.votes))); + const rightPercentage = 100 - leftPercentage; return ( <Card className={classes.root}> @@ -86,19 +88,19 @@ const PollCard: React.FC<PropTypes> = ({ poll, navigate }) => { title={author.username} /> <div className={classes.imagesBlock}> - <CardActionArea onDoubleClick={() => vote('left')}> + <CardActionArea onDoubleClick={handleLeft}> <CardMedia className={classes.images} - image={contents.left.url} + image={left.url} /> - <PercentageBar value={leftPercentage} which="left" /> + <PercentageBar value={leftPercentage} which="left" like={userChoice === 'left'}/> </CardActionArea> - <CardActionArea onDoubleClick={() => vote('right')}> + <CardActionArea onDoubleClick={handleRight}> <CardMedia className={classes.images} - image={contents.right.url} + image={right.url} /> - <PercentageBar value={rightPercentage} which="right" /> + <PercentageBar value={rightPercentage} which="right" like={userChoice === 'right'}/> </CardActionArea> </div> <div className={classes.rateLine}> |