From 1ea912ba0b74c0b47854502d0276ea14cbc98f65 Mon Sep 17 00:00:00 2001 From: Leonardo Merlin Date: Tue, 8 Sep 2015 18:33:33 -0300 Subject: [PATCH] Add initial internal redirect handler --- src/app/components/article-service/article.service.js | 25 +++++++++++++++++++++---- src/app/components/auth-user/auth-user.directive.js | 1 - src/app/components/auth/auth.service.js | 3 ++- src/app/components/event-list/event-list.directive.js | 23 ++++++++++++++++++++--- src/app/components/event-list/event-list.html | 10 ++++++---- src/app/index.route.js | 2 +- src/app/pages/article/article.html | 10 ++++++++-- src/app/pages/auth/auth.controller.js | 37 +++++++++++++++++++++++++++++++++---- 8 files changed, 91 insertions(+), 20 deletions(-) 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}} - (?) -
- Inscritos +
+ (?) +
+ Inscritos +
- diff --git a/src/app/index.route.js b/src/app/index.route.js index 26f1a1a..d5a1bfc 100644 --- a/src/app/index.route.js +++ b/src/app/index.route.js @@ -23,7 +23,7 @@ } }) .state('entrar', { - url: '/entrar', + url: '/entrar?redirect_uri', ncyBreadcrumb: {label: 'Entrar'}, views: { 'header': { templateUrl: 'app/pages/header/header.html' }, diff --git a/src/app/pages/article/article.html b/src/app/pages/article/article.html index 0628c74..1366403 100644 --- a/src/app/pages/article/article.html +++ b/src/app/pages/article/article.html @@ -26,9 +26,15 @@
-

{{::pageArticle.article.title}}

+
+

{{::pageArticle.article.title}}

+
-
+
+
+
+
+
diff --git a/src/app/pages/auth/auth.controller.js b/src/app/pages/auth/auth.controller.js index 0950220..18a3064 100644 --- a/src/app/pages/auth/auth.controller.js +++ b/src/app/pages/auth/auth.controller.js @@ -6,13 +6,13 @@ .controller('AuthPageController', AuthPageController); /** @ngInject */ - function AuthPageController($scope, $rootScope, AUTH_EVENTS, AuthService, DialogaService, Session, $log) { - $log.debug('AuthPageController'); - + function AuthPageController($scope, $rootScope, $location, $state, AUTH_EVENTS, AuthService, DialogaService, Session, $log) { var vm = this; - vm.$rootScope = $rootScope; vm.$scope = $scope; + vm.$rootScope = $rootScope; + vm.$location = $location; + vm.$state = $state; vm.AUTH_EVENTS = AUTH_EVENTS; vm.AuthService = AuthService; vm.DialogaService = DialogaService; @@ -21,6 +21,8 @@ vm.init(); vm.loadData(); + + vm.$log.debug('AuthPageController'); } AuthPageController.prototype.init = function() { @@ -32,6 +34,12 @@ vm.terms = null; vm.loadingTerms = null; + vm.search = vm.$location.search(); + var redirect = vm.search.redirect_uri || ''; + if(redirect && redirect.length > 0){ + vm.params = JSON.parse('{"' + decodeURI(redirect).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}'); + } + // attach events vm.currentUser = vm.Session.getCurrentUser(); @@ -78,6 +86,8 @@ // 'Cadastro efetuado com sucesso.' // 'Verifique seu email para confirmar o cadastro.' + // TODO: show messagens and redirect timeout + vm.redirectBack(); }, function(response){ vm.$log.debug('register error.response', response); @@ -92,9 +102,28 @@ vm.AuthService.login(credentials).then(function(user) { // handle view vm.$log.debug('user', user); + vm.redirectBack(); }, function() { // handle view }); }; + AuthPageController.prototype.redirectBack = function(){ + var vm = this; + + if(!vm.params){ + vm.$log.warn('No redirect params defined.'); + return; + } + var state = vm.params.state; + switch(state){ + case 'inicio': + vm.$state.go(state, { + event_id: vm.params.event_id, + task: vm.params.task + }); + break; + } + } + })(); -- libgit2 0.21.2