Commit 36ca8cfd66384e7720ae2ab6126938fc4a182fd9

Authored by Evandro Junior
1 parent 17f979a5

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 218 });
219 219 $body.off('click', '.vote-actions .like');
220 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 228 $.ajax({
222 229 type: 'post',
223 230 url: host + '/api/v1/articles/' + article.id + '/vote',
... ... @@ -226,6 +233,7 @@ define(['handlebars'], function(Handlebars){
226 233 private_token: private_token
227 234 }
228 235 }).done(function( /*data*/ ) {
  236 + addVotedProposal(article.id);
229 237 loadRandomProposal(topic_id, private_token);
230 238 });
231 239 e.preventDefault();
... ... @@ -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 578 if('onhashchange' in window){
549 579 window.onhashchange = locationHashChanged;
550 580 }else{
... ...