Commit 5c16da40f2ed9af570a6c18e968d40bdc6ebd111

Authored by Ábner Oliveira
1 parent b9f6daf6

starting to resolve profile and articles on ui-router resolve functions

src/app/article/content-viewer/content-viewer.component.ts
@@ -14,26 +14,26 @@ import {ProfileService} from "../../../lib/ng-noosfero-api/http/profile.service" @@ -14,26 +14,26 @@ import {ProfileService} from "../../../lib/ng-noosfero-api/http/profile.service"
14 provide('profileService', { useClass: ProfileService }) 14 provide('profileService', { useClass: ProfileService })
15 ] 15 ]
16 }) 16 })
17 -@Inject(ArticleService, ProfileService, "$log", "$stateParams") 17 +@Inject(ArticleService, ProfileService, "$log", "$stateParams", "currentProfile")
18 export class ContentViewerComponent { 18 export class ContentViewerComponent {
19 19
20 @Input() 20 @Input()
21 article: noosfero.Article = null; 21 article: noosfero.Article = null;
22 22
23 - @Input()  
24 - profile: noosfero.Profile = null;  
25 -  
26 - constructor(private articleService: ArticleService, private profileService: ProfileService, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { 23 + constructor(
  24 + private articleService: ArticleService,
  25 + private profileService: ProfileService,
  26 + private $log: ng.ILogService,
  27 + private $stateParams: angular.ui.IStateParamsService,
  28 + public profile: noosfero.Profile) {
27 this.activate(); 29 this.activate();
28 } 30 }
29 31
30 activate() { 32 activate() {
31 - this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => {  
32 - this.profile = profile;  
33 - return this.articleService.getArticleByProfileAndPath(this.profile, this.$stateParams["page"]);  
34 - }).then((result: noosfero.RestResult<any>) => {  
35 - this.article = <noosfero.Article>result.data;  
36 - this.articleService.setCurrent(this.article);  
37 - }); 33 + this.articleService.getArticleByProfileAndPath(this.profile, this.$stateParams["page"])
  34 + .then((result: noosfero.RestResult<any>) => {
  35 + this.article = <noosfero.Article>result.data;
  36 + this.articleService.setCurrent(this.article);
  37 + });
38 } 38 }
39 } 39 }
src/app/main/main.component.ts
@@ -29,6 +29,7 @@ import {DateFormat} from &quot;../shared/pipes/date-format.filter&quot;; @@ -29,6 +29,7 @@ import {DateFormat} from &quot;../shared/pipes/date-format.filter&quot;;
29 import {AuthService} from "../login/auth.service"; 29 import {AuthService} from "../login/auth.service";
30 import {SessionService} from "../login/session.service"; 30 import {SessionService} from "../login/session.service";
31 import {EnvironmentService} from "./../../lib/ng-noosfero-api/http/environment.service"; 31 import {EnvironmentService} from "./../../lib/ng-noosfero-api/http/environment.service";
  32 +import {ProfileService} from './../../lib/ng-noosfero-api/http/profile.service';
32 import {NotificationService} from "../shared/services/notification.service"; 33 import {NotificationService} from "../shared/services/notification.service";
33 34
34 import {BodyStateClassesService} from "./../layout/services/body-state-classes.service"; 35 import {BodyStateClassesService} from "./../layout/services/body-state-classes.service";
@@ -118,10 +119,10 @@ export class EnvironmentContent { @@ -118,10 +119,10 @@ export class EnvironmentContent {
118 abstract: true, 119 abstract: true,
119 name: 'main', 120 name: 'main',
120 resolve: { 121 resolve: {
121 - currentUser: function(AuthService: AuthService) { 122 + currentUser: function (AuthService: AuthService) {
122 return AuthService.loginFromCookie(); 123 return AuthService.loginFromCookie();
123 }, 124 },
124 - currentEnvironment: function(EnvironmentService: EnvironmentService) { 125 + currentEnvironment: function (EnvironmentService: EnvironmentService) {
125 return EnvironmentService.get(); 126 return EnvironmentService.get();
126 } 127 }
127 } 128 }
@@ -149,6 +150,12 @@ export class EnvironmentContent { @@ -149,6 +150,12 @@ export class EnvironmentContent {
149 templateUrl: "app/profile/profile.html", 150 templateUrl: "app/profile/profile.html",
150 controller: ProfileComponent, 151 controller: ProfileComponent,
151 controllerAs: "vm" 152 controllerAs: "vm"
  153 + },
  154 + },
  155 + resolve: {
  156 + currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) {
  157 + console.log('$stateParams', $stateParams);
  158 + return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]);
152 } 159 }
153 } 160 }
154 } 161 }
src/app/profile/profile-home.component.ts
@@ -7,23 +7,19 @@ import {ProfileService} from &quot;../../lib/ng-noosfero-api/http/profile.service&quot;; @@ -7,23 +7,19 @@ import {ProfileService} from &quot;../../lib/ng-noosfero-api/http/profile.service&quot;;
7 template: "<div></div>", 7 template: "<div></div>",
8 providers: [provide('profileService', { useClass: ProfileService })] 8 providers: [provide('profileService', { useClass: ProfileService })]
9 }) 9 })
10 -@Inject(ProfileService, "$state") 10 +@Inject(ProfileService, "$state", "currentProfile")
11 export class ProfileHomeComponent { 11 export class ProfileHomeComponent {
12 12
13 - profile: noosfero.Profile;  
14 -  
15 - constructor(profileService: ProfileService, $state: ng.ui.IStateService) {  
16 - profileService.getCurrentProfile().then((profile: noosfero.Profile) => {  
17 - this.profile = profile;  
18 - return profileService.getHomePage(<number>this.profile.id, { fields: 'path' });  
19 - }).then((response: restangular.IResponse) => {  
20 - if (response.data.article) {  
21 - this.profile.homepage = response.data.article.path;  
22 - $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false });  
23 - } else {  
24 - this.profile.homepage = null;  
25 - $state.transitionTo('main.profile.info', { profile: this.profile.identifier }, { location: false });  
26 - }  
27 - }); 13 + constructor(profileService: ProfileService, $state: ng.ui.IStateService, public profile: noosfero.Profile) {
  14 + profileService.getHomePage(<number>this.profile.id, { fields: 'path' })
  15 + .then((response: restangular.IResponse) => {
  16 + if (response.data.article) {
  17 + this.profile.homepage = response.data.article.path;
  18 + $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false });
  19 + } else {
  20 + this.profile.homepage = null;
  21 + $state.transitionTo('main.profile.info', { profile: this.profile.identifier }, { location: false });
  22 + }
  23 + });
28 } 24 }
29 } 25 }
src/app/profile/profile.component.ts
1 -import {StateConfig, Component, Inject, provide} from 'ng-forward'; 1 +import {StateConfig, Resolve, Component, Inject, provide} from 'ng-forward';
2 import {ProfileInfoComponent} from './info/profile-info.component'; 2 import {ProfileInfoComponent} from './info/profile-info.component';
3 import {ProfileHomeComponent} from './profile-home.component'; 3 import {ProfileHomeComponent} from './profile-home.component';
4 import {BasicEditorComponent} from '../article/cms/basic-editor/basic-editor.component'; 4 import {BasicEditorComponent} from '../article/cms/basic-editor/basic-editor.component';
@@ -10,7 +10,7 @@ import {ProfileService} from &quot;../../lib/ng-noosfero-api/http/profile.service&quot;; @@ -10,7 +10,7 @@ import {ProfileService} from &quot;../../lib/ng-noosfero-api/http/profile.service&quot;;
10 import {NotificationService} from "../shared/services/notification.service"; 10 import {NotificationService} from "../shared/services/notification.service";
11 import {MyProfileComponent} from "./myprofile.component"; 11 import {MyProfileComponent} from "./myprofile.component";
12 import {ProfileActionsComponent} from "./profile-actions.component"; 12 import {ProfileActionsComponent} from "./profile-actions.component";
13 - 13 +import {ArticleService} from './../../lib/ng-noosfero-api/http/article.service';
14 /** 14 /**
15 * @ngdoc controller 15 * @ngdoc controller
16 * @name profile.Profile 16 * @name profile.Profile
@@ -43,6 +43,11 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;; @@ -43,6 +43,11 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;;
43 controller: ProfileActionsComponent, 43 controller: ProfileActionsComponent,
44 controllerAs: "vm" 44 controllerAs: "vm"
45 } 45 }
  46 + },
  47 + resolve: {
  48 + currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) {
  49 + return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]);
  50 + }
46 } 51 }
47 }, 52 },
48 { 53 {
@@ -55,6 +60,11 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;; @@ -55,6 +60,11 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;;
55 controller: ProfileActionsComponent, 60 controller: ProfileActionsComponent,
56 controllerAs: "vm" 61 controllerAs: "vm"
57 } 62 }
  63 + },
  64 + resolve: {
  65 + currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) {
  66 + return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]);
  67 + }
58 } 68 }
59 }, 69 },
60 { 70 {
@@ -79,6 +89,13 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;; @@ -79,6 +89,13 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;;
79 controller: CmsComponent, 89 controller: CmsComponent,
80 controllerAs: "vm" 90 controllerAs: "vm"
81 } 91 }
  92 + },
  93 + resolve: {
  94 + currentArticle: function ($q: ng.IQService, currentProfile: noosfero.Profile, articleService: ArticleService, $stateParams: angular.ui.IStateParamsService): ng.IPromise<noosfero.Article> {
  95 + let deferred: ng.IDeferred<noosfero.Article> = $q.defer<noosfero.Article>();
  96 + articleService.getArticleByProfileAndPath(currentProfile, $stateParams["page"]).then(result => deferred.resolve(result.data));
  97 + return deferred.promise;
  98 + }
82 } 99 }
83 }, 100 },
84 { 101 {
@@ -107,24 +124,41 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;; @@ -107,24 +124,41 @@ import {ProfileActionsComponent} from &quot;./profile-actions.component&quot;;
107 controller: ContentViewerActionsComponent, 124 controller: ContentViewerActionsComponent,
108 controllerAs: "vm" 125 controllerAs: "vm"
109 } 126 }
  127 + },
  128 + resolve: {
  129 + currentArticle: function ($q: ng.IQService, currentProfile: noosfero.Profile, articleService: ArticleService, $stateParams: angular.ui.IStateParamsService): ng.IPromise<noosfero.Article> {
  130 + let deferred: ng.IDeferred<noosfero.Article> = $q.defer<noosfero.Article>();
  131 + articleService.getArticleByProfileAndPath(currentProfile, $stateParams["page"]).then(result => deferred.resolve(result.data));
  132 + return deferred.promise;
  133 + }
110 } 134 }
111 } 135 }
112 ]) 136 ])
113 -@Inject(ProfileService, "$stateParams", "$state")  
114 -export class ProfileComponent {  
115 137
  138 +
  139 +/**
  140 + * @ngdoc controller
  141 + * @name ProfileComponent
  142 + * @description
  143 + * Used as base controller for the route /:profile
  144 + * the "currentProfile" injected is provided by the "resolve" currentProfile
  145 + * available at @MainComponent @StateConfig definition
  146 + */
  147 +@Inject(ProfileService, "$state", "currentProfile")
  148 +export class ProfileComponent {
116 boxes: noosfero.Box[]; 149 boxes: noosfero.Box[];
117 - profile: noosfero.Profile;  
118 150
119 - constructor(profileService: ProfileService, $stateParams: ng.ui.IStateParamsService, $state: ng.ui.IStateService, notificationService: NotificationService) {  
120 - profileService.setCurrentProfileByIdentifier($stateParams["profile"]).then((profile: noosfero.Profile) => {  
121 - this.profile = profile;  
122 - return profileService.getBoxes(<number>this.profile.id);  
123 - }).then((response: restangular.IResponse) => {  
124 - this.boxes = response.data.boxes;  
125 - }).catch(() => {  
126 - $state.transitionTo('main.environment.home');  
127 - notificationService.error({ message: "notification.profile.not_found" });  
128 - }); 151 + constructor(
  152 + profileService: ProfileService,
  153 + $state: ng.ui.IStateService,
  154 + notificationService: NotificationService,
  155 + public profile: noosfero.Profile) {
  156 + profileService.getBoxes(<number>this.profile.id)
  157 + .then((response: restangular.IResponse) => {
  158 + this.boxes = response.data.boxes;
  159 + }).catch(() => {
  160 + $state.transitionTo('main.environment.home');
  161 + notificationService.error({ message: "notification.profile.not_found" });
  162 + });
129 } 163 }
130 } 164 }
src/lib/ng-noosfero-api/http/profile.service.ts
@@ -4,30 +4,33 @@ import { Injectable, Inject } from &quot;ng-forward&quot;; @@ -4,30 +4,33 @@ import { Injectable, Inject } from &quot;ng-forward&quot;;
4 @Inject("Restangular", "$q") 4 @Inject("Restangular", "$q")
5 export class ProfileService { 5 export class ProfileService {
6 6
7 - private _currentProfilePromise: ng.IDeferred<noosfero.Profile>;  
8 - 7 + // private _currentProfilePromise: ng.IDeferred<noosfero.Profile>;
9 constructor(private restangular: restangular.IService, private $q: ng.IQService) { 8 constructor(private restangular: restangular.IService, private $q: ng.IQService) {
10 - this.resetCurrentProfile(); 9 + // this.resetCurrentProfile();
11 } 10 }
12 11
13 - resetCurrentProfile() {  
14 - this._currentProfilePromise = this.$q.defer();  
15 - } 12 + // resetCurrentProfile() {
  13 + // this._currentProfilePromise = this.$q.defer();
  14 + // }
16 15
17 - getCurrentProfile(): ng.IPromise<noosfero.Profile> {  
18 - return this._currentProfilePromise.promise;  
19 - } 16 + // getCurrentProfile(): ng.IPromise<noosfero.Profile> {
  17 + // return this._currentProfilePromise.promise;
  18 + // }
20 19
21 - setCurrentProfile(profile: noosfero.Profile) {  
22 - this._currentProfilePromise.resolve(profile);  
23 - } 20 + // setCurrentProfile(profile: noosfero.Profile) {
  21 + // this._currentProfilePromise.resolve(profile);
  22 + // }
24 23
25 - setCurrentProfileByIdentifier(identifier: string) {  
26 - this.resetCurrentProfile();  
27 - return this.getByIdentifier(identifier).then((profile: noosfero.Profile) => {  
28 - this.setCurrentProfile(profile);  
29 - return this.getCurrentProfile();  
30 - }); 24 + // setCurrentProfileByIdentifier(identifier: string) {
  25 + // this.resetCurrentProfile();
  26 + // return this.getByIdentifier(identifier).then((profile: noosfero.Profile) => {
  27 + // this.setCurrentProfile(profile);
  28 + // return this.getCurrentProfile();
  29 + // });
  30 + // }
  31 +
  32 + getCurrentProfileByIdentifier(identifier: string): Promise<noosfero.Profile> {
  33 + return this.getByIdentifier(identifier);
31 } 34 }
32 35
33 getHomePage(profileId: number, params?: any) { 36 getHomePage(profileId: number, params?: any) {