From b6c4bc1a5b1ad6e486b62ef776a183717fb37ae6 Mon Sep 17 00:00:00 2001 From: Abner Oliveira Date: Tue, 31 May 2016 18:34:06 -0300 Subject: [PATCH] changed to get environment name using the environment object. changed to use resolve to only render mainComponent if an environment is found --- src/app/article/comment/comments.component.ts | 2 +- src/app/environment/environment-home.component.ts | 2 +- src/app/environment/environment.component.spec.ts | 19 ++++++++----------- src/app/environment/environment.component.ts | 24 +++++++++++++----------- src/app/layout/navbar/navbar.html | 2 +- src/app/layout/navbar/navbar.spec.ts | 5 +++++ src/app/layout/navbar/navbar.ts | 11 +++++++---- src/app/main/main.component.ts | 7 ++++++- src/lib/ng-noosfero-api/http/environment.service.ts | 21 +++++++++++++++++---- 9 files changed, 59 insertions(+), 34 deletions(-) diff --git a/src/app/article/comment/comments.component.ts b/src/app/article/comment/comments.component.ts index c830e81..17f680c 100644 --- a/src/app/article/comment/comments.component.ts +++ b/src/app/article/comment/comments.component.ts @@ -36,7 +36,7 @@ export class CommentsComponent { comment.__show_reply = false; if (comment.reply_of) { this.comments.forEach((commentOnList) => { - if (commentOnList.id == comment.reply_of.id) { + if (commentOnList.id === comment.reply_of.id) { if (commentOnList.replies) { commentOnList.replies.push(comment); } else { diff --git a/src/app/environment/environment-home.component.ts b/src/app/environment/environment-home.component.ts index f989de2..75ad661 100644 --- a/src/app/environment/environment-home.component.ts +++ b/src/app/environment/environment-home.component.ts @@ -22,7 +22,7 @@ export class EnvironmentHomeComponent { environment: noosfero.Environment; constructor(private environmentService: EnvironmentService, private $sce: ng.ISCEService) { - environmentService.getByIdentifier("default").then((result: noosfero.Environment) => { + environmentService.get().then((result: noosfero.Environment) => { this.environment = result; }); } diff --git a/src/app/environment/environment.component.spec.ts b/src/app/environment/environment.component.spec.ts index 47b48a6..11c4f15 100644 --- a/src/app/environment/environment.component.spec.ts +++ b/src/app/environment/environment.component.spec.ts @@ -9,6 +9,7 @@ describe("Components", () => { let environmentServiceMock: any; let notificationMock: any; let $state: any; + let defaultEnvironment = {id: 1, name: 'Noosfero' }; beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { $rootScope = _$rootScope_; @@ -17,44 +18,40 @@ describe("Components", () => { beforeEach(() => { $state = jasmine.createSpyObj("$state", ["transitionTo"]); - environmentServiceMock = jasmine.createSpyObj("environmentServiceMock", ["getByIdentifier", "getBoxes"]); + environmentServiceMock = jasmine.createSpyObj("environmentServiceMock", ["get", "getBoxes"]); notificationMock = jasmine.createSpyObj("notificationMock", ["error"]); - let environmentResponse = $q.defer(); - environmentResponse.resolve({ id: 1 }); let getBoxesResponse = $q.defer(); getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } }); - environmentServiceMock.getByIdentifier = jasmine.createSpy('getByIdentifier').and.returnValue(environmentResponse.promise); environmentServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise); }); it("get the default environment", done => { - let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock); + let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock, defaultEnvironment); $rootScope.$apply(); - expect(component.environment).toEqual({ id: 1 }); + expect(component.environment).toEqual({ id: 1, name: 'Noosfero' }); done(); }); it("get the environment boxes", done => { - let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock); + let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock, defaultEnvironment); $rootScope.$apply(); expect(environmentServiceMock.getBoxes).toHaveBeenCalled(); expect(component.boxes).toEqual({ data: { boxes: [{ id: 2 }] } }); done(); }); - it("display notification error when the environment wasn't found", done => { + it("display notification error when does not find boxes to the environment", done => { let environmentResponse = $q.defer(); environmentResponse.reject(); - environmentServiceMock.getByIdentifier = jasmine.createSpy('getByIdentifier').and.returnValue(environmentResponse.promise); + environmentServiceMock.getBoxes = jasmine.createSpy('getBoxes').and.returnValue(environmentResponse.promise); - let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock); + let component: EnvironmentComponent = new EnvironmentComponent(environmentServiceMock, $state, notificationMock, defaultEnvironment); $rootScope.$apply(); expect(notificationMock.error).toHaveBeenCalled(); - expect(component.environment).toBeUndefined(); done(); }); diff --git a/src/app/environment/environment.component.ts b/src/app/environment/environment.component.ts index dc69467..352f3ee 100644 --- a/src/app/environment/environment.component.ts +++ b/src/app/environment/environment.component.ts @@ -31,21 +31,23 @@ import {EnvironmentHomeComponent} from "./environment-home.component"; } } ]) -@Inject(EnvironmentService, "$state") +@Inject(EnvironmentService, "$state", "currentEnvironment") export class EnvironmentComponent { boxes: noosfero.Box[]; environment: noosfero.Environment; - constructor(environmentService: EnvironmentService, $state: ng.ui.IStateService, notificationService: NotificationService) { - let boxesPromisse = environmentService.getByIdentifier("default").then((environment: noosfero.Environment) => { - this.environment = environment; - return environmentService.getBoxes(this.environment.id); - }).then((boxes: noosfero.Box[]) => { - this.boxes = boxes; - }).catch(() => { - $state.transitionTo('main'); - notificationService.error({ message: "notification.environment.not_found" }); - }); + constructor(private environmentService: EnvironmentService, private $state: ng.ui.IStateService, private notificationService: NotificationService, currentEnvironment: noosfero.Environment) { + this.environment = currentEnvironment; + + this.environmentService.getBoxes(this.environment.id) + .then((boxes: noosfero.Box[]) => { + this.boxes = boxes; + }).catch(() => { + this.$state.transitionTo('main'); + this.notificationService.error({ message: "notification.environment.not_found" }); + }); + } + } diff --git a/src/app/layout/navbar/navbar.html b/src/app/layout/navbar/navbar.html index 1ed001d..c76c7b2 100644 --- a/src/app/layout/navbar/navbar.html +++ b/src/app/layout/navbar/navbar.html @@ -7,7 +7,7 @@ - {{"noosfero.name" | translate}} + {{ ctrl.currentEnvironment.name }} diff --git a/src/app/layout/navbar/navbar.spec.ts b/src/app/layout/navbar/navbar.spec.ts index 59f309b..f8a3d5a 100644 --- a/src/app/layout/navbar/navbar.spec.ts +++ b/src/app/layout/navbar/navbar.spec.ts @@ -69,6 +69,11 @@ describe("Components", () => { AuthEvents } }), + provide('EnvironmentService', { + useValue: { + getCurrentEnviroment: () => { return { id: 1, name: 'Nosofero' }; } + } + }), provide('TranslatorService', { useValue: helpers.mocks.translatorService }) diff --git a/src/app/layout/navbar/navbar.ts b/src/app/layout/navbar/navbar.ts index 2966448..656ff9b 100644 --- a/src/app/layout/navbar/navbar.ts +++ b/src/app/layout/navbar/navbar.ts @@ -1,6 +1,7 @@ import {Component, Inject, EventEmitter, Input} from "ng-forward"; import {LanguageSelectorComponent} from "../language-selector/language-selector.component"; import {SessionService, AuthService, AuthController, AuthEvents} from "./../../login"; +import {EnvironmentService} from "./../../../lib/ng-noosfero-api/http/environment.service"; import {SidebarNotificationService} from "../sidebar/sidebar.notification.service"; import {BodyStateClassesService} from '../services/body-state-classes.service'; @@ -8,15 +9,15 @@ import {BodyStateClassesService} from '../services/body-state-classes.service'; selector: "acme-navbar", templateUrl: "app/layout/navbar/navbar.html", directives: [LanguageSelectorComponent], - providers: [AuthService, SessionService, SidebarNotificationService] + providers: [AuthService, SessionService, SidebarNotificationService, EnvironmentService] }) -@Inject("$uibModal", AuthService, "SessionService", "$state", SidebarNotificationService, BodyStateClassesService) +@Inject("$uibModal", AuthService, "SessionService", "$state", SidebarNotificationService, BodyStateClassesService, EnvironmentService) export class Navbar { private currentUser: noosfero.User; private modalInstance: any = null; - public showHamburguer: boolean = false; + public currentEnvironment: noosfero.Environment = { name: '' }; /** * @@ -27,9 +28,11 @@ export class Navbar { private session: SessionService, private $state: ng.ui.IStateService, private sidebarNotificationService: SidebarNotificationService, - private bodyStateService: BodyStateClassesService + private bodyStateService: BodyStateClassesService, + private environmentService: EnvironmentService ) { this.currentUser = this.session.currentUser(); + this.currentEnvironment = environmentService.getCurrentEnviroment(); this.showHamburguer = this.authService.isAuthenticated(); this.bodyStateService.addContentClass(!this.sidebarNotificationService.sidebarVisible); diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index ccf5e9d..7ceba9d 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -28,7 +28,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 {NotificationService} from "../shared/services/notification.service"; import {BodyStateClassesService} from "./../layout/services/body-state-classes.service"; @@ -113,7 +113,12 @@ export class EnvironmentContent { name: 'main', resolve: { currentUser: function(AuthService: AuthService) { + console.log("RESOLVING USER..."); return AuthService.loginFromCookie(); + }, + currentEnvironment: function(EnvironmentService: EnvironmentService) { + console.log("RESOLVING ENVIRONMENT..."); + return EnvironmentService.get(); } } }, diff --git a/src/lib/ng-noosfero-api/http/environment.service.ts b/src/lib/ng-noosfero-api/http/environment.service.ts index 20b3eca..7fa4883 100644 --- a/src/lib/ng-noosfero-api/http/environment.service.ts +++ b/src/lib/ng-noosfero-api/http/environment.service.ts @@ -4,12 +4,16 @@ import { Injectable, Inject } from "ng-forward"; @Inject("Restangular", "$q") export class EnvironmentService { - private _currentEnvironmentPromise: ng.IDeferred; + private currentEnvironment: noosfero.Environment = null; constructor(private restangular: restangular.IService, private $q: ng.IQService) { } + getCurrentEnviroment(): noosfero.Environment { + return this.currentEnvironment; + } + getEnvironmentPeople(params: any): ng.IPromise { let p = this.restangular.one('people').get(params); let deferred = this.$q.defer(); @@ -18,10 +22,19 @@ export class EnvironmentService { return deferred.promise; } - getByIdentifier(identifier: string): ng.IPromise { + get(identifier: string = 'default'): ng.IPromise { let p = this.restangular.one('environment').customGET(identifier); let deferred = this.$q.defer(); - p.then(this.getHandleSuccessFunction(deferred)); + if (identifier === 'default') { + p.then((response) => { + let data = this.restangular.stripRestangular(response.data); + this.currentEnvironment = data; + this.getHandleSuccessFunction(deferred).bind(this)(response); + }); + } else { + p.then(this.getHandleSuccessFunction(deferred)); + } + p.catch(this.getHandleErrorFunction(deferred)); return deferred.promise; } @@ -37,7 +50,7 @@ export class EnvironmentService { /** TODO - Please, use the base class RestangularService * (description) * - * @template T + * @template T_currentEnvironmentPromise * @param {ng.IDeferred} deferred (description) * @returns {(response: restangular.IResponse) => void} (description) */ -- libgit2 0.21.2