Commit 36ca8cfd66384e7720ae2ab6126938fc4a182fd9
1 parent
17f979a5
Exists in
master
and in
10 other branches
Added code to make harder to vote twice from UI
Showing
1 changed file
with
30 additions
and
0 deletions
Show diff stats
js/main.js
@@ -218,6 +218,13 @@ define(['handlebars'], function(Handlebars){ | @@ -218,6 +218,13 @@ define(['handlebars'], function(Handlebars){ | ||
218 | }); | 218 | }); |
219 | $body.off('click', '.vote-actions .like'); | 219 | $body.off('click', '.vote-actions .like'); |
220 | $body.on('click', '.vote-actions .like', function(e) { | 220 | $body.on('click', '.vote-actions .like', function(e) { |
221 | + //Helps to prevent more than one vote per proposal | ||
222 | + if(hasProposalbeenVoted(article.id)){ | ||
223 | + console.log("Proposta " + article.id + " já havia sido votada"); | ||
224 | + loadRandomProposal(topic_id, private_token); | ||
225 | + e.preventDefault(); | ||
226 | + return; | ||
227 | + } | ||
221 | $.ajax({ | 228 | $.ajax({ |
222 | type: 'post', | 229 | type: 'post', |
223 | url: host + '/api/v1/articles/' + article.id + '/vote', | 230 | url: host + '/api/v1/articles/' + article.id + '/vote', |
@@ -226,6 +233,7 @@ define(['handlebars'], function(Handlebars){ | @@ -226,6 +233,7 @@ define(['handlebars'], function(Handlebars){ | ||
226 | private_token: private_token | 233 | private_token: private_token |
227 | } | 234 | } |
228 | }).done(function( /*data*/ ) { | 235 | }).done(function( /*data*/ ) { |
236 | + addVotedProposal(article.id); | ||
229 | loadRandomProposal(topic_id, private_token); | 237 | loadRandomProposal(topic_id, private_token); |
230 | }); | 238 | }); |
231 | e.preventDefault(); | 239 | e.preventDefault(); |
@@ -545,6 +553,28 @@ define(['handlebars'], function(Handlebars){ | @@ -545,6 +553,28 @@ define(['handlebars'], function(Handlebars){ | ||
545 | } | 553 | } |
546 | } | 554 | } |
547 | 555 | ||
556 | + function addVotedProposal(id) { | ||
557 | + var votedProposals; | ||
558 | + if (typeof($.cookie("votedProposals")) == "undefined"){ | ||
559 | + votedProposals = []; | ||
560 | + } | ||
561 | + else{ | ||
562 | + votedProposals = JSON.parse($.cookie("votedProposals")); | ||
563 | + } | ||
564 | + if (votedProposals.indexOf(id)==-1){ | ||
565 | + votedProposals.push(id); | ||
566 | + } | ||
567 | + $.cookie("votedProposals", JSON.stringify(votedProposals), {expires : 999 }) ; | ||
568 | + } | ||
569 | + | ||
570 | + function hasProposalbeenVoted(id) { | ||
571 | + if (typeof($.cookie("votedProposals")) == "undefined") { | ||
572 | + return false; | ||
573 | + } | ||
574 | + votedProposals = JSON.parse($.cookie("votedProposals")); | ||
575 | + return votedProposals.indexOf(id)!=-1; | ||
576 | + } | ||
577 | + | ||
548 | if('onhashchange' in window){ | 578 | if('onhashchange' in window){ |
549 | window.onhashchange = locationHashChanged; | 579 | window.onhashchange = locationHashChanged; |
550 | }else{ | 580 | }else{ |