Commit 33ae65dd065031b9dce588aefe74404a60d418e3

Authored by Victor Costa
1 parent ed2107bd

Adapt basic editor to edit an existing article

src/app/article/basic-editor/basic-editor.component.spec.ts
@@ -10,6 +10,7 @@ describe("Article BasicEditor", () => { @@ -10,6 +10,7 @@ describe("Article BasicEditor", () => {
10 let profileServiceMock: any; 10 let profileServiceMock: any;
11 let $state: any; 11 let $state: any;
12 let $stateParams: any; 12 let $stateParams: any;
  13 + let $window: any;
13 let profile = { id: 1 }; 14 let profile = { id: 1 };
14 let notification: any; 15 let notification: any;
15 16
@@ -20,13 +21,13 @@ describe("Article BasicEditor", () => { @@ -20,13 +21,13 @@ describe("Article BasicEditor", () => {
20 })); 21 }));
21 22
22 beforeEach(() => { 23 beforeEach(() => {
  24 + $window = jasmine.createSpyObj("$window", ["back"]);
23 $state = jasmine.createSpyObj("$state", ["go"]); 25 $state = jasmine.createSpyObj("$state", ["go"]);
24 - $stateParams = jasmine.createSpyObj("$stateParams", ["parent_id", "profile"]);  
25 notification = jasmine.createSpyObj("notification", ["success"]); 26 notification = jasmine.createSpyObj("notification", ["success"]);
26 profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier"]); 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 let setCurrentProfileByIdentifierResponse = $q.defer(); 32 let setCurrentProfileByIdentifierResponse = $q.defer();
32 setCurrentProfileByIdentifierResponse.resolve(profile); 33 setCurrentProfileByIdentifierResponse.resolve(profile);
@@ -39,20 +40,22 @@ describe("Article BasicEditor", () => { @@ -39,20 +40,22 @@ describe("Article BasicEditor", () => {
39 40
40 profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise); 41 profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise);
41 articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise); 42 articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise);
  43 + articleServiceMock.updateArticle = jasmine.createSpy("updateArticle").and.returnValue(articleCreate.promise);
42 articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise); 44 articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise);
43 }); 45 });
44 46
45 it("create an article in the current profile when save", done => { 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 component.save(); 50 component.save();
48 $rootScope.$apply(); 51 $rootScope.$apply();
49 expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); 52 expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled();
50 - expect(articleServiceMock.createInParent).toHaveBeenCalledWith($stateParams.parent_id, component.article); 53 + expect(articleServiceMock.createInParent).toHaveBeenCalledWith(1, component.article);
51 done(); 54 done();
52 }); 55 });
53 56
54 it("got to the new article page and display an alert when saving sucessfully", done => { 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 component.save(); 59 component.save();
57 $rootScope.$apply(); 60 $rootScope.$apply();
58 expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); 61 expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" });
@@ -60,18 +63,22 @@ describe("Article BasicEditor", () => { @@ -60,18 +63,22 @@ describe("Article BasicEditor", () => {
60 done(); 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 component.cancel(); 69 component.cancel();
67 - expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "parent-path", profile: $stateParams.profile }); 70 + expect($window.history.back).toHaveBeenCalled();
68 done(); 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 done(); 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,11 +12,13 @@ import {NotificationService} from "../../shared/services/notification.service.ts
12 provide('notification', { useClass: NotificationService }) 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 export class BasicEditorComponent { 16 export class BasicEditorComponent {
17 17
18 - article: noosfero.Article = <noosfero.Article>{}; 18 + article: noosfero.Article = <noosfero.Article>{ type: "TextArticle" };
19 parent: noosfero.Article = <noosfero.Article>{}; 19 parent: noosfero.Article = <noosfero.Article>{};
  20 +
  21 + id: number;
20 parentId: number; 22 parentId: number;
21 profileIdentifier: string; 23 profileIdentifier: string;
22 editorOptions = {}; 24 editorOptions = {};
@@ -25,18 +27,33 @@ export class BasicEditorComponent { @@ -25,18 +27,33 @@ export class BasicEditorComponent {
25 private profileService: ProfileService, 27 private profileService: ProfileService,
26 private $state: ng.ui.IStateService, 28 private $state: ng.ui.IStateService,
27 private notification: NotificationService, 29 private notification: NotificationService,
28 - private $stateParams: ng.ui.IStateParamsService) { 30 + private $stateParams: ng.ui.IStateParamsService,
  31 + private $window: ng.IWindowService) {
29 32
30 this.parentId = this.$stateParams['parent_id']; 33 this.parentId = this.$stateParams['parent_id'];
31 this.profileIdentifier = this.$stateParams["profile"]; 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 save() { 50 save() {
38 this.profileService.setCurrentProfileByIdentifier(this.profileIdentifier).then((profile: noosfero.Profile) => { 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 }).then((response: noosfero.RestResult<noosfero.Article>) => { 57 }).then((response: noosfero.RestResult<noosfero.Article>) => {
41 let article = (<noosfero.Article>response.data); 58 let article = (<noosfero.Article>response.data);
42 this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier }); 59 this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier });
@@ -45,11 +62,7 @@ export class BasicEditorComponent { @@ -45,11 +62,7 @@ export class BasicEditorComponent {
45 } 62 }
46 63
47 cancel() { 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 &quot;./myprofile.component&quot;; @@ -57,6 +57,18 @@ import {MyProfileComponent} from &quot;./myprofile.component&quot;;
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 name: 'main.profile.home', 72 name: 'main.profile.home',
61 url: "", 73 url: "",
62 component: ProfileHomeComponent, 74 component: ProfileHomeComponent,
src/lib/ng-noosfero-api/http/article.service.ts
@@ -22,6 +22,17 @@ export class ArticleService extends RestangularService&lt;noosfero.Article&gt; { @@ -22,6 +22,17 @@ export class ArticleService extends RestangularService&lt;noosfero.Article&gt; {
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 createInProfile(profile: noosfero.Profile, article: noosfero.Article): ng.IPromise<noosfero.RestResult<noosfero.Article>> { 37 createInProfile(profile: noosfero.Profile, article: noosfero.Article): ng.IPromise<noosfero.RestResult<noosfero.Article>> {
27 let profileElement = this.profileService.get(<number>profile.id); 38 let profileElement = this.profileService.get(<number>profile.id);
@@ -85,5 +96,4 @@ export class ArticleService extends RestangularService&lt;noosfero.Article&gt; { @@ -85,5 +96,4 @@ export class ArticleService extends RestangularService&lt;noosfero.Article&gt; {
85 } 96 }
86 97
87 98
88 -  
89 } 99 }
src/lib/ng-noosfero-api/interfaces/article.ts
@@ -5,5 +5,8 @@ namespace noosfero { @@ -5,5 +5,8 @@ namespace noosfero {
5 profile: Profile; 5 profile: Profile;
6 type: string; 6 type: string;
7 parent: Article; 7 parent: Article;
  8 + body: string;
  9 + title: string;
  10 + name: string;
8 } 11 }
9 } 12 }