From ecac812cf39ab95ea985ddb160b8f9fc43062010 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 26 Apr 2016 15:35:48 -0300 Subject: [PATCH] Add cancel button in basic editor --- src/app/article/basic-editor/basic-editor.component.spec.ts | 28 ++++++++++++++++++++++++---- src/app/article/basic-editor/basic-editor.component.ts | 20 +++++++++++++++++--- src/app/article/basic-editor/basic-editor.html | 1 + 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/app/article/basic-editor/basic-editor.component.spec.ts b/src/app/article/basic-editor/basic-editor.component.spec.ts index 6d671df..72a971b 100644 --- a/src/app/article/basic-editor/basic-editor.component.spec.ts +++ b/src/app/article/basic-editor/basic-editor.component.spec.ts @@ -20,11 +20,13 @@ describe("Article BasicEditor", () => { })); beforeEach(() => { - $state = jasmine.createSpyObj("$state", ["transitionTo"]); - $stateParams = jasmine.createSpyObj("$stateParams", ["parent_id"]); + $state = jasmine.createSpyObj("$state", ["go"]); + $stateParams = jasmine.createSpyObj("$stateParams", ["parent_id", "profile"]); notification = jasmine.createSpyObj("notification", ["success"]); profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier"]); - articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent"]); + articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "get"]); + + $stateParams.profile = jasmine.createSpy("profile").and.returnValue("profile"); let setCurrentProfileByIdentifierResponse = $q.defer(); setCurrentProfileByIdentifierResponse.resolve(profile); @@ -32,8 +34,12 @@ describe("Article BasicEditor", () => { let articleCreate = $q.defer(); articleCreate.resolve({ data: { path: "path", profile: { identifier: "profile" } } }); + let articleGet = $q.defer(); + articleGet.resolve({ data: { path: "parent-path", profile: { identifier: "profile" } } }); + profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise); articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise); + articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise); }); it("create an article in the current profile when save", done => { @@ -49,9 +55,23 @@ describe("Article BasicEditor", () => { let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); component.save(); $rootScope.$apply(); - expect($state.transitionTo).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); + expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); expect(notification.success).toHaveBeenCalled(); done(); }); + it("got to the parent article page when cancelled", done => { + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); + $rootScope.$apply(); + component.cancel(); + expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "parent-path", profile: $stateParams.profile }); + done(); + }); + + it("got to the profile home when cancelled and parent was not defined", done => { + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); + component.cancel(); + expect($state.go).toHaveBeenCalledWith("main.profile.home", { profile: $stateParams.profile }); + done(); + }); }); diff --git a/src/app/article/basic-editor/basic-editor.component.ts b/src/app/article/basic-editor/basic-editor.component.ts index ee41ce5..a0d7206 100644 --- a/src/app/article/basic-editor/basic-editor.component.ts +++ b/src/app/article/basic-editor/basic-editor.component.ts @@ -16,8 +16,9 @@ import {NotificationService} from "../../shared/services/notification.service.ts export class BasicEditorComponent { article: noosfero.Article = {}; + parent: noosfero.Article = {}; parentId: number; - + profileIdentifier: string; editorOptions = {}; constructor(private articleService: ArticleService, @@ -25,17 +26,30 @@ export class BasicEditorComponent { private $state: ng.ui.IStateService, private notification: NotificationService, private $stateParams: ng.ui.IStateParamsService) { + this.parentId = this.$stateParams['parent_id']; + this.profileIdentifier = this.$stateParams["profile"]; + this.articleService.get(this.parentId).then((result: noosfero.RestResult) => { + this.parent = result.data; + }); } save() { - this.profileService.setCurrentProfileByIdentifier(this.$stateParams["profile"]).then((profile: noosfero.Profile) => { + this.profileService.setCurrentProfileByIdentifier(this.profileIdentifier).then((profile: noosfero.Profile) => { return this.articleService.createInParent(this.parentId, this.article); }).then((response: noosfero.RestResult) => { let article = (response.data); - this.$state.transitionTo('main.profile.page', { page: article.path, profile: article.profile.identifier }); + this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier }); this.notification.success({ title: "Good job!", message: "Article saved!" }); }); } + cancel() { + if (this.parent && this.parent.path) { + this.$state.go('main.profile.page', { page: this.parent.path, profile: this.profileIdentifier }); + } else { + this.$state.go('main.profile.home', { profile: this.profileIdentifier }); + } + } + } diff --git a/src/app/article/basic-editor/basic-editor.html b/src/app/article/basic-editor/basic-editor.html index 518c4e0..1b3cf7a 100644 --- a/src/app/article/basic-editor/basic-editor.html +++ b/src/app/article/basic-editor/basic-editor.html @@ -11,6 +11,7 @@ +
-- libgit2 0.21.2