From 94323b65caa529ea0ef423ea0d1c22c4552cae39 Mon Sep 17 00:00:00 2001 From: Leonardo Merlin Date: Fri, 25 Sep 2015 14:14:03 -0300 Subject: [PATCH] Change proposal vote -> by captcha now --- src/app/components/article-service/article.service.js | 11 +++-------- src/app/components/auth/auth.service.js | 25 +++++++++++++++++++++++-- src/app/components/proposal-box/proposal-box.directive.js | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------- src/app/components/proposal-box/proposal-box.html | 22 +++++++++++----------- src/app/index.run.js | 1 + src/app/pages/programas/programa.controller.js | 43 +++++++++++-------------------------------- src/app/pages/programas/programa.html | 9 ++++++++- 7 files changed, 127 insertions(+), 98 deletions(-) diff --git a/src/app/components/article-service/article.service.js b/src/app/components/article-service/article.service.js index 9e7f585..a1e4b92 100644 --- a/src/app/components/article-service/article.service.js +++ b/src/app/components/article-service/article.service.js @@ -162,20 +162,15 @@ } } - function voteProposal (proposal_id, params, cbSuccess, cbError){ + function voteProposal (proposal_id, params){ var url = service.apiArticles + proposal_id + '/vote'; var paramsExtended = angular.extend({ - private_token: $rootScope.currentUser.private_token - // private_token: 'e2198fdbcc20409f082829b4b5c0848e' + private_token: $rootScope.temporaryToken.private_token }, params); var encodedParams = angular.element.param(paramsExtended); - UtilService.post(url, encodedParams).then(function(response){ - cbSuccess(response); - }).catch(function(error){ - cbError(error); - }); + return UtilService.post(url, encodedParams); } function getEvents (community_id, params) { diff --git a/src/app/components/auth/auth.service.js b/src/app/components/auth/auth.service.js index bcb3ffc..11c69ee 100644 --- a/src/app/components/auth/auth.service.js +++ b/src/app/components/auth/auth.service.js @@ -136,6 +136,8 @@ }, function(response) { $log.debug('AuthService.login [FAIL] response', response); $rootScope.$broadcast(AUTH_EVENTS.loginFailed); + + return $q.reject(response); }); } @@ -143,7 +145,17 @@ var url = PATH.host + '/api/v1/login-captcha'; var encodedData = angular.element.param(data); - return $http.post(url, encodedData); + return $http.post(url, encodedData).then(function(response){ + // SUCCESS + $log.debug('AuthService.loginCaptcha [SUCCESS] response', response); + + var temporaryToken = response.data.private_token; + Session.setTemporaryToken(temporaryToken); + $rootScope.temporaryToken = temporaryToken; + return temporaryToken; + }, function(response){ + return $q.reject(response.data); + }); } function logout () { @@ -206,7 +218,7 @@ service.create = function(data) { - $localStorage.currentUser = data; + $localStorage.currentUser = data.user; $log.debug('User session created.', $localStorage.currentUser); return $localStorage.currentUser; @@ -223,6 +235,15 @@ return $localStorage.currentUser; }; + service.setTemporaryToken = function (data) { + $localStorage.temporaryToken = data.private_token; + $log.debug('temporaryToken created:', $localStorage.temporaryToken); + }; + + service.getTemporaryToken = function () { + return $localStorage.temporaryToken; + }; + return service; } diff --git a/src/app/components/proposal-box/proposal-box.directive.js b/src/app/components/proposal-box/proposal-box.directive.js index 8e6e676..749b61e 100644 --- a/src/app/components/proposal-box/proposal-box.directive.js +++ b/src/app/components/proposal-box/proposal-box.directive.js @@ -9,7 +9,7 @@ function proposalBox() { /** @ngInject */ - function ProposalBoxController($scope, $rootScope, $state, $timeout, $interval, $window, VOTE_STATUS, VOTE_OPTIONS, AuthService, $log) { + function ProposalBoxController($scope, $rootScope, $state, $timeout, $interval, $window, VOTE_STATUS, VOTE_OPTIONS, AuthService, DialogaService, $log) { $log.debug('ProposalBoxController'); var vm = this; @@ -28,7 +28,7 @@ vm.addListeners(); } - ProposalBoxController.prototype.init = function () { + ProposalBoxController.prototype.init = function() { var vm = this; @@ -44,52 +44,54 @@ vm.voteProposalRedirectURI = 'state=programa&task=vote-proposal&slug=' + slug + '&proposal_id=' + proposal_id; }; - ProposalBoxController.prototype.addListeners = function () { + ProposalBoxController.prototype.addListeners = function() { var vm = this; - vm.$scope.$on('proposal-box:proposal-loaded', function(event, data){ - if(data.success){ + vm.$scope.$on('proposal-box:proposal-loaded', function(event, data) { + if (data.success) { vm.STATE = null; } - if(data.error){ + if (data.error) { vm.errorOnSkip = data.error; } }); - vm.$scope.$on('proposal-box:vote-response', function(event, data){ + vm.$scope.$on('proposal-box:vote-response', function(event, data) { vm.$log.debug('proposal-box:vote-response'); vm.$log.debug('event', event); vm.$log.debug('data', data); - if(data.success) { + if (data.success) { vm.STATE = vm.VOTE_STATUS.SUCCESS; } - if(data.error) { + if (data.error) { vm.STATE = vm.VOTE_STATUS.ERROR; } - vm.message = data.message; + if (data.code === 401) { + vm.message = 'Não autorizado.'; + } }); // Load captcha var stop = null; - stop = vm.$interval(function(){ + stop = vm.$interval(function() { var $el = angular.element('#serpro_captcha'); - if ($el && $el.length > 0 ){ + if ($el && $el.length > 0) { vm.$window.initCaptcha($el[0]); vm.$interval.cancel(stop); stop = undefined; - }else{ + }else { vm.$log.debug('captcha element not found.'); } }, 10); }; - ProposalBoxController.prototype.showContent = function (slug) { + ProposalBoxController.prototype.showContent = function(slug) { var vm = this; vm.$state.go('programa', { @@ -100,11 +102,13 @@ }); }; - ProposalBoxController.prototype.canVote = function () { - return false; + ProposalBoxController.prototype.canVote = function() { + var vm = this; + + return !!vm.$rootScope.temporaryToken; }; - ProposalBoxController.prototype.submitCaptcha = function ($event, captchaForm) { + ProposalBoxController.prototype.submitCaptcha = function($event, captchaForm) { var vm = this; var target = $event.target; @@ -119,52 +123,73 @@ // SUCCESS vm.$log.debug('register success.data', data); - // get captcha_token + // SEND VOTE + if (vm._oldVoteValue) { + // hide captcha form + vm.showCaptchaForm = false; + vm.vote(vm._oldVoteValue); + } + }, function(data) { // ERROR vm.$log.debug('register error.data', data); - vm.sendingCaptchaError = {code: data.status }; + vm.sendingCaptchaError = { + code: data.status, + message: data.message || ('Erro (' + data.status + '). Já estamos trabalhando para resolver o problema.
Por favor, tente novamente mais tarde') + }; - if(data.status === 404){ - vm.$log.error('The api service is out!?'); + if (angular.equals(vm.sendingCaptchaError.message, 'Internal captcha validation error')) { + vm.sendingCaptchaError.message = 'Erro interno ao tentar validar captcha.

Já estamos trabalhando para resolver o problema.
Por favor, tente novamente mais tarde.'; } - }, function(data){ + }, function(data) { // UPDATE vm.$log.debug('register update.data', data); - }).finally(function(){ + }).finally(function() { vm.sendingCaptcha = false; }); }; - ProposalBoxController.prototype.vote = function (value) { + ProposalBoxController.prototype.captchaTryAgain = function() { var vm = this; - if(vm.canVote()){ - vm.$scope.$emit('proposal-box:vote', { - OPTION: value, - proposal_id: vm.proposal.id - }); - vm.$log.debug('Sending vote', value); - }else{ - vm.$log.debug('You cannot vote.'); - vm.showCaptchaForm = true; - } + vm.showCaptchaForm = true; + vm.sendingCaptcha = false; + vm.sendingCaptchaError = false; + vm.message = null; + + // reload new captcha + var $el = angular.element('#serpro_captcha'); + vm.$window.reloadCaptcha($el[0]); + + // focus on input + angular.element('#captcha_text').val('').focus(); }; - ProposalBoxController.prototype.voteDown = function () { + ProposalBoxController.prototype.vote = function(value) { var vm = this; - vm.STATE = vm.VOTE_STATUS.LOADING; - vm.$scope.$emit('proposal-box:vote', { - OPTION: vm.VOTE_OPTIONS.DOWN, - proposal_id: vm.proposal.id - }); - vm.$log.debug('Sending vote'); + if (vm.canVote()) { + if (vm.doVote) { + var data = { + proposal_id: vm.proposal.id, + value: value + }; + vm.doVote(data); + }else { + vm.$log.error('No vote function to handler votes'); + } + }else { + vm.$log.debug('You cannot vote.'); + vm._oldVoteValue = value; + vm.showCaptchaForm = true; + + angular.element('#captcha_text').focus(); + } }; - ProposalBoxController.prototype.skip = function () { + ProposalBoxController.prototype.skip = function() { var vm = this; vm.errorOnSkip = false; @@ -176,7 +201,7 @@ vm.$log.debug('Sending vote'); }; - ProposalBoxController.prototype.getSocialUrl = function () { + ProposalBoxController.prototype.getSocialUrl = function() { var vm = this; return vm.$state.href('programa', { @@ -193,7 +218,8 @@ topic: '=', category: '=', showVote: '=', - focus: '@' + focus: '@', + doVote: '&' // @ -> Text binding / one-way binding // = -> Direct model binding / two-way binding // & -> Behaviour binding / Method binding diff --git a/src/app/components/proposal-box/proposal-box.html b/src/app/components/proposal-box/proposal-box.html index 0bb7db3..5df0f22 100644 --- a/src/app/components/proposal-box/proposal-box.html +++ b/src/app/components/proposal-box/proposal-box.html @@ -72,23 +72,23 @@
-
+

Enviando captcha...

-
- -
+
+