diff --git a/src/app/components/article-service/article.service.js b/src/app/components/article-service/article.service.js index 4c4e44b..5f07308 100644 --- a/src/app/components/article-service/article.service.js +++ b/src/app/components/article-service/article.service.js @@ -156,19 +156,36 @@ }); } - function subscribeToEvent (event_id, params, cbSuccess, cbError) { - var url = service.apiArticles + event_id + '/follow'; + function getSubscribers (event_id, params, cbSuccess, cbError) { + var url = service.apiArticles + event_id + '/followers?_=' + new Date().getTime(); var paramsExtended = angular.extend({ - private_token: API.token + // 'fields[]': ['id', 'slug', 'title', 'abstract', 'body', 'categories', 'created_at', 'start_date', 'end_date', 'hits'], + 'content_type':'Event' }, params); - UtilService.post(url, {params: paramsExtended}).then(function(data){ + UtilService.get(url, {params: paramsExtended}).then(function(data){ cbSuccess(data.articles); }).catch(function(error){ cbError(error); }); } + function subscribeToEvent (event_id, params, cbSuccess, cbError) { + + if(!$rootScope.currentUser){ + cbError({message: 'Usuário não logado.'}); + } + + 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); + }); + } + function searchTopics (params, cbSuccess, cbError) { // Ex.: /api/v1/search/article?type=ProposalsDiscussionPlugin::Topic&query=cisternas var url = '/api/v1/search/article'; diff --git a/src/app/components/auth-user/auth-user.directive.js b/src/app/components/auth-user/auth-user.directive.js index 42d1fa9..b6b7ebc 100644 --- a/src/app/components/auth-user/auth-user.directive.js +++ b/src/app/components/auth-user/auth-user.directive.js @@ -42,7 +42,6 @@ AuthUserController.prototype.onClickLogout = function (){ var vm = this; - // TODO: emit event? vm.AuthService.logout(); }; diff --git a/src/app/components/auth/auth.service.js b/src/app/components/auth/auth.service.js index 3a3ccd9..1c32e38 100644 --- a/src/app/components/auth/auth.service.js +++ b/src/app/components/auth/auth.service.js @@ -130,6 +130,7 @@ $log.debug('AuthService.login [SUCCESS] response', response); var currentUser = Session.create(response.data); + $rootScope.currentUser = currentUser; $rootScope.$broadcast(AUTH_EVENTS.loginSuccess, currentUser); return currentUser; @@ -142,7 +143,7 @@ function logout () { Session.destroy(); - + $rootScope.currentUser = null; $rootScope.$broadcast(AUTH_EVENTS.logoutSuccess); } diff --git a/src/app/components/event-list/event-list.directive.js b/src/app/components/event-list/event-list.directive.js index 907ab2b..ba73068 100644 --- a/src/app/components/event-list/event-list.directive.js +++ b/src/app/components/event-list/event-list.directive.js @@ -8,11 +8,12 @@ /** @ngInject */ function eventList() { /** @ngInject */ - function EventListController($scope, $rootScope, $state, $log) { + function EventListController(ArticleService, $scope, $rootScope, $state, $log) { $log.debug('EventListController'); var vm = this; + vm.ArticleService = ArticleService; vm.$scope = $scope; vm.$rootScope = $rootScope; vm.$state = $state; @@ -39,10 +40,26 @@ vm.isCollapsed = !vm.isCollapsed; }; - EventListController.prototype.subscribe = function (data) { + EventListController.prototype.subscribe = function (event_id) { var vm = this; - vm.$log.debug('data', data); + vm.$log.debug('event_id', event_id); + + if(!vm.$rootScope.currentUser){ + vm.$log.warn('User is not logged in. Redirect to Auth page.'); + 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); + }) + } + }; var directive = { diff --git a/src/app/components/event-list/event-list.html b/src/app/components/event-list/event-list.html index 15e75dd..3549f47 100644 --- a/src/app/components/event-list/event-list.html +++ b/src/app/components/event-list/event-list.html @@ -59,12 +59,14 @@ {{::event.categories[0].name}}