From 36ca8cfd66384e7720ae2ab6126938fc4a182fd9 Mon Sep 17 00:00:00 2001 From: Evandro Junior Date: Mon, 27 Apr 2015 11:49:33 -0300 Subject: [PATCH] Added code to make harder to vote twice from UI --- js/main.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+), 0 deletions(-) diff --git a/js/main.js b/js/main.js index ab05245..92a1a37 100644 --- a/js/main.js +++ b/js/main.js @@ -218,6 +218,13 @@ define(['handlebars'], function(Handlebars){ }); $body.off('click', '.vote-actions .like'); $body.on('click', '.vote-actions .like', function(e) { + //Helps to prevent more than one vote per proposal + if(hasProposalbeenVoted(article.id)){ + console.log("Proposta " + article.id + " já havia sido votada"); + loadRandomProposal(topic_id, private_token); + e.preventDefault(); + return; + } $.ajax({ type: 'post', url: host + '/api/v1/articles/' + article.id + '/vote', @@ -226,6 +233,7 @@ define(['handlebars'], function(Handlebars){ private_token: private_token } }).done(function( /*data*/ ) { + addVotedProposal(article.id); loadRandomProposal(topic_id, private_token); }); e.preventDefault(); @@ -545,6 +553,28 @@ define(['handlebars'], function(Handlebars){ } } + function addVotedProposal(id) { + var votedProposals; + if (typeof($.cookie("votedProposals")) == "undefined"){ + votedProposals = []; + } + else{ + votedProposals = JSON.parse($.cookie("votedProposals")); + } + if (votedProposals.indexOf(id)==-1){ + votedProposals.push(id); + } + $.cookie("votedProposals", JSON.stringify(votedProposals), {expires : 999 }) ; + } + + function hasProposalbeenVoted(id) { + if (typeof($.cookie("votedProposals")) == "undefined") { + return false; + } + votedProposals = JSON.parse($.cookie("votedProposals")); + return votedProposals.indexOf(id)!=-1; + } + if('onhashchange' in window){ window.onhashchange = locationHashChanged; }else{ -- libgit2 0.21.2