aboutsummaryrefslogtreecommitdiff
path: root/src/components/PollCard
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/PollCard')
-rw-r--r--src/components/PollCard/PollCard.tsx36
1 files changed, 24 insertions, 12 deletions
diff --git a/src/components/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx
index 2a23522..98ae001 100644
--- a/src/components/PollCard/PollCard.tsx
+++ b/src/components/PollCard/PollCard.tsx
@@ -6,10 +6,12 @@ import {
CardMedia
} from '@material-ui/core/';
import { Which, Poll } from 'which-types';
+import { useSnackbar } from 'notistack';
import PercentageBar from './PercentageBar';
import UserStrip from '../UserStrip/UserStrip';
import { post } from '../../requests';
+import { useAuth } from '../../hooks/useAuth';
interface PropTypes {
initialPoll: Poll;
@@ -24,14 +26,8 @@ const DATE_FORMAT = {
};
const useStyles = makeStyles(theme => ({
- root: {
- maxWidth: theme.spacing(75),
- height: 488,
- margin: '40px auto'
- },
images: {
- height: theme.spacing(50),
- width: 300
+ height: theme.spacing(50)
},
imagesBlock: {
display: 'flex'
@@ -57,15 +53,31 @@ const PollCard: React.FC<PropTypes> = ({ initialPoll }) => {
const [poll, setPoll] = useState<Poll>(initialPoll);
const classes = useStyles();
const { author, contents: { left, right }, vote } = poll;
+ const { enqueueSnackbar } = useSnackbar();
+ const { isAuthenticated } = useAuth();
const date: string = new Date(poll.createdAt).toLocaleString('default', DATE_FORMAT);
const handleVote = (which: Which) => {
- if (vote) return;
- post('votes/', { which, pollId: poll._id }).then(response => {
+ if (!isAuthenticated()) {
+ enqueueSnackbar('Unauthorized users can not vote in polls', {
+ variant: 'error'
+ });
+ } else if (vote) {
+ enqueueSnackbar('You have already voted in this poll', {
+ variant: 'error'
+ });
+ } else {
+ const newVote = ({ which, pollId: poll._id });
+ post('votes/', newVote);
poll.contents[which].votes += 1;
- poll.vote = response.data;
+ poll.vote = {
+ _id: '',
+ authorId: '',
+ createdAt: new Date(),
+ ...newVote
+ };
setPoll({ ...poll });
- });
+ }
};
const handleLeft = () => handleVote('left');
@@ -85,7 +97,7 @@ const PollCard: React.FC<PropTypes> = ({ initialPoll }) => {
const dominant: Which = left.votes >= right.votes ? 'left' : 'right';
return (
- <Card className={classes.root}>
+ <Card>
<UserStrip user={author} info={date} />
<div className={classes.imagesBlock}>
<CardActionArea onDoubleClick={handleLeft}>