Commit ec0140bcb3d2be6325cc3ce77b1fcca8789c23dd

Authored by Carlos Purificação
2 parents 10f26709 12a1974c

Merge branch 'change_environment_name' into 'master'

Change environment name

### Updated to shows the EnvironmentName dinamically on the navbar component.

Some changes were made to bring the request to the api to return the environment to the `@StateConfig` `resolve` function of the mainComponent.

See merge request !35
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.spec.ts 0 → 100644
@@ -0,0 +1,81 @@ @@ -0,0 +1,81 @@
  1 +
  2 +import { provide, Component, componentStore, bundleStore } from "ng-forward";
  3 +import {MainComponent} from "./main.component";
  4 +import {TestComponentBuilder, ComponentFixture} from "ng-forward/cjs/testing/test-component-builder";
  5 +
  6 +import {quickCreateComponent} from "./../../spec/helpers";
  7 +
  8 +describe("MainComponent", function () {
  9 +
  10 + let localFixture: ComponentFixture;
  11 + let $state: angular.ui.IStateService;
  12 + let $q: ng.IQService;
  13 + let authService: any = jasmine.createSpyObj("authService", ["loginFromCookie"]);
  14 + let environmentService: any = jasmine.createSpyObj("environmentService", ["get"]);
  15 +
  16 + beforeEach(angular.mock.module("ui.router"));
  17 + beforeEach(angular.mock.module("templates"));
  18 +
  19 + @Component({
  20 + selector: "parent",
  21 + template: "<main></main>",
  22 + directives: [MainComponent],
  23 + providers: [
  24 + provide("AuthService",
  25 + {
  26 + useValue: authService
  27 + }),
  28 +
  29 + provide("EnvironmentService",
  30 + {
  31 + useValue: environmentService
  32 + }),
  33 + ]
  34 + })
  35 + class MainComponentParent {
  36 + constructor() {
  37 +
  38 + }
  39 + }
  40 +
  41 + beforeEach(() => {
  42 + authService.loginFromCookie = jasmine.createSpy("loginFromCookie").and.returnValue({ id: 1, name: "user1" });
  43 + environmentService.get = jasmine.createSpy("get").and.returnValue({ id: 1, name: "Noosfero Default Environment" });
  44 + });
  45 +
  46 + it("renders the main component only when the login and environment were resolved", (done) => {
  47 + quickCreateComponent({ directives: [MainComponentParent], template: "<parent></parent>" })
  48 + .then((fixture) => {
  49 + localFixture = fixture;
  50 + // get the $state service to navigate between routes
  51 + $state = fixture.debugElement.getLocal("$state");
  52 + // navigates to the environment home
  53 + $state.go("main.environment.home");
  54 + localFixture.detectChanges();
  55 + // after changes were detected it checks the current $state route
  56 + expect($state.current.name).toEqual("main.environment.home");
  57 + done();
  58 + });
  59 +
  60 + });
  61 +
  62 + it("does not render the main component when get error loading the environment", (done) => {
  63 + quickCreateComponent({ directives: [MainComponentParent], template: "<parent></parent>" })
  64 + .then((fixture) => {
  65 + localFixture = fixture;
  66 + // get the $state service to navigate between routes
  67 + $state = fixture.debugElement.getLocal("$state");
  68 + // get the $q service to create a rejected promise
  69 + $q = fixture.debugElement.getLocal("$q");
  70 + // mock the environmentService to force a rejected promise
  71 + environmentService.get = jasmine.createSpy("get").and.returnValue($q.reject("Error simulated"));
  72 + // tries to navigate to the environment home
  73 + $state.go("main.environment.home");
  74 + localFixture.detectChanges();
  75 + // after the changes were detected the state remains '' because the environment could not be loaded
  76 + expect($state.current.name).toEqual("");
  77 + done();
  78 + });
  79 +
  80 + });
  81 +});
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";
@@ -114,6 +114,9 @@ export class EnvironmentContent { @@ -114,6 +114,9 @@ export class EnvironmentContent {
114 resolve: { 114 resolve: {
115 currentUser: function(AuthService: AuthService) { 115 currentUser: function(AuthService: AuthService) {
116 return AuthService.loginFromCookie(); 116 return AuthService.loginFromCookie();
  117 + },
  118 + currentEnvironment: function(EnvironmentService: EnvironmentService) {
  119 + return EnvironmentService.get();
117 } 120 }
118 } 121 }
119 }, 122 },
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 */