Commit ffc634cb5c1fed89ca9c42f3b63778756b3664c4
1 parent
7c38f4b5
Refactor methods to close task
Showing
3 changed files
with
18 additions
and
27 deletions
Show diff stats
src/app/task/task-list/task-list.component.spec.ts
@@ -71,7 +71,7 @@ describe("Components", () => { | @@ -71,7 +71,7 @@ describe("Components", () => { | ||
71 | it("call cancel and emit event when accept was called successfully", () => { | 71 | it("call cancel and emit event when accept was called successfully", () => { |
72 | helper.component.currentTask = <any>{ id: 1 }; | 72 | helper.component.currentTask = <any>{ id: 1 }; |
73 | let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); | 73 | let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); |
74 | - taskService.finishTask = jasmine.createSpy("finishTask").and.returnValue(result); | 74 | + taskService.closeTask = jasmine.createSpy("closeTask").and.returnValue(result); |
75 | helper.component.cancel = jasmine.createSpy("cancel"); | 75 | helper.component.cancel = jasmine.createSpy("cancel"); |
76 | helper.component.callAccept(); | 76 | helper.component.callAccept(); |
77 | expect(helper.component.cancel).toHaveBeenCalled(); | 77 | expect(helper.component.cancel).toHaveBeenCalled(); |
@@ -81,7 +81,7 @@ describe("Components", () => { | @@ -81,7 +81,7 @@ describe("Components", () => { | ||
81 | it("call cancel and emit event when reject was called successfully", () => { | 81 | it("call cancel and emit event when reject was called successfully", () => { |
82 | helper.component.currentTask = <any>{ id: 1 }; | 82 | helper.component.currentTask = <any>{ id: 1 }; |
83 | let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); | 83 | let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); |
84 | - taskService.cancelTask = jasmine.createSpy("cancelTask").and.returnValue(result); | 84 | + taskService.closeTask = jasmine.createSpy("closeTask").and.returnValue(result); |
85 | helper.component.cancel = jasmine.createSpy("cancel"); | 85 | helper.component.cancel = jasmine.createSpy("cancel"); |
86 | helper.component.callReject(); | 86 | helper.component.callReject(); |
87 | expect(helper.component.cancel).toHaveBeenCalled(); | 87 | expect(helper.component.cancel).toHaveBeenCalled(); |
src/app/task/task-list/task-list.component.ts
@@ -51,50 +51,41 @@ export class TaskListComponent { | @@ -51,50 +51,41 @@ export class TaskListComponent { | ||
51 | } | 51 | } |
52 | 52 | ||
53 | accept(task: noosfero.Task) { | 53 | accept(task: noosfero.Task) { |
54 | - this.currentTask = task; | ||
55 | - this.confirmationTask = <any>{ id: task.id }; | ||
56 | - if (task.accept_details) { | ||
57 | - this.modalInstance = this.$uibModal.open({ | ||
58 | - templateUrl: "app/task/task-list/accept.html", | ||
59 | - controller: TaskListComponent, | ||
60 | - controllerAs: 'modal', | ||
61 | - bindToController: true, | ||
62 | - scope: this.$scope | ||
63 | - }); | ||
64 | - } else { | ||
65 | - this.callAccept(); | ||
66 | - } | 54 | + this.closeTask(task, task.accept_details, "app/task/task-list/accept.html", () => { this.callAccept(); }); |
67 | } | 55 | } |
68 | 56 | ||
69 | reject(task: noosfero.Task) { | 57 | reject(task: noosfero.Task) { |
58 | + this.closeTask(task, task.reject_details, "app/task/task-list/reject.html", () => { this.callReject(); }); | ||
59 | + } | ||
60 | + | ||
61 | + private closeTask(task: noosfero.Task, hasDetails: boolean, templateUrl: string, confirmationFunction: Function) { | ||
70 | this.currentTask = task; | 62 | this.currentTask = task; |
71 | this.confirmationTask = <any>{ id: task.id }; | 63 | this.confirmationTask = <any>{ id: task.id }; |
72 | - if (task.reject_details) { | 64 | + if (hasDetails) { |
73 | this.modalInstance = this.$uibModal.open({ | 65 | this.modalInstance = this.$uibModal.open({ |
74 | - templateUrl: "app/task/task-list/reject.html", | 66 | + templateUrl: templateUrl, |
75 | controller: TaskListComponent, | 67 | controller: TaskListComponent, |
76 | controllerAs: 'modal', | 68 | controllerAs: 'modal', |
77 | bindToController: true, | 69 | bindToController: true, |
78 | scope: this.$scope | 70 | scope: this.$scope |
79 | }); | 71 | }); |
80 | } else { | 72 | } else { |
81 | - this.callReject(); | 73 | + confirmationFunction(); |
82 | } | 74 | } |
83 | } | 75 | } |
84 | 76 | ||
85 | callAccept() { | 77 | callAccept() { |
86 | - this.taskService.finishTask(this.confirmationTask).then(() => { | ||
87 | - this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask); | ||
88 | - this.notificationService.success({ title: "tasks.actions.accept.title", message: "tasks.actions.accept.message" }); | ||
89 | - }).finally(() => { | ||
90 | - this.cancel(); | ||
91 | - }); | 78 | + this.callCloseTask("finish", "tasks.actions.accept.title", "tasks.actions.accept.message"); |
92 | } | 79 | } |
93 | 80 | ||
94 | callReject() { | 81 | callReject() { |
95 | - this.taskService.cancelTask(this.confirmationTask).then(() => { | 82 | + this.callCloseTask("cancel", "tasks.actions.reject.title", "tasks.actions.reject.message"); |
83 | + } | ||
84 | + | ||
85 | + private callCloseTask(action: string, title: string, message: string) { | ||
86 | + this.taskService.closeTask(this.confirmationTask, action).then(() => { | ||
96 | this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask); | 87 | this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask); |
97 | - this.notificationService.success({ title: "tasks.actions.reject.title", message: "tasks.actions.reject.message" }); | 88 | + this.notificationService.success({ title: title, message: message }); |
98 | }).finally(() => { | 89 | }).finally(() => { |
99 | this.cancel(); | 90 | this.cancel(); |
100 | }); | 91 | }); |
src/lib/ng-noosfero-api/http/task.service.ts
@@ -34,7 +34,7 @@ export class TaskService extends RestangularService<noosfero.Task> { | @@ -34,7 +34,7 @@ export class TaskService extends RestangularService<noosfero.Task> { | ||
34 | return this.closeTask(task, "cancel"); | 34 | return this.closeTask(task, "cancel"); |
35 | } | 35 | } |
36 | 36 | ||
37 | - private closeTask(task: noosfero.Task, action: string) { | 37 | + closeTask(task: noosfero.Task, action: string) { |
38 | let element = this.getElement(task.id); | 38 | let element = this.getElement(task.id); |
39 | delete task.id; | 39 | delete task.id; |
40 | let put = element.customPUT({ task: task }, action); | 40 | let put = element.customPUT({ task: task }, action); |