From ffc634cb5c1fed89ca9c42f3b63778756b3664c4 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 28 Jul 2016 10:27:21 -0300 Subject: [PATCH] Refactor methods to close task --- src/app/task/task-list/task-list.component.spec.ts | 4 ++-- src/app/task/task-list/task-list.component.ts | 39 +++++++++++++++------------------------ src/lib/ng-noosfero-api/http/task.service.ts | 2 +- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/app/task/task-list/task-list.component.spec.ts b/src/app/task/task-list/task-list.component.spec.ts index ae0b9a3..6a3ab7b 100644 --- a/src/app/task/task-list/task-list.component.spec.ts +++ b/src/app/task/task-list/task-list.component.spec.ts @@ -71,7 +71,7 @@ describe("Components", () => { it("call cancel and emit event when accept was called successfully", () => { helper.component.currentTask = { id: 1 }; let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); - taskService.finishTask = jasmine.createSpy("finishTask").and.returnValue(result); + taskService.closeTask = jasmine.createSpy("closeTask").and.returnValue(result); helper.component.cancel = jasmine.createSpy("cancel"); helper.component.callAccept(); expect(helper.component.cancel).toHaveBeenCalled(); @@ -81,7 +81,7 @@ describe("Components", () => { it("call cancel and emit event when reject was called successfully", () => { helper.component.currentTask = { id: 1 }; let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); - taskService.cancelTask = jasmine.createSpy("cancelTask").and.returnValue(result); + taskService.closeTask = jasmine.createSpy("closeTask").and.returnValue(result); helper.component.cancel = jasmine.createSpy("cancel"); helper.component.callReject(); expect(helper.component.cancel).toHaveBeenCalled(); diff --git a/src/app/task/task-list/task-list.component.ts b/src/app/task/task-list/task-list.component.ts index 025abdc..5954c62 100644 --- a/src/app/task/task-list/task-list.component.ts +++ b/src/app/task/task-list/task-list.component.ts @@ -51,50 +51,41 @@ export class TaskListComponent { } accept(task: noosfero.Task) { - this.currentTask = task; - this.confirmationTask = { id: task.id }; - if (task.accept_details) { - this.modalInstance = this.$uibModal.open({ - templateUrl: "app/task/task-list/accept.html", - controller: TaskListComponent, - controllerAs: 'modal', - bindToController: true, - scope: this.$scope - }); - } else { - this.callAccept(); - } + this.closeTask(task, task.accept_details, "app/task/task-list/accept.html", () => { this.callAccept(); }); } reject(task: noosfero.Task) { + this.closeTask(task, task.reject_details, "app/task/task-list/reject.html", () => { this.callReject(); }); + } + + private closeTask(task: noosfero.Task, hasDetails: boolean, templateUrl: string, confirmationFunction: Function) { this.currentTask = task; this.confirmationTask = { id: task.id }; - if (task.reject_details) { + if (hasDetails) { this.modalInstance = this.$uibModal.open({ - templateUrl: "app/task/task-list/reject.html", + templateUrl: templateUrl, controller: TaskListComponent, controllerAs: 'modal', bindToController: true, scope: this.$scope }); } else { - this.callReject(); + confirmationFunction(); } } callAccept() { - this.taskService.finishTask(this.confirmationTask).then(() => { - this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask); - this.notificationService.success({ title: "tasks.actions.accept.title", message: "tasks.actions.accept.message" }); - }).finally(() => { - this.cancel(); - }); + this.callCloseTask("finish", "tasks.actions.accept.title", "tasks.actions.accept.message"); } callReject() { - this.taskService.cancelTask(this.confirmationTask).then(() => { + this.callCloseTask("cancel", "tasks.actions.reject.title", "tasks.actions.reject.message"); + } + + private callCloseTask(action: string, title: string, message: string) { + this.taskService.closeTask(this.confirmationTask, action).then(() => { this.eventsHubService.emitEvent(this.eventsNames.TASK_CLOSED, this.currentTask); - this.notificationService.success({ title: "tasks.actions.reject.title", message: "tasks.actions.reject.message" }); + this.notificationService.success({ title: title, message: message }); }).finally(() => { this.cancel(); }); diff --git a/src/lib/ng-noosfero-api/http/task.service.ts b/src/lib/ng-noosfero-api/http/task.service.ts index c74f7d4..2768b2c 100644 --- a/src/lib/ng-noosfero-api/http/task.service.ts +++ b/src/lib/ng-noosfero-api/http/task.service.ts @@ -34,7 +34,7 @@ export class TaskService extends RestangularService { return this.closeTask(task, "cancel"); } - private closeTask(task: noosfero.Task, action: string) { + closeTask(task: noosfero.Task, action: string) { let element = this.getElement(task.id); delete task.id; let put = element.customPUT({ task: task }, action); -- libgit2 0.21.2