Commit 94323b65caa529ea0ef423ea0d1c22c4552cae39

Authored by Leonardo Merlin
1 parent a9de7b59

Change proposal vote -> by captcha now

src/app/components/article-service/article.service.js
... ... @@ -162,20 +162,15 @@
162 162 }
163 163 }
164 164  
165   - function voteProposal (proposal_id, params, cbSuccess, cbError){
  165 + function voteProposal (proposal_id, params){
166 166 var url = service.apiArticles + proposal_id + '/vote';
167 167 var paramsExtended = angular.extend({
168   - private_token: $rootScope.currentUser.private_token
169   - // private_token: 'e2198fdbcc20409f082829b4b5c0848e'
  168 + private_token: $rootScope.temporaryToken.private_token
170 169 }, params);
171 170  
172 171 var encodedParams = angular.element.param(paramsExtended);
173 172  
174   - UtilService.post(url, encodedParams).then(function(response){
175   - cbSuccess(response);
176   - }).catch(function(error){
177   - cbError(error);
178   - });
  173 + return UtilService.post(url, encodedParams);
179 174 }
180 175  
181 176 function getEvents (community_id, params) {
... ...
src/app/components/auth/auth.service.js
... ... @@ -136,6 +136,8 @@
136 136 }, function(response) {
137 137 $log.debug('AuthService.login [FAIL] response', response);
138 138 $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
  139 +
  140 + return $q.reject(response);
139 141 });
140 142 }
141 143  
... ... @@ -143,7 +145,17 @@
143 145 var url = PATH.host + '/api/v1/login-captcha';
144 146 var encodedData = angular.element.param(data);
145 147  
146   - return $http.post(url, encodedData);
  148 + return $http.post(url, encodedData).then(function(response){
  149 + // SUCCESS
  150 + $log.debug('AuthService.loginCaptcha [SUCCESS] response', response);
  151 +
  152 + var temporaryToken = response.data.private_token;
  153 + Session.setTemporaryToken(temporaryToken);
  154 + $rootScope.temporaryToken = temporaryToken;
  155 + return temporaryToken;
  156 + }, function(response){
  157 + return $q.reject(response.data);
  158 + });
147 159 }
148 160  
149 161 function logout () {
... ... @@ -206,7 +218,7 @@
206 218  
207 219 service.create = function(data) {
208 220  
209   - $localStorage.currentUser = data;
  221 + $localStorage.currentUser = data.user;
210 222 $log.debug('User session created.', $localStorage.currentUser);
211 223  
212 224 return $localStorage.currentUser;
... ... @@ -223,6 +235,15 @@
223 235 return $localStorage.currentUser;
224 236 };
225 237  
  238 + service.setTemporaryToken = function (data) {
  239 + $localStorage.temporaryToken = data.private_token;
  240 + $log.debug('temporaryToken created:', $localStorage.temporaryToken);
  241 + };
  242 +
  243 + service.getTemporaryToken = function () {
  244 + return $localStorage.temporaryToken;
  245 + };
  246 +
226 247 return service;
227 248 }
228 249  
... ...
src/app/components/proposal-box/proposal-box.directive.js
... ... @@ -9,7 +9,7 @@
9 9 function proposalBox() {
10 10  
11 11 /** @ngInject */
12   - function ProposalBoxController($scope, $rootScope, $state, $timeout, $interval, $window, VOTE_STATUS, VOTE_OPTIONS, AuthService, $log) {
  12 + function ProposalBoxController($scope, $rootScope, $state, $timeout, $interval, $window, VOTE_STATUS, VOTE_OPTIONS, AuthService, DialogaService, $log) {
13 13 $log.debug('ProposalBoxController');
14 14  
15 15 var vm = this;
... ... @@ -28,7 +28,7 @@
28 28 vm.addListeners();
29 29 }
30 30  
31   - ProposalBoxController.prototype.init = function () {
  31 + ProposalBoxController.prototype.init = function() {
32 32  
33 33 var vm = this;
34 34  
... ... @@ -44,52 +44,54 @@
44 44 vm.voteProposalRedirectURI = 'state=programa&task=vote-proposal&slug=' + slug + '&proposal_id=' + proposal_id;
45 45 };
46 46  
47   - ProposalBoxController.prototype.addListeners = function () {
  47 + ProposalBoxController.prototype.addListeners = function() {
48 48 var vm = this;
49 49  
50   - vm.$scope.$on('proposal-box:proposal-loaded', function(event, data){
51   - if(data.success){
  50 + vm.$scope.$on('proposal-box:proposal-loaded', function(event, data) {
  51 + if (data.success) {
52 52 vm.STATE = null;
53 53 }
54 54  
55   - if(data.error){
  55 + if (data.error) {
56 56 vm.errorOnSkip = data.error;
57 57 }
58 58 });
59 59  
60   - vm.$scope.$on('proposal-box:vote-response', function(event, data){
  60 + vm.$scope.$on('proposal-box:vote-response', function(event, data) {
61 61 vm.$log.debug('proposal-box:vote-response');
62 62 vm.$log.debug('event', event);
63 63 vm.$log.debug('data', data);
64 64  
65   - if(data.success) {
  65 + if (data.success) {
66 66 vm.STATE = vm.VOTE_STATUS.SUCCESS;
67 67 }
68 68  
69   - if(data.error) {
  69 + if (data.error) {
70 70 vm.STATE = vm.VOTE_STATUS.ERROR;
71 71 }
72 72  
73   - vm.message = data.message;
  73 + if (data.code === 401) {
  74 + vm.message = 'Não autorizado.';
  75 + }
74 76 });
75 77  
76 78 // Load captcha
77 79 var stop = null;
78   - stop = vm.$interval(function(){
  80 + stop = vm.$interval(function() {
79 81 var $el = angular.element('#serpro_captcha');
80 82  
81   - if ($el && $el.length > 0 ){
  83 + if ($el && $el.length > 0) {
82 84 vm.$window.initCaptcha($el[0]);
83 85 vm.$interval.cancel(stop);
84 86 stop = undefined;
85   - }else{
  87 + }else {
86 88 vm.$log.debug('captcha element not found.');
87 89 }
88 90  
89 91 }, 10);
90 92 };
91 93  
92   - ProposalBoxController.prototype.showContent = function (slug) {
  94 + ProposalBoxController.prototype.showContent = function(slug) {
93 95 var vm = this;
94 96  
95 97 vm.$state.go('programa', {
... ... @@ -100,11 +102,13 @@
100 102 });
101 103 };
102 104  
103   - ProposalBoxController.prototype.canVote = function () {
104   - return false;
  105 + ProposalBoxController.prototype.canVote = function() {
  106 + var vm = this;
  107 +
  108 + return !!vm.$rootScope.temporaryToken;
105 109 };
106 110  
107   - ProposalBoxController.prototype.submitCaptcha = function ($event, captchaForm) {
  111 + ProposalBoxController.prototype.submitCaptcha = function($event, captchaForm) {
108 112 var vm = this;
109 113  
110 114 var target = $event.target;
... ... @@ -119,52 +123,73 @@
119 123 // SUCCESS
120 124 vm.$log.debug('register success.data', data);
121 125  
122   - // get captcha_token
  126 + // SEND VOTE
  127 + if (vm._oldVoteValue) {
  128 + // hide captcha form
  129 + vm.showCaptchaForm = false;
  130 + vm.vote(vm._oldVoteValue);
  131 + }
  132 +
123 133 }, function(data) {
124 134 // ERROR
125 135 vm.$log.debug('register error.data', data);
126 136  
127   - vm.sendingCaptchaError = {code: data.status };
  137 + vm.sendingCaptchaError = {
  138 + code: data.status,
  139 + message: data.message || ('Erro (' + data.status + '). Já estamos trabalhando para resolver o problema.<br/>Por favor, tente novamente mais tarde')
  140 + };
128 141  
129   - if(data.status === 404){
130   - vm.$log.error('The api service is out!?');
  142 + if (angular.equals(vm.sendingCaptchaError.message, 'Internal captcha validation error')) {
  143 + vm.sendingCaptchaError.message = 'Erro interno ao tentar validar captcha.<br/><br/>Já estamos trabalhando para resolver o problema.<br/>Por favor, tente novamente mais tarde.';
131 144 }
132 145  
133   - }, function(data){
  146 + }, function(data) {
134 147 // UPDATE
135 148 vm.$log.debug('register update.data', data);
136   - }).finally(function(){
  149 + }).finally(function() {
137 150 vm.sendingCaptcha = false;
138 151 });
139 152 };
140 153  
141   - ProposalBoxController.prototype.vote = function (value) {
  154 + ProposalBoxController.prototype.captchaTryAgain = function() {
142 155 var vm = this;
143 156  
144   - if(vm.canVote()){
145   - vm.$scope.$emit('proposal-box:vote', {
146   - OPTION: value,
147   - proposal_id: vm.proposal.id
148   - });
149   - vm.$log.debug('Sending vote', value);
150   - }else{
151   - vm.$log.debug('You cannot vote.');
152   - vm.showCaptchaForm = true;
153   - }
  157 + vm.showCaptchaForm = true;
  158 + vm.sendingCaptcha = false;
  159 + vm.sendingCaptchaError = false;
  160 + vm.message = null;
  161 +
  162 + // reload new captcha
  163 + var $el = angular.element('#serpro_captcha');
  164 + vm.$window.reloadCaptcha($el[0]);
  165 +
  166 + // focus on input
  167 + angular.element('#captcha_text').val('').focus();
154 168 };
155 169  
156   - ProposalBoxController.prototype.voteDown = function () {
  170 + ProposalBoxController.prototype.vote = function(value) {
157 171 var vm = this;
158 172  
159   - vm.STATE = vm.VOTE_STATUS.LOADING;
160   - vm.$scope.$emit('proposal-box:vote', {
161   - OPTION: vm.VOTE_OPTIONS.DOWN,
162   - proposal_id: vm.proposal.id
163   - });
164   - vm.$log.debug('Sending vote');
  173 + if (vm.canVote()) {
  174 + if (vm.doVote) {
  175 + var data = {
  176 + proposal_id: vm.proposal.id,
  177 + value: value
  178 + };
  179 + vm.doVote(data);
  180 + }else {
  181 + vm.$log.error('No vote function to handler votes');
  182 + }
  183 + }else {
  184 + vm.$log.debug('You cannot vote.');
  185 + vm._oldVoteValue = value;
  186 + vm.showCaptchaForm = true;
  187 +
  188 + angular.element('#captcha_text').focus();
  189 + }
165 190 };
166 191  
167   - ProposalBoxController.prototype.skip = function () {
  192 + ProposalBoxController.prototype.skip = function() {
168 193 var vm = this;
169 194  
170 195 vm.errorOnSkip = false;
... ... @@ -176,7 +201,7 @@
176 201 vm.$log.debug('Sending vote');
177 202 };
178 203  
179   - ProposalBoxController.prototype.getSocialUrl = function () {
  204 + ProposalBoxController.prototype.getSocialUrl = function() {
180 205 var vm = this;
181 206  
182 207 return vm.$state.href('programa', {
... ... @@ -193,7 +218,8 @@
193 218 topic: '=',
194 219 category: '=',
195 220 showVote: '=',
196   - focus: '@'
  221 + focus: '@',
  222 + doVote: '&'
197 223 // @ -> Text binding / one-way binding
198 224 // = -> Direct model binding / two-way binding
199 225 // & -> Behaviour binding / Method binding
... ...
src/app/components/proposal-box/proposal-box.html
... ... @@ -72,23 +72,23 @@
72 72 <div class="col-sm-12 col-height col-middle">
73 73 <div class="inside inside-full-height">
74 74 <div class="content text-center">
75   - <div ng-if="vm.sendingCaptcha">
  75 + <div ng-show="vm.sendingCaptcha">
76 76 <p>Enviando captcha...</p>
77 77 </div>
78   - <div ng-if="!vm.sendingCaptcha">
79   -
80   - <div class="row" class="feedback-message" ng-if="vm.sendingCaptchaError">
  78 + <div ng-hide="vm.sendingCaptcha">
  79 + <div class="row feedback-message" ng-show="vm.sendingCaptchaError">
81 80 <div class="col-sm-12">
82 81 <div class="feedback--title alert alert-danger">Erro!</div>
83   - <div class="feedback--message" ng-if="vm.sendingCaptchaError.code === 404">
84   - <p>
85   - Houve uma falha. Já estamos trabalhando para resolver o problema.
86   - <br/> Por favor, tente novamente mais tarde
87   - </p>
  82 + <div class="feedback--message" ng-if="!vm.message">
  83 + <p ng-bind-html="vm.sendingCaptchaError.message"></p>
  84 + </div>
  85 + <div>
  86 + <button type="reset" class="btn btn-link" ng-click="vm.captchaTryAgain()">Tentar novamente</button>
  87 + <button type="reset" class="btn btn-link" ng-click="vm.showCaptchaForm = false">Voltar</button>
88 88 </div>
89 89 </div>
90 90 </div>
91   - <div ng-if="!vm.sendingCaptchaError">
  91 + <div ng-hide="vm.sendingCaptchaError">
92 92 <form name="captchaForm" ng-submit="vm.submitCaptcha($event, captchaForm)">
93 93 <div class="form-group">
94 94 <div id="serpro_captcha" class="captcha"></div>
... ... @@ -102,7 +102,7 @@
102 102 <button type="submit" class="btn btn-lg btn-block btn-submit">Enviar</button>
103 103 </div>
104 104 <div class="form-group">
105   - <button class="btn btn-link" ng-click="vm.showCaptchaForm = false">Voltar</button>
  105 + <button type="reset" class="btn btn-link" ng-click="vm.showCaptchaForm = false">Voltar</button>
106 106 </div>
107 107 </form>
108 108 </div>
... ...
src/app/index.run.js
... ... @@ -41,6 +41,7 @@
41 41 });
42 42  
43 43 $rootScope.currentUser = $localStorage.currentUser;
  44 + $rootScope.temporaryToken = $localStorage.temporaryToken;
44 45  
45 46 $log.debug('[RUN] Auth end.');
46 47 }
... ...
src/app/pages/programas/programa.controller.js
... ... @@ -142,24 +142,6 @@
142 142 vm.proposalStatus = vm.PROPOSAL_STATUS.ERROR;
143 143 });
144 144 });
145   -
146   - vm.$scope.$on('proposal-box:vote', function(event, params) {
147   - // vm.$log.debug('event', event);
148   - // vm.$log.debug('params', params);
149   - var proposal_id = params.proposal_id;
150   - var OPTION = params.OPTION;
151   -
152   - switch (OPTION){
153   - case vm.VOTE_OPTIONS.UP:
154   - case vm.VOTE_OPTIONS.DOWN:
155   - case vm.VOTE_OPTIONS.SKIP:
156   - vm.vote(proposal_id, OPTION);
157   - break;
158   - default:
159   - vm.$log.error('Vote option not handled:', OPTION);
160   - break;
161   - }
162   - });
163 145 };
164 146  
165 147 ProgramaPageController.prototype.loadProposalById = function(proposal_id) {
... ... @@ -219,31 +201,28 @@
219 201 return;
220 202 }
221 203  
222   - if (!vm.$rootScope.currentUser) {
223   - // vm.$state.go('entrar', {
224   - // redirect_uri: vm.sendProposalRedirectURI,
225   - // message: 'Você precisa estar logado para votar em uma proposta.'
226   - // }, {
227   - // location: true
228   - // });
  204 + if (!vm.$rootScope.temporaryToken) {
  205 + vm.$log.debug('"temporaryToken" not defined. Abort.');
229 206 return;
230 207 }
231 208  
232 209 vm.DialogaService.voteProposal(proposal_id, {
233 210 value: value
234   - }, function(response) {
235   - vm.$log.debug('response', response);
  211 + }).then(function(response) {
  212 + vm.$log.debug('voteProposal response', response);
236 213  
237 214 response.success = true;
238 215 vm.$scope.$broadcast('proposal-box:vote-response', response);
239   - }, function(error) {
240   - vm.$log.error('error', error);
  216 + }, function(response) {
  217 + vm.$log.debug('voteProposal error', response);
  218 +
  219 + response.error = true;
  220 + vm.$scope.$broadcast('proposal-box:vote-response', response);
  221 + }).finally(function(response){
  222 + vm.$log.debug('voteProposal finally', response);
241 223  
242   - error.error = true;
243   - vm.$scope.$broadcast('proposal-box:vote-response', error);
244 224 });
245 225 };
246   - ProgramaPageController.prototype.voteHasBeenComputed = function() {};
247 226  
248 227 ProgramaPageController.prototype.showProposalsList = function() {
249 228 var vm = this;
... ...
src/app/pages/programas/programa.html
... ... @@ -90,7 +90,14 @@
90 90 <div>
91 91 <div class="col-xs-12" ng-if="!pagePrograma.loadingProposalBox && pagePrograma.randomProposal" ng-class="{'focused-proposal': !!pagePrograma.search.proposal_id}">
92 92 <h3 class="color-theme-fg">Apoie outras propostas</h3>
93   - <proposal-box proposal="pagePrograma.randomProposal" topic="pagePrograma.article" category="pagePrograma.category" show-vote="true" focus="{{pagePrograma.search.proposal_id}}" ></proposal-box>
  93 + <proposal-box
  94 + proposal="pagePrograma.randomProposal"
  95 + topic="pagePrograma.article"
  96 + category="pagePrograma.category"
  97 + show-vote="true"
  98 + focus="{{pagePrograma.search.proposal_id}}"
  99 + do-vote="pagePrograma.vote(proposal_id, value)"
  100 + ></proposal-box>
94 101 </div>
95 102  
96 103 <!-- Loading Proposal Box -->
... ...