Commit ffc634cb5c1fed89ca9c42f3b63778756b3664c4

Authored by Victor Costa
1 parent 7c38f4b5

Refactor methods to close task

src/app/task/task-list/task-list.component.spec.ts
... ... @@ -71,7 +71,7 @@ describe("Components", () => {
71 71 it("call cancel and emit event when accept was called successfully", () => {
72 72 helper.component.currentTask = <any>{ id: 1 };
73 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 75 helper.component.cancel = jasmine.createSpy("cancel");
76 76 helper.component.callAccept();
77 77 expect(helper.component.cancel).toHaveBeenCalled();
... ... @@ -81,7 +81,7 @@ describe(&quot;Components&quot;, () =&gt; {
81 81 it("call cancel and emit event when reject was called successfully", () => {
82 82 helper.component.currentTask = <any>{ id: 1 };
83 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 85 helper.component.cancel = jasmine.createSpy("cancel");
86 86 helper.component.callReject();
87 87 expect(helper.component.cancel).toHaveBeenCalled();
... ...
src/app/task/task-list/task-list.component.ts
... ... @@ -51,50 +51,41 @@ export class TaskListComponent {
51 51 }
52 52  
53 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 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 62 this.currentTask = task;
71 63 this.confirmationTask = <any>{ id: task.id };
72   - if (task.reject_details) {
  64 + if (hasDetails) {
73 65 this.modalInstance = this.$uibModal.open({
74   - templateUrl: "app/task/task-list/reject.html",
  66 + templateUrl: templateUrl,
75 67 controller: TaskListComponent,
76 68 controllerAs: 'modal',
77 69 bindToController: true,
78 70 scope: this.$scope
79 71 });
80 72 } else {
81   - this.callReject();
  73 + confirmationFunction();
82 74 }
83 75 }
84 76  
85 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 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 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 89 }).finally(() => {
99 90 this.cancel();
100 91 });
... ...
src/lib/ng-noosfero-api/http/task.service.ts
... ... @@ -34,7 +34,7 @@ export class TaskService extends RestangularService&lt;noosfero.Task&gt; {
34 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 38 let element = this.getElement(task.id);
39 39 delete task.id;
40 40 let put = element.customPUT({ task: task }, action);
... ...