diff --git a/src/app/components/article-service/article.service.js b/src/app/components/article-service/article.service.js index 196553b..9e7f585 100644 --- a/src/app/components/article-service/article.service.js +++ b/src/app/components/article-service/article.service.js @@ -178,7 +178,7 @@ }); } - function getEvents (community_id, params, cbSuccess, cbError) { + function getEvents (community_id, params) { // Ex.: /api/v1/communities/19195/articles?categories_ids[]=' + cat_id + '&content_type=Event'; // Ex.: /api/v1/communities/' + community_id + '/articles?categories_ids[]=' + cat_id + '&content_type=Event'; @@ -188,11 +188,9 @@ 'content_type':'Event' }, params); - UtilService.get(url, {params: paramsExtended}).then(function(data){ - _pipeIsInThePast(data); - cbSuccess(data.articles); - }).catch(function(error){ - cbError(error); + return UtilService.get(url, {params: paramsExtended}).then(function(data){ + _pipeRemoveOldEvents(data); + return data; }); } @@ -210,21 +208,11 @@ // }); // } - function subscribeToEvent (event_id, params, cbSuccess, cbError) { + function subscribeToEvent (event_id) { + var url = service.apiArticles + event_id + '/follow'; + var encodedParams = 'private_token=' + $rootScope.currentUser.private_token; - if(!$rootScope.currentUser){ - cbError({message: 'Usuário não logado.'}); - - }else{ - var url = service.apiArticles + event_id + '/follow'; - var encodedParams = 'private_token=' + $rootScope.currentUser.private_token; - - UtilService.post(url, encodedParams).then(function(response){ - cbSuccess(response); - }).catch(function(error){ - cbError(error); - }); - } + return UtilService.post(url, encodedParams); } function searchTopics (params, cbSuccess, cbError) { @@ -282,14 +270,17 @@ }); } - function _pipeIsInThePast(data){ + function _pipeRemoveOldEvents(data){ if(!data.articles && data.article){ data.articles = [data.article]; + data.article = null; } + var now = (new Date()).getTime(); var eventDate = null; var events = data.articles; + var results = []; for (var i = events.length - 1; i >= 0; i--) { var event = events[i]; @@ -297,10 +288,16 @@ eventDate = new Date(event.end_date); } - if(eventDate.getTime() < now){ - event.isOld = true; + // if(eventDate.getTime() < now){ + // event.isOld = true; + // } + if(eventDate.getTime() >= now){ + results.push(event); } } + + data.articles = results; } + } })(); diff --git a/src/app/components/auth/auth.service.js b/src/app/components/auth/auth.service.js index aa42270..cf3a6b4 100644 --- a/src/app/components/auth/auth.service.js +++ b/src/app/components/auth/auth.service.js @@ -53,7 +53,7 @@ } function activate (code) { - var url = '/api/v1/activate'; + var url = PATH.host +'/api/v1/activate'; // var data = { // private_token: API.token, // activation_code: code @@ -77,7 +77,7 @@ } function changePassword (code, newPassword, newPasswordConfirmation){ - var url = '/api/v1/new_password'; + var url = PATH.host +'/api/v1/new_password'; // var data = { // code: code, // password: newPassword, @@ -103,7 +103,7 @@ } function forgotPassword (data){ - var url = 'http://hom.login.dialoga.gov.br/api/v1/forgot_password'; + var url = PATH.host +'/api/v1/forgot_password'; var encodedData = ([ 'value=' + data.login, 'captcha_text=' + data.captcha_text, @@ -129,8 +129,7 @@ } function login (credentials) { - var hostProd = 'http://login.dialoga.gov.br'; - var url = hostProd + '/api/v1/login'; + var url = PATH.host + '/api/v1/login'; var encodedData = 'login=' + credentials.username + '&password=' + credentials.password; return $http diff --git a/src/app/components/dialoga-service/dialoga.service.js b/src/app/components/dialoga-service/dialoga.service.js index e34ce1f..6449b3a 100644 --- a/src/app/components/dialoga-service/dialoga.service.js +++ b/src/app/components/dialoga-service/dialoga.service.js @@ -205,10 +205,10 @@ } } - function getEvents (params, cbSuccess, cbError) { + function getEvents (params) { var paramsExtended = angular.extend({}, params); - ArticleService.getEvents(API.communityId, paramsExtended, cbSuccess, cbError); + return ArticleService.getEvents(API.communityId, paramsExtended); } // TODO: implement diff --git a/src/app/components/event-list/event-list.directive.js b/src/app/components/event-list/event-list.directive.js index bb666ea..6a863ae 100644 --- a/src/app/components/event-list/event-list.directive.js +++ b/src/app/components/event-list/event-list.directive.js @@ -23,48 +23,62 @@ // vm.attachListeners(); } - EventListController.prototype.init = function () { + EventListController.prototype.init = function() { var vm = this; - if(!vm.events){ + if (!vm.events) { throw { name: 'NotDefined', message: 'The attribute "events" is undefined.'}; } - if(!vm.isCollapsed){ + if (!vm.isCollapsed) { vm.isCollapsed = true; } }; - EventListController.prototype.toggleView = function () { + EventListController.prototype.toggleView = function() { var vm = this; vm.isCollapsed = !vm.isCollapsed; }; - EventListController.prototype.subscribe = function (event) { + EventListController.prototype.subscribe = function(event) { var vm = this; - if(event.isOld){ - vm.$log.debug('Event already happened. Abort.'); - return; - } - var event_id = event.id; - vm.$log.debug('event_id', event_id); - if(!vm.$rootScope.currentUser){ + // must be authenticated + if (!vm.$rootScope.currentUser) { vm.$log.info('User is not logged in. Redirect to Auth page.'); - vm.$state.go('entrar',{ + vm.$state.go('entrar', { redirect_uri: 'state=inicio&task=subscribe&event_id=' + event_id - },{ + }, { location: true }); - }else{ - vm.ArticleService.subscribeToEvent(event_id, {}, function(response){ - vm.$log.debug('response', response); - }, function(error){ - vm.$log.debug('error', error); - }); + + return; } + + // do the subscription + event._loading = true; + vm.ArticleService.subscribeToEvent(event_id).then(function (data) { + vm.$log.debug('success', data); + + if(data.success === true){ + // subscribed with success + event.already_follow = true; + } + + if(data.success === false && data.already_follow === true){ + // already subscribed + event.already_follow = true; + } + }, function (data) { + vm.$log.debug('error', data); + }, function (data){ + vm.$log.debug('update', data); + }).finally(function(data){ + vm.$log.debug('finally', data); + event._loading = false; + }); }; var directive = { diff --git a/src/app/components/event-list/event-list.html b/src/app/components/event-list/event-list.html index 3e79272..118c454 100644 --- a/src/app/components/event-list/event-list.html +++ b/src/app/components/event-list/event-list.html @@ -40,11 +40,8 @@