diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-10 13:51:11 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-10 13:51:11 +0300 |
commit | 823c82383424616bc7c2562e2a763321edb6050c (patch) | |
tree | 1d5220d68ab8ebb392c87038f2fc24cc72b28775 /src/components/PollCard/PollCard.tsx | |
parent | 70d20b76f042a519e8e164279dfa31b5ce027d44 (diff) | |
parent | 78218c0f3427ad79de003ac59cffb99b08f0ae7d (diff) | |
download | which-ui-823c82383424616bc7c2562e2a763321edb6050c.tar.gz |
Merge pull request #74 from which-ecosystem/fetching
SWR feat. crazy refactor
Diffstat (limited to 'src/components/PollCard/PollCard.tsx')
-rw-r--r-- | src/components/PollCard/PollCard.tsx | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/components/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx index 98ae001..689e872 100644 --- a/src/components/PollCard/PollCard.tsx +++ b/src/components/PollCard/PollCard.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Card, @@ -14,7 +14,8 @@ import { post } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; interface PropTypes { - initialPoll: Poll; + poll: Poll; + setPoll: (poll: Poll) => void; } const DATE_FORMAT = { @@ -49,8 +50,7 @@ const useStyles = makeStyles(theme => ({ } })); -const PollCard: React.FC<PropTypes> = ({ initialPoll }) => { - const [poll, setPoll] = useState<Poll>(initialPoll); +const PollCard: React.FC<PropTypes> = ({ poll, setPoll }) => { const classes = useStyles(); const { author, contents: { left, right }, vote } = poll; const { enqueueSnackbar } = useSnackbar(); @@ -58,7 +58,7 @@ const PollCard: React.FC<PropTypes> = ({ initialPoll }) => { const date: string = new Date(poll.createdAt).toLocaleString('default', DATE_FORMAT); const handleVote = (which: Which) => { - if (!isAuthenticated()) { + if (!isAuthenticated) { enqueueSnackbar('Unauthorized users can not vote in polls', { variant: 'error' }); @@ -68,15 +68,17 @@ const PollCard: React.FC<PropTypes> = ({ initialPoll }) => { }); } else { const newVote = ({ which, pollId: poll._id }); - post('votes/', newVote); - poll.contents[which].votes += 1; - poll.vote = { + const newPoll = { ...poll }; + newPoll.contents[which].votes += 1; + newPoll.vote = { _id: '', authorId: '', createdAt: new Date(), ...newVote }; - setPoll({ ...poll }); + setPoll(newPoll); + + post('votes/', newVote); } }; |