Commit b6c4bc1a5b1ad6e486b62ef776a183717fb37ae6
1 parent
0456e67c
Exists in
master
and in
14 other branches
changed to get environment name using the environment object. changed to use res…
…olve to only render mainComponent if an environment is found
Showing
9 changed files
with
59 additions
and
34 deletions
Show diff stats
src/app/article/comment/comments.component.ts
... | ... | @@ -36,7 +36,7 @@ export class CommentsComponent { |
36 | 36 | comment.__show_reply = false; |
37 | 37 | if (comment.reply_of) { |
38 | 38 | this.comments.forEach((commentOnList) => { |
39 | - if (commentOnList.id == comment.reply_of.id) { | |
39 | + if (commentOnList.id === comment.reply_of.id) { | |
40 | 40 | if (commentOnList.replies) { |
41 | 41 | commentOnList.replies.push(comment); |
42 | 42 | } else { | ... | ... |
src/app/environment/environment-home.component.ts
... | ... | @@ -22,7 +22,7 @@ export class EnvironmentHomeComponent { |
22 | 22 | environment: noosfero.Environment; |
23 | 23 | |
24 | 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 | 26 | this.environment = result; |
27 | 27 | }); |
28 | 28 | } | ... | ... |
src/app/environment/environment.component.spec.ts
... | ... | @@ -9,6 +9,7 @@ describe("Components", () => { |
9 | 9 | let environmentServiceMock: any; |
10 | 10 | let notificationMock: any; |
11 | 11 | let $state: any; |
12 | + let defaultEnvironment = <any> {id: 1, name: 'Noosfero' }; | |
12 | 13 | |
13 | 14 | beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { |
14 | 15 | $rootScope = _$rootScope_; |
... | ... | @@ -17,44 +18,40 @@ describe("Components", () => { |
17 | 18 | |
18 | 19 | beforeEach(() => { |
19 | 20 | $state = jasmine.createSpyObj("$state", ["transitionTo"]); |
20 | - environmentServiceMock = jasmine.createSpyObj("environmentServiceMock", ["getByIdentifier", "getBoxes"]); | |
21 | + environmentServiceMock = jasmine.createSpyObj("environmentServiceMock", ["get", "getBoxes"]); | |
21 | 22 | notificationMock = jasmine.createSpyObj("notificationMock", ["error"]); |
22 | 23 | |
23 | - let environmentResponse = $q.defer(); | |
24 | - environmentResponse.resolve({ id: 1 }); | |
25 | 24 | let getBoxesResponse = $q.defer(); |
26 | 25 | getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } }); |
27 | 26 | |
28 | - environmentServiceMock.getByIdentifier = jasmine.createSpy('getByIdentifier').and.returnValue(environmentResponse.promise); | |
29 | 27 | environmentServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise); |
30 | 28 | }); |
31 | 29 | |
32 | 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 | 32 | $rootScope.$apply(); |
35 | - expect(component.environment).toEqual({ id: 1 }); | |
33 | + expect(component.environment).toEqual({ id: 1, name: 'Noosfero' }); | |
36 | 34 | done(); |
37 | 35 | }); |
38 | 36 | |
39 | 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 | 39 | $rootScope.$apply(); |
42 | 40 | expect(environmentServiceMock.getBoxes).toHaveBeenCalled(); |
43 | 41 | expect(component.boxes).toEqual({ data: { boxes: [{ id: 2 }] } }); |
44 | 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 | 46 | let environmentResponse = $q.defer(); |
49 | 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 | 52 | $rootScope.$apply(); |
55 | 53 | |
56 | 54 | expect(notificationMock.error).toHaveBeenCalled(); |
57 | - expect(component.environment).toBeUndefined(); | |
58 | 55 | done(); |
59 | 56 | }); |
60 | 57 | ... | ... |
src/app/environment/environment.component.ts
... | ... | @@ -31,21 +31,23 @@ import {EnvironmentHomeComponent} from "./environment-home.component"; |
31 | 31 | } |
32 | 32 | } |
33 | 33 | ]) |
34 | -@Inject(EnvironmentService, "$state") | |
34 | +@Inject(EnvironmentService, "$state", "currentEnvironment") | |
35 | 35 | export class EnvironmentComponent { |
36 | 36 | |
37 | 37 | boxes: noosfero.Box[]; |
38 | 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 | </button> |
8 | 8 | <a class="navbar-brand" ui-sref="main.environment.home"> |
9 | 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 | 11 | </a> |
12 | 12 | </div> |
13 | 13 | ... | ... |
src/app/layout/navbar/navbar.spec.ts
... | ... | @@ -69,6 +69,11 @@ describe("Components", () => { |
69 | 69 | AuthEvents |
70 | 70 | } |
71 | 71 | }), |
72 | + provide('EnvironmentService', { | |
73 | + useValue: { | |
74 | + getCurrentEnviroment: () => { return { id: 1, name: 'Nosofero' }; } | |
75 | + } | |
76 | + }), | |
72 | 77 | provide('TranslatorService', { |
73 | 78 | useValue: helpers.mocks.translatorService |
74 | 79 | }) | ... | ... |
src/app/layout/navbar/navbar.ts
1 | 1 | import {Component, Inject, EventEmitter, Input} from "ng-forward"; |
2 | 2 | import {LanguageSelectorComponent} from "../language-selector/language-selector.component"; |
3 | 3 | import {SessionService, AuthService, AuthController, AuthEvents} from "./../../login"; |
4 | +import {EnvironmentService} from "./../../../lib/ng-noosfero-api/http/environment.service"; | |
4 | 5 | import {SidebarNotificationService} from "../sidebar/sidebar.notification.service"; |
5 | 6 | import {BodyStateClassesService} from '../services/body-state-classes.service'; |
6 | 7 | |
... | ... | @@ -8,15 +9,15 @@ import {BodyStateClassesService} from '../services/body-state-classes.service'; |
8 | 9 | selector: "acme-navbar", |
9 | 10 | templateUrl: "app/layout/navbar/navbar.html", |
10 | 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 | 15 | export class Navbar { |
15 | 16 | |
16 | 17 | private currentUser: noosfero.User; |
17 | 18 | private modalInstance: any = null; |
18 | - | |
19 | 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 | 28 | private session: SessionService, |
28 | 29 | private $state: ng.ui.IStateService, |
29 | 30 | private sidebarNotificationService: SidebarNotificationService, |
30 | - private bodyStateService: BodyStateClassesService | |
31 | + private bodyStateService: BodyStateClassesService, | |
32 | + private environmentService: EnvironmentService | |
31 | 33 | ) { |
32 | 34 | this.currentUser = this.session.currentUser(); |
35 | + this.currentEnvironment = environmentService.getCurrentEnviroment(); | |
33 | 36 | |
34 | 37 | this.showHamburguer = this.authService.isAuthenticated(); |
35 | 38 | this.bodyStateService.addContentClass(!this.sidebarNotificationService.sidebarVisible); | ... | ... |
src/app/main/main.component.ts
... | ... | @@ -28,7 +28,7 @@ import {DateFormat} from "../shared/pipes/date-format.filter"; |
28 | 28 | |
29 | 29 | import {AuthService} from "../login/auth.service"; |
30 | 30 | import {SessionService} from "../login/session.service"; |
31 | - | |
31 | +import {EnvironmentService} from "./../../lib/ng-noosfero-api/http/environment.service"; | |
32 | 32 | import {NotificationService} from "../shared/services/notification.service"; |
33 | 33 | |
34 | 34 | import {BodyStateClassesService} from "./../layout/services/body-state-classes.service"; |
... | ... | @@ -113,7 +113,12 @@ export class EnvironmentContent { |
113 | 113 | name: 'main', |
114 | 114 | resolve: { |
115 | 115 | currentUser: function(AuthService: AuthService) { |
116 | + console.log("RESOLVING USER..."); | |
116 | 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 "ng-forward"; |
4 | 4 | @Inject("Restangular", "$q") |
5 | 5 | export class EnvironmentService { |
6 | 6 | |
7 | - private _currentEnvironmentPromise: ng.IDeferred<noosfero.Environment>; | |
8 | 7 | |
8 | + private currentEnvironment: noosfero.Environment = null; | |
9 | 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 | 17 | getEnvironmentPeople(params: any): ng.IPromise<noosfero.Person[]> { |
14 | 18 | let p = this.restangular.one('people').get(params); |
15 | 19 | let deferred = this.$q.defer<noosfero.Person[]>(); |
... | ... | @@ -18,10 +22,19 @@ export class EnvironmentService { |
18 | 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 | 26 | let p = this.restangular.one('environment').customGET(identifier); |
23 | 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 | 38 | p.catch(this.getHandleErrorFunction<noosfero.Environment>(deferred)); |
26 | 39 | return deferred.promise; |
27 | 40 | } |
... | ... | @@ -37,7 +50,7 @@ export class EnvironmentService { |
37 | 50 | /** TODO - Please, use the base class RestangularService |
38 | 51 | * (description) |
39 | 52 | * |
40 | - * @template T | |
53 | + * @template T_currentEnvironmentPromise | |
41 | 54 | * @param {ng.IDeferred<T>} deferred (description) |
42 | 55 | * @returns {(response: restangular.IResponse) => void} (description) |
43 | 56 | */ | ... | ... |