Commit b6c4bc1a5b1ad6e486b62ef776a183717fb37ae6

Authored by Ábner Oliveira
1 parent 0456e67c

changed to get environment name using the environment object. changed to use res…

…olve to only render mainComponent if an environment is found
src/app/article/comment/comments.component.ts
@@ -36,7 +36,7 @@ export class CommentsComponent { @@ -36,7 +36,7 @@ export class CommentsComponent {
36 comment.__show_reply = false; 36 comment.__show_reply = false;
37 if (comment.reply_of) { 37 if (comment.reply_of) {
38 this.comments.forEach((commentOnList) => { 38 this.comments.forEach((commentOnList) => {
39 - if (commentOnList.id == comment.reply_of.id) { 39 + if (commentOnList.id === comment.reply_of.id) {
40 if (commentOnList.replies) { 40 if (commentOnList.replies) {
41 commentOnList.replies.push(comment); 41 commentOnList.replies.push(comment);
42 } else { 42 } else {
src/app/environment/environment-home.component.ts
@@ -22,7 +22,7 @@ export class EnvironmentHomeComponent { @@ -22,7 +22,7 @@ export class EnvironmentHomeComponent {
22 environment: noosfero.Environment; 22 environment: noosfero.Environment;
23 23
24 constructor(private environmentService: EnvironmentService, private $sce: ng.ISCEService) { 24 constructor(private environmentService: EnvironmentService, private $sce: ng.ISCEService) {
25 - environmentService.getByIdentifier("default").then((result: noosfero.Environment) => { 25 + environmentService.get().then((result: noosfero.Environment) => {
26 this.environment = result; 26 this.environment = result;
27 }); 27 });
28 } 28 }
src/app/environment/environment.component.spec.ts
@@ -9,6 +9,7 @@ describe("Components", () => { @@ -9,6 +9,7 @@ describe("Components", () => {
9 let environmentServiceMock: any; 9 let environmentServiceMock: any;
10 let notificationMock: any; 10 let notificationMock: any;
11 let $state: any; 11 let $state: any;
  12 + let defaultEnvironment = <any> {id: 1, name: 'Noosfero' };
12 13
13 beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { 14 beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => {
14 $rootScope = _$rootScope_; 15 $rootScope = _$rootScope_;
@@ -17,44 +18,40 @@ describe(&quot;Components&quot;, () =&gt; { @@ -17,44 +18,40 @@ describe(&quot;Components&quot;, () =&gt; {
17 18
18 beforeEach(() => { 19 beforeEach(() => {
19 $state = jasmine.createSpyObj("$state", ["transitionTo"]); 20 $state = jasmine.createSpyObj("$state", ["transitionTo"]);
20 - environmentServiceMock = jasmine.createSpyObj("environmentServiceMock", ["getByIdentifier", "getBoxes"]); 21 + environmentServiceMock = jasmine.createSpyObj("environmentServiceMock", ["get", "getBoxes"]);
21 notificationMock = jasmine.createSpyObj("notificationMock", ["error"]); 22 notificationMock = jasmine.createSpyObj("notificationMock", ["error"]);
22 23
23 - let environmentResponse = $q.defer();  
24 - environmentResponse.resolve({ id: 1 });  
25 let getBoxesResponse = $q.defer(); 24 let getBoxesResponse = $q.defer();
26 getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } }); 25 getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } });
27 26
28 - environmentServiceMock.getByIdentifier = jasmine.createSpy('getByIdentifier').and.returnValue(environmentResponse.promise);  
29 environmentServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise); 27 environmentServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise);
30 }); 28 });
31 29
32 it("get the default environment", done => { 30 it("get the default environment", done => {
33 - let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock); 31 + let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock, defaultEnvironment);
34 $rootScope.$apply(); 32 $rootScope.$apply();
35 - expect(component.environment).toEqual({ id: 1 }); 33 + expect(component.environment).toEqual({ id: 1, name: 'Noosfero' });
36 done(); 34 done();
37 }); 35 });
38 36
39 it("get the environment boxes", done => { 37 it("get the environment boxes", done => {
40 - let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock); 38 + let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock, defaultEnvironment);
41 $rootScope.$apply(); 39 $rootScope.$apply();
42 expect(environmentServiceMock.getBoxes).toHaveBeenCalled(); 40 expect(environmentServiceMock.getBoxes).toHaveBeenCalled();
43 expect(component.boxes).toEqual({ data: { boxes: [{ id: 2 }] } }); 41 expect(component.boxes).toEqual({ data: { boxes: [{ id: 2 }] } });
44 done(); 42 done();
45 }); 43 });
46 44
47 - it("display notification error when the environment wasn't found", done => { 45 + it("display notification error when does not find boxes to the environment", done => {
48 let environmentResponse = $q.defer(); 46 let environmentResponse = $q.defer();
49 environmentResponse.reject(); 47 environmentResponse.reject();
50 48
51 - environmentServiceMock.getByIdentifier = jasmine.createSpy('getByIdentifier').and.returnValue(environmentResponse.promise); 49 + environmentServiceMock.getBoxes = jasmine.createSpy('getBoxes').and.returnValue(environmentResponse.promise);
52 50
53 - let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock); 51 + let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock, defaultEnvironment);
54 $rootScope.$apply(); 52 $rootScope.$apply();
55 53
56 expect(notificationMock.error).toHaveBeenCalled(); 54 expect(notificationMock.error).toHaveBeenCalled();
57 - expect(component.environment).toBeUndefined();  
58 done(); 55 done();
59 }); 56 });
60 57
src/app/environment/environment.component.ts
@@ -31,21 +31,23 @@ import {EnvironmentHomeComponent} from &quot;./environment-home.component&quot;; @@ -31,21 +31,23 @@ import {EnvironmentHomeComponent} from &quot;./environment-home.component&quot;;
31 } 31 }
32 } 32 }
33 ]) 33 ])
34 -@Inject(EnvironmentService, "$state") 34 +@Inject(EnvironmentService, "$state", "currentEnvironment")
35 export class EnvironmentComponent { 35 export class EnvironmentComponent {
36 36
37 boxes: noosfero.Box[]; 37 boxes: noosfero.Box[];
38 environment: noosfero.Environment; 38 environment: noosfero.Environment;
39 39
40 - constructor(environmentService: EnvironmentService, $state: ng.ui.IStateService, notificationService: NotificationService) {  
41 - let boxesPromisse = environmentService.getByIdentifier("default").then((environment: noosfero.Environment) => {  
42 - this.environment = environment;  
43 - return environmentService.getBoxes(this.environment.id);  
44 - }).then((boxes: noosfero.Box[]) => {  
45 - this.boxes = boxes;  
46 - }).catch(() => {  
47 - $state.transitionTo('main');  
48 - notificationService.error({ message: "notification.environment.not_found" });  
49 - }); 40 + constructor(private environmentService: EnvironmentService, private $state: ng.ui.IStateService, private notificationService: NotificationService, currentEnvironment: noosfero.Environment) {
  41 + this.environment = currentEnvironment;
  42 +
  43 + this.environmentService.getBoxes(this.environment.id)
  44 + .then((boxes: noosfero.Box[]) => {
  45 + this.boxes = boxes;
  46 + }).catch(() => {
  47 + this.$state.transitionTo('main');
  48 + this.notificationService.error({ message: "notification.environment.not_found" });
  49 + });
  50 +
50 } 51 }
  52 +
51 } 53 }
src/app/layout/navbar/navbar.html
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 </button> 7 </button>
8 <a class="navbar-brand" ui-sref="main.environment.home"> 8 <a class="navbar-brand" ui-sref="main.environment.home">
9 <span class="noosfero-logo"> </span> 9 <span class="noosfero-logo"> </span>
10 - <span class="noosfero-name">{{"noosfero.name" | translate}}</span> 10 + <span class="noosfero-name">{{ ctrl.currentEnvironment.name }}</span>
11 </a> 11 </a>
12 </div> 12 </div>
13 13
src/app/layout/navbar/navbar.spec.ts
@@ -69,6 +69,11 @@ describe(&quot;Components&quot;, () =&gt; { @@ -69,6 +69,11 @@ describe(&quot;Components&quot;, () =&gt; {
69 AuthEvents 69 AuthEvents
70 } 70 }
71 }), 71 }),
  72 + provide('EnvironmentService', {
  73 + useValue: {
  74 + getCurrentEnviroment: () => { return { id: 1, name: 'Nosofero' }; }
  75 + }
  76 + }),
72 provide('TranslatorService', { 77 provide('TranslatorService', {
73 useValue: helpers.mocks.translatorService 78 useValue: helpers.mocks.translatorService
74 }) 79 })
src/app/layout/navbar/navbar.ts
1 import {Component, Inject, EventEmitter, Input} from "ng-forward"; 1 import {Component, Inject, EventEmitter, Input} from "ng-forward";
2 import {LanguageSelectorComponent} from "../language-selector/language-selector.component"; 2 import {LanguageSelectorComponent} from "../language-selector/language-selector.component";
3 import {SessionService, AuthService, AuthController, AuthEvents} from "./../../login"; 3 import {SessionService, AuthService, AuthController, AuthEvents} from "./../../login";
  4 +import {EnvironmentService} from "./../../../lib/ng-noosfero-api/http/environment.service";
4 import {SidebarNotificationService} from "../sidebar/sidebar.notification.service"; 5 import {SidebarNotificationService} from "../sidebar/sidebar.notification.service";
5 import {BodyStateClassesService} from '../services/body-state-classes.service'; 6 import {BodyStateClassesService} from '../services/body-state-classes.service';
6 7
@@ -8,15 +9,15 @@ import {BodyStateClassesService} from &#39;../services/body-state-classes.service&#39;; @@ -8,15 +9,15 @@ import {BodyStateClassesService} from &#39;../services/body-state-classes.service&#39;;
8 selector: "acme-navbar", 9 selector: "acme-navbar",
9 templateUrl: "app/layout/navbar/navbar.html", 10 templateUrl: "app/layout/navbar/navbar.html",
10 directives: [LanguageSelectorComponent], 11 directives: [LanguageSelectorComponent],
11 - providers: [AuthService, SessionService, SidebarNotificationService] 12 + providers: [AuthService, SessionService, SidebarNotificationService, EnvironmentService]
12 }) 13 })
13 -@Inject("$uibModal", AuthService, "SessionService", "$state", SidebarNotificationService, BodyStateClassesService) 14 +@Inject("$uibModal", AuthService, "SessionService", "$state", SidebarNotificationService, BodyStateClassesService, EnvironmentService)
14 export class Navbar { 15 export class Navbar {
15 16
16 private currentUser: noosfero.User; 17 private currentUser: noosfero.User;
17 private modalInstance: any = null; 18 private modalInstance: any = null;
18 -  
19 public showHamburguer: boolean = false; 19 public showHamburguer: boolean = false;
  20 + public currentEnvironment: noosfero.Environment = <any>{ name: '' };
20 21
21 /** 22 /**
22 * 23 *
@@ -27,9 +28,11 @@ export class Navbar { @@ -27,9 +28,11 @@ export class Navbar {
27 private session: SessionService, 28 private session: SessionService,
28 private $state: ng.ui.IStateService, 29 private $state: ng.ui.IStateService,
29 private sidebarNotificationService: SidebarNotificationService, 30 private sidebarNotificationService: SidebarNotificationService,
30 - private bodyStateService: BodyStateClassesService 31 + private bodyStateService: BodyStateClassesService,
  32 + private environmentService: EnvironmentService
31 ) { 33 ) {
32 this.currentUser = this.session.currentUser(); 34 this.currentUser = this.session.currentUser();
  35 + this.currentEnvironment = environmentService.getCurrentEnviroment();
33 36
34 this.showHamburguer = this.authService.isAuthenticated(); 37 this.showHamburguer = this.authService.isAuthenticated();
35 this.bodyStateService.addContentClass(!this.sidebarNotificationService.sidebarVisible); 38 this.bodyStateService.addContentClass(!this.sidebarNotificationService.sidebarVisible);
src/app/main/main.component.ts
@@ -28,7 +28,7 @@ import {DateFormat} from &quot;../shared/pipes/date-format.filter&quot;; @@ -28,7 +28,7 @@ import {DateFormat} from &quot;../shared/pipes/date-format.filter&quot;;
28 28
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 - 31 +import {EnvironmentService} from "./../../lib/ng-noosfero-api/http/environment.service";
32 import {NotificationService} from "../shared/services/notification.service"; 32 import {NotificationService} from "../shared/services/notification.service";
33 33
34 import {BodyStateClassesService} from "./../layout/services/body-state-classes.service"; 34 import {BodyStateClassesService} from "./../layout/services/body-state-classes.service";
@@ -113,7 +113,12 @@ export class EnvironmentContent { @@ -113,7 +113,12 @@ export class EnvironmentContent {
113 name: 'main', 113 name: 'main',
114 resolve: { 114 resolve: {
115 currentUser: function(AuthService: AuthService) { 115 currentUser: function(AuthService: AuthService) {
  116 + console.log("RESOLVING USER...");
116 return AuthService.loginFromCookie(); 117 return AuthService.loginFromCookie();
  118 + },
  119 + currentEnvironment: function(EnvironmentService: EnvironmentService) {
  120 + console.log("RESOLVING ENVIRONMENT...");
  121 + return EnvironmentService.get();
117 } 122 }
118 } 123 }
119 }, 124 },
src/lib/ng-noosfero-api/http/environment.service.ts
@@ -4,12 +4,16 @@ import { Injectable, Inject } from &quot;ng-forward&quot;; @@ -4,12 +4,16 @@ import { Injectable, Inject } from &quot;ng-forward&quot;;
4 @Inject("Restangular", "$q") 4 @Inject("Restangular", "$q")
5 export class EnvironmentService { 5 export class EnvironmentService {
6 6
7 - private _currentEnvironmentPromise: ng.IDeferred<noosfero.Environment>;  
8 7
  8 + private currentEnvironment: noosfero.Environment = null;
9 constructor(private restangular: restangular.IService, private $q: ng.IQService) { 9 constructor(private restangular: restangular.IService, private $q: ng.IQService) {
10 10
11 } 11 }
12 12
  13 + getCurrentEnviroment(): noosfero.Environment {
  14 + return this.currentEnvironment;
  15 + }
  16 +
13 getEnvironmentPeople(params: any): ng.IPromise<noosfero.Person[]> { 17 getEnvironmentPeople(params: any): ng.IPromise<noosfero.Person[]> {
14 let p = this.restangular.one('people').get(params); 18 let p = this.restangular.one('people').get(params);
15 let deferred = this.$q.defer<noosfero.Person[]>(); 19 let deferred = this.$q.defer<noosfero.Person[]>();
@@ -18,10 +22,19 @@ export class EnvironmentService { @@ -18,10 +22,19 @@ export class EnvironmentService {
18 return deferred.promise; 22 return deferred.promise;
19 } 23 }
20 24
21 - getByIdentifier(identifier: string): ng.IPromise<noosfero.Environment> { 25 + get(identifier: string = 'default'): ng.IPromise<noosfero.Environment> {
22 let p = this.restangular.one('environment').customGET(identifier); 26 let p = this.restangular.one('environment').customGET(identifier);
23 let deferred = this.$q.defer<noosfero.Environment>(); 27 let deferred = this.$q.defer<noosfero.Environment>();
24 - p.then(this.getHandleSuccessFunction<noosfero.Environment>(deferred)); 28 + if (identifier === 'default') {
  29 + p.then((response) => {
  30 + let data = this.restangular.stripRestangular(response.data);
  31 + this.currentEnvironment = data;
  32 + this.getHandleSuccessFunction<noosfero.Environment>(deferred).bind(this)(response);
  33 + });
  34 + } else {
  35 + p.then(this.getHandleSuccessFunction<noosfero.Environment>(deferred));
  36 + }
  37 +
25 p.catch(this.getHandleErrorFunction<noosfero.Environment>(deferred)); 38 p.catch(this.getHandleErrorFunction<noosfero.Environment>(deferred));
26 return deferred.promise; 39 return deferred.promise;
27 } 40 }
@@ -37,7 +50,7 @@ export class EnvironmentService { @@ -37,7 +50,7 @@ export class EnvironmentService {
37 /** TODO - Please, use the base class RestangularService 50 /** TODO - Please, use the base class RestangularService
38 * (description) 51 * (description)
39 * 52 *
40 - * @template T 53 + * @template T_currentEnvironmentPromise
41 * @param {ng.IDeferred<T>} deferred (description) 54 * @param {ng.IDeferred<T>} deferred (description)
42 * @returns {(response: restangular.IResponse) => void} (description) 55 * @returns {(response: restangular.IResponse) => void} (description)
43 */ 56 */