From c65aa75ba0b88721b32541c6d2cc2acb2a641bcb Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Fri, 3 Jul 2020 17:26:17 +0300
Subject: fix: place like immediately not awaiting response

---
 src/components/PollCard/PollCard.tsx | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

(limited to 'src/components/PollCard/PollCard.tsx')

diff --git a/src/components/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx
index 12f7198..6cdd777 100644
--- a/src/components/PollCard/PollCard.tsx
+++ b/src/components/PollCard/PollCard.tsx
@@ -62,16 +62,21 @@ const PollCard: React.FC<PropTypes> = ({ initialPoll }) => {
 
   const handleVote = (which: Which) => {
     if (vote) {
-      enqueueSnackbar('You have already voted', {
+      enqueueSnackbar('You have already voted in this poll', {
         variant: 'error'
       });
-      return;
-    }
-    post('votes/', { which, pollId: poll._id }).then(response => {
+    } 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');
-- 
cgit v1.2.3


From e4a641aca387370adf5bfbe06f3f9ea9ed22215d Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Fri, 3 Jul 2020 17:49:57 +0300
Subject: feat: notify unauthorized user trying to vote

---
 src/components/PollCard/PollCard.tsx | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

(limited to 'src/components/PollCard/PollCard.tsx')

diff --git a/src/components/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx
index 6cdd777..f5a5762 100644
--- a/src/components/PollCard/PollCard.tsx
+++ b/src/components/PollCard/PollCard.tsx
@@ -7,9 +7,11 @@ import {
 } 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;
@@ -58,10 +60,15 @@ const PollCard: React.FC<PropTypes> = ({ 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) {
+    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'
       });
-- 
cgit v1.2.3

