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 72a971b..7673a8e 100644 --- a/src/app/article/basic-editor/basic-editor.component.spec.ts +++ b/src/app/article/basic-editor/basic-editor.component.spec.ts @@ -10,6 +10,7 @@ describe("Article BasicEditor", () => { let profileServiceMock: any; let $state: any; let $stateParams: any; + let $window: any; let profile = { id: 1 }; let notification: any; @@ -20,13 +21,13 @@ describe("Article BasicEditor", () => { })); beforeEach(() => { + $window = jasmine.createSpyObj("$window", ["back"]); $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", "get"]); + articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "updateArticle", "get"]); - $stateParams.profile = jasmine.createSpy("profile").and.returnValue("profile"); + $stateParams = { profile: "profile" }; let setCurrentProfileByIdentifierResponse = $q.defer(); setCurrentProfileByIdentifierResponse.resolve(profile); @@ -39,20 +40,22 @@ describe("Article BasicEditor", () => { profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise); articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise); + articleServiceMock.updateArticle = jasmine.createSpy("updateArticle").and.returnValue(articleCreate.promise); articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise); }); it("create an article in the current profile when save", done => { - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); + $stateParams['parent_id'] = 1; + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); component.save(); $rootScope.$apply(); expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); - expect(articleServiceMock.createInParent).toHaveBeenCalledWith($stateParams.parent_id, component.article); + expect(articleServiceMock.createInParent).toHaveBeenCalledWith(1, component.article); done(); }); it("got to the new article page and display an alert when saving sucessfully", done => { - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); component.save(); $rootScope.$apply(); expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); @@ -60,18 +63,22 @@ describe("Article BasicEditor", () => { done(); }); - it("got to the parent article page when cancelled", done => { - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); - $rootScope.$apply(); + it("go back when cancel article edition", done => { + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); + $window.history = { back: jasmine.createSpy('back') }; component.cancel(); - expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "parent-path", profile: $stateParams.profile }); + expect($window.history.back).toHaveBeenCalled(); 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 }); + it("edit existing article when save", done => { + $stateParams['parent_id'] = null; + $stateParams['id'] = 2; + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); + component.save(); + $rootScope.$apply(); + expect(articleServiceMock.updateArticle).toHaveBeenCalledWith(component.article); done(); }); + }); diff --git a/src/app/article/basic-editor/basic-editor.component.ts b/src/app/article/basic-editor/basic-editor.component.ts index 4530f85..1044818 100644 --- a/src/app/article/basic-editor/basic-editor.component.ts +++ b/src/app/article/basic-editor/basic-editor.component.ts @@ -12,11 +12,13 @@ import {NotificationService} from "../../shared/services/notification.service.ts provide('notification', { useClass: NotificationService }) ] }) -@Inject(ArticleService, ProfileService, "$state", NotificationService, "$stateParams") +@Inject(ArticleService, ProfileService, "$state", NotificationService, "$stateParams", "$window") export class BasicEditorComponent { - article: noosfero.Article = {}; + article: noosfero.Article = { type: "TextArticle" }; parent: noosfero.Article = {}; + + id: number; parentId: number; profileIdentifier: string; editorOptions = {}; @@ -25,18 +27,33 @@ export class BasicEditorComponent { private profileService: ProfileService, private $state: ng.ui.IStateService, private notification: NotificationService, - private $stateParams: ng.ui.IStateParamsService) { + private $stateParams: ng.ui.IStateParamsService, + private $window: ng.IWindowService) { this.parentId = this.$stateParams['parent_id']; this.profileIdentifier = this.$stateParams["profile"]; - this.articleService.get(this.parentId).then((result: noosfero.RestResult) => { - this.parent = result.data; - }); + this.id = this.$stateParams['id']; + + if (this.parentId) { + this.articleService.get(this.parentId).then((result: noosfero.RestResult) => { + this.parent = result.data; + }); + } + if (this.id) { + this.articleService.get(this.id).then((result: noosfero.RestResult) => { + this.article = result.data; + this.article.name = this.article.title; // FIXME + }); + } } save() { this.profileService.setCurrentProfileByIdentifier(this.profileIdentifier).then((profile: noosfero.Profile) => { - return this.articleService.createInParent(this.parentId, this.article); + if (this.id) { + return this.articleService.updateArticle(this.article); + } else { + return this.articleService.createInParent(this.parentId, this.article); + } }).then((response: noosfero.RestResult) => { let article = (response.data); this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier }); @@ -45,11 +62,7 @@ export class BasicEditorComponent { } 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 }); - } + this.$window.history.back(); } } diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index c858646..f416ceb 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -57,6 +57,18 @@ import {MyProfileComponent} from "./myprofile.component"; } }, { + name: 'main.cmsEdit', + url: "^/myprofile/:profile/cms/edit/:id", + component: BasicEditorComponent, + views: { + "content": { + templateUrl: "app/article/basic-editor/basic-editor.html", + controller: BasicEditorComponent, + controllerAs: "vm" + } + } + }, + { name: 'main.profile.home', url: "", component: ProfileHomeComponent, diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index 1859109..d4fe67a 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -22,6 +22,17 @@ export class ArticleService extends RestangularService { }; } + updateArticle(article: noosfero.Article) { + let headers = { + 'Content-Type': 'application/json' + }; + let deferred = this.$q.defer>(); + let attributesToUpdate: any = { article: { name: article.name, body: article.body } }; + let restRequest: ng.IPromise> = this.getElement(article.id).customPOST(attributesToUpdate, null, null, headers); + restRequest.then(this.getHandleSuccessFunction(deferred)) + .catch(this.getHandleErrorFunction(deferred)); + return deferred.promise; + } createInProfile(profile: noosfero.Profile, article: noosfero.Article): ng.IPromise> { let profileElement = this.profileService.get(profile.id); @@ -85,5 +96,4 @@ export class ArticleService extends RestangularService { } - } diff --git a/src/lib/ng-noosfero-api/interfaces/article.ts b/src/lib/ng-noosfero-api/interfaces/article.ts index 9a70d40..bda2ecd 100644 --- a/src/lib/ng-noosfero-api/interfaces/article.ts +++ b/src/lib/ng-noosfero-api/interfaces/article.ts @@ -5,5 +5,8 @@ namespace noosfero { profile: Profile; type: string; parent: Article; + body: string; + title: string; + name: string; } } -- libgit2 0.21.2