From 5c16da40f2ed9af570a6c18e968d40bdc6ebd111 Mon Sep 17 00:00:00 2001 From: Abner Oliveira Date: Mon, 13 Jun 2016 14:54:04 -0300 Subject: [PATCH] starting to resolve profile and articles on ui-router resolve functions --- src/app/article/content-viewer/content-viewer.component.ts | 24 ++++++++++++------------ src/app/main/main.component.ts | 11 +++++++++-- src/app/profile/profile-home.component.ts | 28 ++++++++++++---------------- src/app/profile/profile.component.ts | 64 +++++++++++++++++++++++++++++++++++++++++++++++++--------------- src/lib/ng-noosfero-api/http/profile.service.ts | 39 +++++++++++++++++++++------------------ 5 files changed, 103 insertions(+), 63 deletions(-) diff --git a/src/app/article/content-viewer/content-viewer.component.ts b/src/app/article/content-viewer/content-viewer.component.ts index 233bfd5..ef45ba8 100644 --- a/src/app/article/content-viewer/content-viewer.component.ts +++ b/src/app/article/content-viewer/content-viewer.component.ts @@ -14,26 +14,26 @@ import {ProfileService} from "../../../lib/ng-noosfero-api/http/profile.service" provide('profileService', { useClass: ProfileService }) ] }) -@Inject(ArticleService, ProfileService, "$log", "$stateParams") +@Inject(ArticleService, ProfileService, "$log", "$stateParams", "currentProfile") export class ContentViewerComponent { @Input() article: noosfero.Article = null; - @Input() - profile: noosfero.Profile = null; - - constructor(private articleService: ArticleService, private profileService: ProfileService, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { + constructor( + private articleService: ArticleService, + private profileService: ProfileService, + private $log: ng.ILogService, + private $stateParams: angular.ui.IStateParamsService, + public profile: noosfero.Profile) { this.activate(); } activate() { - this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { - this.profile = profile; - return this.articleService.getArticleByProfileAndPath(this.profile, this.$stateParams["page"]); - }).then((result: noosfero.RestResult) => { - this.article = result.data; - this.articleService.setCurrent(this.article); - }); + this.articleService.getArticleByProfileAndPath(this.profile, this.$stateParams["page"]) + .then((result: noosfero.RestResult) => { + this.article = result.data; + this.articleService.setCurrent(this.article); + }); } } diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index 2637b47..871c84d 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -29,6 +29,7 @@ import {DateFormat} from "../shared/pipes/date-format.filter"; import {AuthService} from "../login/auth.service"; import {SessionService} from "../login/session.service"; import {EnvironmentService} from "./../../lib/ng-noosfero-api/http/environment.service"; +import {ProfileService} from './../../lib/ng-noosfero-api/http/profile.service'; import {NotificationService} from "../shared/services/notification.service"; import {BodyStateClassesService} from "./../layout/services/body-state-classes.service"; @@ -118,10 +119,10 @@ export class EnvironmentContent { abstract: true, name: 'main', resolve: { - currentUser: function(AuthService: AuthService) { + currentUser: function (AuthService: AuthService) { return AuthService.loginFromCookie(); }, - currentEnvironment: function(EnvironmentService: EnvironmentService) { + currentEnvironment: function (EnvironmentService: EnvironmentService) { return EnvironmentService.get(); } } @@ -149,6 +150,12 @@ export class EnvironmentContent { templateUrl: "app/profile/profile.html", controller: ProfileComponent, controllerAs: "vm" + }, + }, + resolve: { + currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) { + console.log('$stateParams', $stateParams); + return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]); } } } diff --git a/src/app/profile/profile-home.component.ts b/src/app/profile/profile-home.component.ts index 3616664..3ea2658 100644 --- a/src/app/profile/profile-home.component.ts +++ b/src/app/profile/profile-home.component.ts @@ -7,23 +7,19 @@ import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; template: "
", providers: [provide('profileService', { useClass: ProfileService })] }) -@Inject(ProfileService, "$state") +@Inject(ProfileService, "$state", "currentProfile") export class ProfileHomeComponent { - profile: noosfero.Profile; - - constructor(profileService: ProfileService, $state: ng.ui.IStateService) { - profileService.getCurrentProfile().then((profile: noosfero.Profile) => { - this.profile = profile; - return profileService.getHomePage(this.profile.id, { fields: 'path' }); - }).then((response: restangular.IResponse) => { - if (response.data.article) { - this.profile.homepage = response.data.article.path; - $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); - } else { - this.profile.homepage = null; - $state.transitionTo('main.profile.info', { profile: this.profile.identifier }, { location: false }); - } - }); + constructor(profileService: ProfileService, $state: ng.ui.IStateService, public profile: noosfero.Profile) { + profileService.getHomePage(this.profile.id, { fields: 'path' }) + .then((response: restangular.IResponse) => { + if (response.data.article) { + this.profile.homepage = response.data.article.path; + $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); + } else { + this.profile.homepage = null; + $state.transitionTo('main.profile.info', { profile: this.profile.identifier }, { location: false }); + } + }); } } diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index ffabfb0..c54a592 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -1,4 +1,4 @@ -import {StateConfig, Component, Inject, provide} from 'ng-forward'; +import {StateConfig, Resolve, Component, Inject, provide} from 'ng-forward'; import {ProfileInfoComponent} from './info/profile-info.component'; import {ProfileHomeComponent} from './profile-home.component'; import {BasicEditorComponent} from '../article/cms/basic-editor/basic-editor.component'; @@ -10,7 +10,7 @@ import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; import {NotificationService} from "../shared/services/notification.service"; import {MyProfileComponent} from "./myprofile.component"; import {ProfileActionsComponent} from "./profile-actions.component"; - +import {ArticleService} from './../../lib/ng-noosfero-api/http/article.service'; /** * @ngdoc controller * @name profile.Profile @@ -43,6 +43,11 @@ import {ProfileActionsComponent} from "./profile-actions.component"; controller: ProfileActionsComponent, controllerAs: "vm" } + }, + resolve: { + currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) { + return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]); + } } }, { @@ -55,6 +60,11 @@ import {ProfileActionsComponent} from "./profile-actions.component"; controller: ProfileActionsComponent, controllerAs: "vm" } + }, + resolve: { + currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) { + return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]); + } } }, { @@ -79,6 +89,13 @@ import {ProfileActionsComponent} from "./profile-actions.component"; controller: CmsComponent, controllerAs: "vm" } + }, + resolve: { + currentArticle: function ($q: ng.IQService, currentProfile: noosfero.Profile, articleService: ArticleService, $stateParams: angular.ui.IStateParamsService): ng.IPromise { + let deferred: ng.IDeferred = $q.defer(); + articleService.getArticleByProfileAndPath(currentProfile, $stateParams["page"]).then(result => deferred.resolve(result.data)); + return deferred.promise; + } } }, { @@ -107,24 +124,41 @@ import {ProfileActionsComponent} from "./profile-actions.component"; controller: ContentViewerActionsComponent, controllerAs: "vm" } + }, + resolve: { + currentArticle: function ($q: ng.IQService, currentProfile: noosfero.Profile, articleService: ArticleService, $stateParams: angular.ui.IStateParamsService): ng.IPromise { + let deferred: ng.IDeferred = $q.defer(); + articleService.getArticleByProfileAndPath(currentProfile, $stateParams["page"]).then(result => deferred.resolve(result.data)); + return deferred.promise; + } } } ]) -@Inject(ProfileService, "$stateParams", "$state") -export class ProfileComponent { + +/** + * @ngdoc controller + * @name ProfileComponent + * @description + * Used as base controller for the route /:profile + * the "currentProfile" injected is provided by the "resolve" currentProfile + * available at @MainComponent @StateConfig definition + */ +@Inject(ProfileService, "$state", "currentProfile") +export class ProfileComponent { boxes: noosfero.Box[]; - profile: noosfero.Profile; - constructor(profileService: ProfileService, $stateParams: ng.ui.IStateParamsService, $state: ng.ui.IStateService, notificationService: NotificationService) { - profileService.setCurrentProfileByIdentifier($stateParams["profile"]).then((profile: noosfero.Profile) => { - this.profile = profile; - return profileService.getBoxes(this.profile.id); - }).then((response: restangular.IResponse) => { - this.boxes = response.data.boxes; - }).catch(() => { - $state.transitionTo('main.environment.home'); - notificationService.error({ message: "notification.profile.not_found" }); - }); + constructor( + profileService: ProfileService, + $state: ng.ui.IStateService, + notificationService: NotificationService, + public profile: noosfero.Profile) { + profileService.getBoxes(this.profile.id) + .then((response: restangular.IResponse) => { + this.boxes = response.data.boxes; + }).catch(() => { + $state.transitionTo('main.environment.home'); + notificationService.error({ message: "notification.profile.not_found" }); + }); } } diff --git a/src/lib/ng-noosfero-api/http/profile.service.ts b/src/lib/ng-noosfero-api/http/profile.service.ts index 4e9bd64..4b58a12 100644 --- a/src/lib/ng-noosfero-api/http/profile.service.ts +++ b/src/lib/ng-noosfero-api/http/profile.service.ts @@ -4,30 +4,33 @@ import { Injectable, Inject } from "ng-forward"; @Inject("Restangular", "$q") export class ProfileService { - private _currentProfilePromise: ng.IDeferred; - + // private _currentProfilePromise: ng.IDeferred; constructor(private restangular: restangular.IService, private $q: ng.IQService) { - this.resetCurrentProfile(); + // this.resetCurrentProfile(); } - resetCurrentProfile() { - this._currentProfilePromise = this.$q.defer(); - } + // resetCurrentProfile() { + // this._currentProfilePromise = this.$q.defer(); + // } - getCurrentProfile(): ng.IPromise { - return this._currentProfilePromise.promise; - } + // getCurrentProfile(): ng.IPromise { + // return this._currentProfilePromise.promise; + // } - setCurrentProfile(profile: noosfero.Profile) { - this._currentProfilePromise.resolve(profile); - } + // setCurrentProfile(profile: noosfero.Profile) { + // this._currentProfilePromise.resolve(profile); + // } - setCurrentProfileByIdentifier(identifier: string) { - this.resetCurrentProfile(); - return this.getByIdentifier(identifier).then((profile: noosfero.Profile) => { - this.setCurrentProfile(profile); - return this.getCurrentProfile(); - }); + // setCurrentProfileByIdentifier(identifier: string) { + // this.resetCurrentProfile(); + // return this.getByIdentifier(identifier).then((profile: noosfero.Profile) => { + // this.setCurrentProfile(profile); + // return this.getCurrentProfile(); + // }); + // } + + getCurrentProfileByIdentifier(identifier: string): Promise { + return this.getByIdentifier(identifier); } getHomePage(profileId: number, params?: any) { -- libgit2 0.21.2