Commit 7205a81d4a525daeee24d948e8d8126e6c012872
Committed by
Michel Felipe

1 parent
0354c769
Exists in
master
and in
6 other branches
Emit and subscribe to events for task accept/reject
Showing
11 changed files
with
61 additions
and
22 deletions
Show diff stats
src/app/known-events.ts
1 | 1 | import { EventsHubKnownEventNames } from './shared/services/events-hub.service'; |
2 | 2 | |
3 | -export class NoosferoKnownEvents implements EventsHubKnownEventNames { | |
3 | +export class NoosferoKnownEvents implements EventsHubKnownEventNames { | |
4 | 4 | IMAGE_PROFILE_UPDATED: string = 'IMAGE_PROFILE_UPDATED'; |
5 | 5 | PROFILE_INFO_UPDATED: string = 'PROFILE_INFO_UPDATED'; |
6 | 6 | ARTICLE_UPDATED: string = 'ARTICLE_UPDATED'; |
7 | + TASK_CLOSED: string = 'TASK_CLOSED'; | |
7 | 8 | |
8 | 9 | constructor() { |
9 | 10 | } |
... | ... | @@ -11,4 +12,4 @@ export class NoosferoKnownEvents implements EventsHubKnownEventNames { |
11 | 12 | getNames() { |
12 | 13 | return Object.getOwnPropertyNames(this); |
13 | 14 | } |
14 | -} | |
15 | 15 | \ No newline at end of file |
16 | +} | ... | ... |
src/app/main/main.component.spec.ts
src/app/shared/services/events-hub.service.ts
... | ... | @@ -31,13 +31,13 @@ export class EventsHubService { |
31 | 31 | emitEvent(eventType: string, payload?: any) { |
32 | 32 | this.checkKnownEvent(eventType); |
33 | 33 | let event = this.emitters.get(eventType); |
34 | - if ( event ) this.emitters.get(eventType).next(payload); | |
34 | + if (event) this.emitters.get(eventType).next(payload); | |
35 | 35 | } |
36 | 36 | |
37 | 37 | subscribeToEvent<T>(eventType: string, generatorOrNext?: ((p?: T) => void), error?: any, complete?: any) { |
38 | 38 | this.checkKnownEvent(eventType); |
39 | 39 | let event = this.emitters.get(eventType); |
40 | - if (event) event.subscribe(generatorOrNext, error, complete); | |
40 | + if (event) event.subscribe(generatorOrNext, error, complete); | |
41 | 41 | } |
42 | 42 | |
43 | 43 | private setupEmitters() { |
... | ... | @@ -53,4 +53,4 @@ export class EventsHubService { |
53 | 53 | } |
54 | 54 | |
55 | 55 | |
56 | -} | |
57 | 56 | \ No newline at end of file |
57 | +} | ... | ... |
src/app/task/task-list/task-accept.component.spec.ts
src/app/task/task-list/task-list.component.spec.ts
... | ... | @@ -12,6 +12,7 @@ describe("Components", () => { |
12 | 12 | let taskService = jasmine.createSpyObj("taskService", ["getAllPending"]); |
13 | 13 | let tasks = [{ id: 1 }, { id: 2 }]; |
14 | 14 | let modal = helpers.mocks.$modal; |
15 | + let eventsHubService = jasmine.createSpyObj("eventsHubService", ["subscribeToEvent", "emitEvent"]); | |
15 | 16 | taskService.getAllPending = jasmine.createSpy("getAllPending").and.returnValue(Promise.resolve({ headers: () => { }, data: tasks })); |
16 | 17 | |
17 | 18 | beforeEach(angular.mock.module("templates")); |
... | ... | @@ -22,6 +23,7 @@ describe("Components", () => { |
22 | 23 | directives: [TaskListComponent], |
23 | 24 | providers: [ |
24 | 25 | helpers.createProviderToValue("TaskService", taskService), |
26 | + helpers.createProviderToValue("EventsHubService", eventsHubService), | |
25 | 27 | helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), |
26 | 28 | helpers.createProviderToValue('$uibModal', modal), |
27 | 29 | ].concat(helpers.provideFilters("groupByFilter")), |
... | ... | @@ -66,24 +68,24 @@ describe("Components", () => { |
66 | 68 | expect(helper.component.callReject).toHaveBeenCalled(); |
67 | 69 | }); |
68 | 70 | |
69 | - it("call cancel and remove the current task when accept was called successfully", () => { | |
71 | + it("call cancel and emit event when accept was called successfully", () => { | |
70 | 72 | helper.component.currentTask = <any>{ id: 1 }; |
71 | 73 | let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); |
72 | 74 | taskService.finishTask = jasmine.createSpy("finishTask").and.returnValue(result); |
73 | 75 | helper.component.cancel = jasmine.createSpy("cancel"); |
74 | 76 | helper.component.callAccept(); |
75 | 77 | expect(helper.component.cancel).toHaveBeenCalled(); |
76 | - expect(helper.component.tasks).toEqual([{ id: 2 }]); | |
78 | + expect((<any>helper.component)['eventsHubService'].emitEvent).toHaveBeenCalled(); | |
77 | 79 | }); |
78 | 80 | |
79 | - it("call cancel and remove the current task when reject was called successfully", () => { | |
81 | + it("call cancel and emit event when reject was called successfully", () => { | |
80 | 82 | helper.component.currentTask = <any>{ id: 1 }; |
81 | 83 | let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); |
82 | 84 | taskService.cancelTask = jasmine.createSpy("cancelTask").and.returnValue(result); |
83 | 85 | helper.component.cancel = jasmine.createSpy("cancel"); |
84 | 86 | helper.component.callReject(); |
85 | 87 | expect(helper.component.cancel).toHaveBeenCalled(); |
86 | - expect(helper.component.tasks).toEqual([{ id: 2 }]); | |
88 | + expect((<any>helper.component)['eventsHubService'].emitEvent).toHaveBeenCalled(); | |
87 | 89 | }); |
88 | 90 | |
89 | 91 | it("reset currentTask and close modal when call cancel", () => { | ... | ... |
src/app/task/task-list/task-list.component.ts
1 | -import { Component, Input, Inject } from "ng-forward"; | |
1 | +import { Component, Input, Inject, provide } from "ng-forward"; | |
2 | 2 | import { NotificationService } from "../../shared/services/notification.service"; |
3 | 3 | import { TaskService } from "../../../lib/ng-noosfero-api/http/task.service"; |
4 | 4 | import { TaskAcceptComponent } from "./task-accept.component"; |
5 | 5 | import { Arrays } from "../../../lib/util/arrays"; |
6 | +import { EventsHubService } from "../../shared/services/events-hub.service"; | |
7 | +import { NoosferoKnownEvents } from "../../known-events"; | |
6 | 8 | |
7 | 9 | @Component({ |
8 | 10 | selector: "task-list", |
9 | 11 | templateUrl: "app/task/task-list/task-list.html", |
10 | - directives: [TaskAcceptComponent] | |
12 | + directives: [TaskAcceptComponent], | |
13 | + providers: [ | |
14 | + provide('eventsHubService', { useClass: EventsHubService }) | |
15 | + ] | |
11 | 16 | }) |
12 | -@Inject(NotificationService, "$scope", "$uibModal", TaskService) | |
17 | +@Inject(NotificationService, "$scope", "$uibModal", TaskService, EventsHubService) | |
13 | 18 | export class TaskListComponent { |
14 | 19 | |
15 | 20 | @Input() tasks: noosfero.Task[]; |
... | ... | @@ -18,9 +23,23 @@ export class TaskListComponent { |
18 | 23 | |
19 | 24 | currentTask: noosfero.Task; |
20 | 25 | confirmationTask: noosfero.Task; |
26 | + eventsNames: NoosferoKnownEvents; | |
21 | 27 | private modalInstance: any = null; |
22 | 28 | |
23 | - constructor(private notificationService: NotificationService, private $scope: ng.IScope, private $uibModal: any, private taskService: TaskService) { } | |
29 | + constructor(private notificationService: NotificationService, | |
30 | + private $scope: ng.IScope, | |
31 | + private $uibModal: any, | |
32 | + private taskService: TaskService, | |
33 | + private eventsHubService: EventsHubService) { | |
34 | + | |
35 | + this.eventsNames = new NoosferoKnownEvents(); | |
36 | + } | |
37 | + | |
38 | + ngOnInit() { | |
39 | + this.eventsHubService.subscribeToEvent(this.eventsNames.TASK_CLOSED, (task: noosfero.Task) => { | |
40 | + Arrays.remove(this.tasks, task); | |
41 | + }); | |
42 | + } | |
24 | 43 | |
25 | 44 | getTaskTemplate(task: noosfero.Task) { |
26 | 45 | if (this.taskTemplates.indexOf(task.type) >= 0) { |
... | ... | @@ -65,7 +84,7 @@ export class TaskListComponent { |
65 | 84 | |
66 | 85 | callAccept() { |
67 | 86 | this.taskService.finishTask(this.confirmationTask).then(() => { |
68 | - Arrays.remove(this.tasks, this.currentTask); | |
87 | + this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask); | |
69 | 88 | this.notificationService.success({ title: "tasks.actions.accept.title", message: "tasks.actions.accept.message" }); |
70 | 89 | }).finally(() => { |
71 | 90 | this.cancel(); |
... | ... | @@ -74,7 +93,7 @@ export class TaskListComponent { |
74 | 93 | |
75 | 94 | callReject() { |
76 | 95 | this.taskService.cancelTask(this.confirmationTask).then(() => { |
77 | - Arrays.remove(this.tasks, this.currentTask); | |
96 | + this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask); | |
78 | 97 | this.notificationService.success({ title: "tasks.actions.reject.title", message: "tasks.actions.reject.message" }); |
79 | 98 | }).finally(() => { |
80 | 99 | this.cancel(); | ... | ... |
src/app/task/tasks-menu/tasks-menu.component.spec.ts
... | ... | @@ -12,6 +12,7 @@ describe("Components", () => { |
12 | 12 | let helper: ComponentTestHelper<TasksMenuComponent>; |
13 | 13 | let taskService = jasmine.createSpyObj("taskService", ["getAllPending"]); |
14 | 14 | let tasks = [{ id: 1 }, { id: 2 }]; |
15 | + let eventsHubService = jasmine.createSpyObj("eventsHubService", ["subscribeToEvent", "emitEvent"]); | |
15 | 16 | taskService.getAllPending = jasmine.createSpy("getAllPending").and.returnValue(Promise.resolve({ headers: () => { }, data: tasks })); |
16 | 17 | |
17 | 18 | beforeEach(angular.mock.module("templates")); |
... | ... | @@ -22,6 +23,7 @@ describe("Components", () => { |
22 | 23 | directives: [TasksMenuComponent], |
23 | 24 | providers: [ |
24 | 25 | helpers.createProviderToValue("TaskService", taskService), |
26 | + helpers.createProviderToValue("EventsHubService", eventsHubService), | |
25 | 27 | helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({})) |
26 | 28 | ] |
27 | 29 | }); | ... | ... |
src/app/task/tasks-menu/tasks-menu.component.ts
1 | 1 | import { Component, Inject } from "ng-forward"; |
2 | 2 | import { TaskService } from "../../../lib/ng-noosfero-api/http/task.service"; |
3 | 3 | import { AuthService, SessionService, AuthEvents } from "./../../login"; |
4 | +import { EventsHubService } from "../../shared/services/events-hub.service"; | |
5 | +import { NoosferoKnownEvents } from "../../known-events"; | |
4 | 6 | |
5 | 7 | @Component({ |
6 | 8 | selector: "tasks-menu", |
7 | 9 | templateUrl: "app/task/tasks-menu/tasks-menu.html" |
8 | 10 | }) |
9 | -@Inject(TaskService, SessionService, AuthService) | |
11 | +@Inject(TaskService, SessionService, AuthService, EventsHubService) | |
10 | 12 | export class TasksMenuComponent { |
11 | 13 | |
12 | 14 | tasks: noosfero.Task[]; |
13 | 15 | total: number; |
14 | 16 | perPage = 5; |
15 | 17 | person: noosfero.Person; |
18 | + eventsNames: NoosferoKnownEvents; | |
16 | 19 | |
17 | - constructor(private taskService: TaskService, private session: SessionService, private authService: AuthService) { } | |
20 | + constructor(private taskService: TaskService, | |
21 | + private session: SessionService, | |
22 | + private authService: AuthService, | |
23 | + private eventsHubService: EventsHubService) { | |
24 | + | |
25 | + this.eventsNames = new NoosferoKnownEvents(); | |
26 | + } | |
18 | 27 | |
19 | 28 | ngOnInit() { |
29 | + this.eventsHubService.subscribeToEvent(this.eventsNames.TASK_CLOSED, (task: noosfero.Task) => { | |
30 | + this.total--; | |
31 | + }); | |
20 | 32 | this.authService.subscribe(AuthEvents[AuthEvents.loginSuccess], () => { |
21 | 33 | this.loadTasks(); |
22 | 34 | }); | ... | ... |
src/app/task/types/add-member/add-member-accept.html
1 | 1 | <div class="add-member-details"> |
2 | - <label>Select Roles:</label> | |
2 | + <label>{{"tasks.add_member.accept.select_role" | translate}}</label> | |
3 | 3 | <div class="form-group roles"> |
4 | 4 | <div class="checkbox" ng-repeat="role in ctrl.roles"> |
5 | 5 | <input type="checkbox" ng-click="ctrl.toggleSelection(role)"> {{role.name}} | ... | ... |
src/languages/en.json
... | ... | @@ -139,5 +139,6 @@ |
139 | 139 | "tasks.actions.reject.title": "Good job!", |
140 | 140 | "tasks.actions.accept.message": "Task Accepted", |
141 | 141 | "tasks.actions.reject.message": "Task Rejected", |
142 | - "tasks.actions.reject.explanation.label": "Rejection explanation" | |
142 | + "tasks.actions.reject.explanation.label": "Rejection explanation", | |
143 | + "tasks.add_member.accept.select_role": "Select Roles:" | |
143 | 144 | } | ... | ... |
src/languages/pt.json
... | ... | @@ -142,5 +142,6 @@ |
142 | 142 | "tasks.actions.reject.title": "Bom trabalho!", |
143 | 143 | "tasks.actions.accept.message": "Tarefa Aceita", |
144 | 144 | "tasks.actions.reject.message": "Tarefa Rejeitada", |
145 | - "tasks.actions.reject.explanation.label": "Motivo da rejeição" | |
145 | + "tasks.actions.reject.explanation.label": "Motivo da rejeição", | |
146 | + "tasks.add_member.accept.select_role": "Selecionar papéis:" | |
146 | 147 | } | ... | ... |