Commit 33ae65dd065031b9dce588aefe74404a60d418e3
1 parent
ed2107bd
Exists in
master
and in
27 other branches
Adapt basic editor to edit an existing article
Showing
5 changed files
with
72 additions
and
27 deletions
Show diff stats
src/app/article/basic-editor/basic-editor.component.spec.ts
| ... | ... | @@ -10,6 +10,7 @@ describe("Article BasicEditor", () => { |
| 10 | 10 | let profileServiceMock: any; |
| 11 | 11 | let $state: any; |
| 12 | 12 | let $stateParams: any; |
| 13 | + let $window: any; | |
| 13 | 14 | let profile = { id: 1 }; |
| 14 | 15 | let notification: any; |
| 15 | 16 | |
| ... | ... | @@ -20,13 +21,13 @@ describe("Article BasicEditor", () => { |
| 20 | 21 | })); |
| 21 | 22 | |
| 22 | 23 | beforeEach(() => { |
| 24 | + $window = jasmine.createSpyObj("$window", ["back"]); | |
| 23 | 25 | $state = jasmine.createSpyObj("$state", ["go"]); |
| 24 | - $stateParams = jasmine.createSpyObj("$stateParams", ["parent_id", "profile"]); | |
| 25 | 26 | notification = jasmine.createSpyObj("notification", ["success"]); |
| 26 | 27 | profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier"]); |
| 27 | - articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "get"]); | |
| 28 | + articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "updateArticle", "get"]); | |
| 28 | 29 | |
| 29 | - $stateParams.profile = jasmine.createSpy("profile").and.returnValue("profile"); | |
| 30 | + $stateParams = { profile: "profile" }; | |
| 30 | 31 | |
| 31 | 32 | let setCurrentProfileByIdentifierResponse = $q.defer(); |
| 32 | 33 | setCurrentProfileByIdentifierResponse.resolve(profile); |
| ... | ... | @@ -39,20 +40,22 @@ describe("Article BasicEditor", () => { |
| 39 | 40 | |
| 40 | 41 | profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise); |
| 41 | 42 | articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise); |
| 43 | + articleServiceMock.updateArticle = jasmine.createSpy("updateArticle").and.returnValue(articleCreate.promise); | |
| 42 | 44 | articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise); |
| 43 | 45 | }); |
| 44 | 46 | |
| 45 | 47 | it("create an article in the current profile when save", done => { |
| 46 | - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); | |
| 48 | + $stateParams['parent_id'] = 1; | |
| 49 | + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); | |
| 47 | 50 | component.save(); |
| 48 | 51 | $rootScope.$apply(); |
| 49 | 52 | expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); |
| 50 | - expect(articleServiceMock.createInParent).toHaveBeenCalledWith($stateParams.parent_id, component.article); | |
| 53 | + expect(articleServiceMock.createInParent).toHaveBeenCalledWith(1, component.article); | |
| 51 | 54 | done(); |
| 52 | 55 | }); |
| 53 | 56 | |
| 54 | 57 | it("got to the new article page and display an alert when saving sucessfully", done => { |
| 55 | - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); | |
| 58 | + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); | |
| 56 | 59 | component.save(); |
| 57 | 60 | $rootScope.$apply(); |
| 58 | 61 | expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); |
| ... | ... | @@ -60,18 +63,22 @@ describe("Article BasicEditor", () => { |
| 60 | 63 | done(); |
| 61 | 64 | }); |
| 62 | 65 | |
| 63 | - it("got to the parent article page when cancelled", done => { | |
| 64 | - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); | |
| 65 | - $rootScope.$apply(); | |
| 66 | + it("go back when cancel article edition", done => { | |
| 67 | + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); | |
| 68 | + $window.history = { back: jasmine.createSpy('back') }; | |
| 66 | 69 | component.cancel(); |
| 67 | - expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "parent-path", profile: $stateParams.profile }); | |
| 70 | + expect($window.history.back).toHaveBeenCalled(); | |
| 68 | 71 | done(); |
| 69 | 72 | }); |
| 70 | 73 | |
| 71 | - it("got to the profile home when cancelled and parent was not defined", done => { | |
| 72 | - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); | |
| 73 | - component.cancel(); | |
| 74 | - expect($state.go).toHaveBeenCalledWith("main.profile.home", { profile: $stateParams.profile }); | |
| 74 | + it("edit existing article when save", done => { | |
| 75 | + $stateParams['parent_id'] = null; | |
| 76 | + $stateParams['id'] = 2; | |
| 77 | + let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); | |
| 78 | + component.save(); | |
| 79 | + $rootScope.$apply(); | |
| 80 | + expect(articleServiceMock.updateArticle).toHaveBeenCalledWith(component.article); | |
| 75 | 81 | done(); |
| 76 | 82 | }); |
| 83 | + | |
| 77 | 84 | }); | ... | ... |
src/app/article/basic-editor/basic-editor.component.ts
| ... | ... | @@ -12,11 +12,13 @@ import {NotificationService} from "../../shared/services/notification.service.ts |
| 12 | 12 | provide('notification', { useClass: NotificationService }) |
| 13 | 13 | ] |
| 14 | 14 | }) |
| 15 | -@Inject(ArticleService, ProfileService, "$state", NotificationService, "$stateParams") | |
| 15 | +@Inject(ArticleService, ProfileService, "$state", NotificationService, "$stateParams", "$window") | |
| 16 | 16 | export class BasicEditorComponent { |
| 17 | 17 | |
| 18 | - article: noosfero.Article = <noosfero.Article>{}; | |
| 18 | + article: noosfero.Article = <noosfero.Article>{ type: "TextArticle" }; | |
| 19 | 19 | parent: noosfero.Article = <noosfero.Article>{}; |
| 20 | + | |
| 21 | + id: number; | |
| 20 | 22 | parentId: number; |
| 21 | 23 | profileIdentifier: string; |
| 22 | 24 | editorOptions = {}; |
| ... | ... | @@ -25,18 +27,33 @@ export class BasicEditorComponent { |
| 25 | 27 | private profileService: ProfileService, |
| 26 | 28 | private $state: ng.ui.IStateService, |
| 27 | 29 | private notification: NotificationService, |
| 28 | - private $stateParams: ng.ui.IStateParamsService) { | |
| 30 | + private $stateParams: ng.ui.IStateParamsService, | |
| 31 | + private $window: ng.IWindowService) { | |
| 29 | 32 | |
| 30 | 33 | this.parentId = this.$stateParams['parent_id']; |
| 31 | 34 | this.profileIdentifier = this.$stateParams["profile"]; |
| 32 | - this.articleService.get(this.parentId).then((result: noosfero.RestResult<noosfero.Article>) => { | |
| 33 | - this.parent = result.data; | |
| 34 | - }); | |
| 35 | + this.id = this.$stateParams['id']; | |
| 36 | + | |
| 37 | + if (this.parentId) { | |
| 38 | + this.articleService.get(this.parentId).then((result: noosfero.RestResult<noosfero.Article>) => { | |
| 39 | + this.parent = result.data; | |
| 40 | + }); | |
| 41 | + } | |
| 42 | + if (this.id) { | |
| 43 | + this.articleService.get(this.id).then((result: noosfero.RestResult<noosfero.Article>) => { | |
| 44 | + this.article = result.data; | |
| 45 | + this.article.name = this.article.title; // FIXME | |
| 46 | + }); | |
| 47 | + } | |
| 35 | 48 | } |
| 36 | 49 | |
| 37 | 50 | save() { |
| 38 | 51 | this.profileService.setCurrentProfileByIdentifier(this.profileIdentifier).then((profile: noosfero.Profile) => { |
| 39 | - return this.articleService.createInParent(this.parentId, this.article); | |
| 52 | + if (this.id) { | |
| 53 | + return this.articleService.updateArticle(this.article); | |
| 54 | + } else { | |
| 55 | + return this.articleService.createInParent(this.parentId, this.article); | |
| 56 | + } | |
| 40 | 57 | }).then((response: noosfero.RestResult<noosfero.Article>) => { |
| 41 | 58 | let article = (<noosfero.Article>response.data); |
| 42 | 59 | this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier }); |
| ... | ... | @@ -45,11 +62,7 @@ export class BasicEditorComponent { |
| 45 | 62 | } |
| 46 | 63 | |
| 47 | 64 | cancel() { |
| 48 | - if (this.parent && this.parent.path) { | |
| 49 | - this.$state.go('main.profile.page', { page: this.parent.path, profile: this.profileIdentifier }); | |
| 50 | - } else { | |
| 51 | - this.$state.go('main.profile.home', { profile: this.profileIdentifier }); | |
| 52 | - } | |
| 65 | + this.$window.history.back(); | |
| 53 | 66 | } |
| 54 | 67 | |
| 55 | 68 | } | ... | ... |
src/app/profile/profile.component.ts
| ... | ... | @@ -57,6 +57,18 @@ import {MyProfileComponent} from "./myprofile.component"; |
| 57 | 57 | } |
| 58 | 58 | }, |
| 59 | 59 | { |
| 60 | + name: 'main.cmsEdit', | |
| 61 | + url: "^/myprofile/:profile/cms/edit/:id", | |
| 62 | + component: BasicEditorComponent, | |
| 63 | + views: { | |
| 64 | + "content": { | |
| 65 | + templateUrl: "app/article/basic-editor/basic-editor.html", | |
| 66 | + controller: BasicEditorComponent, | |
| 67 | + controllerAs: "vm" | |
| 68 | + } | |
| 69 | + } | |
| 70 | + }, | |
| 71 | + { | |
| 60 | 72 | name: 'main.profile.home', |
| 61 | 73 | url: "", |
| 62 | 74 | component: ProfileHomeComponent, | ... | ... |
src/lib/ng-noosfero-api/http/article.service.ts
| ... | ... | @@ -22,6 +22,17 @@ export class ArticleService extends RestangularService<noosfero.Article> { |
| 22 | 22 | }; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | + updateArticle(article: noosfero.Article) { | |
| 26 | + let headers = { | |
| 27 | + 'Content-Type': 'application/json' | |
| 28 | + }; | |
| 29 | + let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>(); | |
| 30 | + let attributesToUpdate: any = { article: { name: article.name, body: article.body } }; | |
| 31 | + let restRequest: ng.IPromise<noosfero.RestResult<noosfero.Article>> = this.getElement(article.id).customPOST(attributesToUpdate, null, null, headers); | |
| 32 | + restRequest.then(this.getHandleSuccessFunction(deferred)) | |
| 33 | + .catch(this.getHandleErrorFunction(deferred)); | |
| 34 | + return deferred.promise; | |
| 35 | + } | |
| 25 | 36 | |
| 26 | 37 | createInProfile(profile: noosfero.Profile, article: noosfero.Article): ng.IPromise<noosfero.RestResult<noosfero.Article>> { |
| 27 | 38 | let profileElement = this.profileService.get(<number>profile.id); |
| ... | ... | @@ -85,5 +96,4 @@ export class ArticleService extends RestangularService<noosfero.Article> { |
| 85 | 96 | } |
| 86 | 97 | |
| 87 | 98 | |
| 88 | - | |
| 89 | 99 | } | ... | ... |