Commit ecac812cf39ab95ea985ddb160b8f9fc43062010
1 parent
a5984494
Exists in
master
and in
27 other branches
Add cancel button in basic editor
Showing
3 changed files
with
42 additions
and
7 deletions
Show diff stats
src/app/article/basic-editor/basic-editor.component.spec.ts
... | ... | @@ -20,11 +20,13 @@ describe("Article BasicEditor", () => { |
20 | 20 | })); |
21 | 21 | |
22 | 22 | beforeEach(() => { |
23 | - $state = jasmine.createSpyObj("$state", ["transitionTo"]); | |
24 | - $stateParams = jasmine.createSpyObj("$stateParams", ["parent_id"]); | |
23 | + $state = jasmine.createSpyObj("$state", ["go"]); | |
24 | + $stateParams = jasmine.createSpyObj("$stateParams", ["parent_id", "profile"]); | |
25 | 25 | notification = jasmine.createSpyObj("notification", ["success"]); |
26 | 26 | profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier"]); |
27 | - articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent"]); | |
27 | + articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "get"]); | |
28 | + | |
29 | + $stateParams.profile = jasmine.createSpy("profile").and.returnValue("profile"); | |
28 | 30 | |
29 | 31 | let setCurrentProfileByIdentifierResponse = $q.defer(); |
30 | 32 | setCurrentProfileByIdentifierResponse.resolve(profile); |
... | ... | @@ -32,8 +34,12 @@ describe("Article BasicEditor", () => { |
32 | 34 | let articleCreate = $q.defer(); |
33 | 35 | articleCreate.resolve({ data: { path: "path", profile: { identifier: "profile" } } }); |
34 | 36 | |
37 | + let articleGet = $q.defer(); | |
38 | + articleGet.resolve({ data: { path: "parent-path", profile: { identifier: "profile" } } }); | |
39 | + | |
35 | 40 | profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise); |
36 | 41 | articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise); |
42 | + articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise); | |
37 | 43 | }); |
38 | 44 | |
39 | 45 | it("create an article in the current profile when save", done => { |
... | ... | @@ -49,9 +55,23 @@ describe("Article BasicEditor", () => { |
49 | 55 | let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams); |
50 | 56 | component.save(); |
51 | 57 | $rootScope.$apply(); |
52 | - expect($state.transitionTo).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); | |
58 | + expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); | |
53 | 59 | expect(notification.success).toHaveBeenCalled(); |
54 | 60 | done(); |
55 | 61 | }); |
56 | 62 | |
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 | + component.cancel(); | |
67 | + expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "parent-path", profile: $stateParams.profile }); | |
68 | + done(); | |
69 | + }); | |
70 | + | |
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 }); | |
75 | + done(); | |
76 | + }); | |
57 | 77 | }); | ... | ... |
src/app/article/basic-editor/basic-editor.component.ts
... | ... | @@ -16,8 +16,9 @@ import {NotificationService} from "../../shared/services/notification.service.ts |
16 | 16 | export class BasicEditorComponent { |
17 | 17 | |
18 | 18 | article: noosfero.Article = <noosfero.Article>{}; |
19 | + parent: noosfero.Article = <noosfero.Article>{}; | |
19 | 20 | parentId: number; |
20 | - | |
21 | + profileIdentifier: string; | |
21 | 22 | editorOptions = {}; |
22 | 23 | |
23 | 24 | constructor(private articleService: ArticleService, |
... | ... | @@ -25,17 +26,30 @@ export class BasicEditorComponent { |
25 | 26 | private $state: ng.ui.IStateService, |
26 | 27 | private notification: NotificationService, |
27 | 28 | private $stateParams: ng.ui.IStateParamsService) { |
29 | + | |
28 | 30 | this.parentId = this.$stateParams['parent_id']; |
31 | + this.profileIdentifier = this.$stateParams["profile"]; | |
32 | + this.articleService.get(this.parentId).then((result: noosfero.RestResult<noosfero.Article>) => { | |
33 | + this.parent = result.data; | |
34 | + }); | |
29 | 35 | } |
30 | 36 | |
31 | 37 | save() { |
32 | - this.profileService.setCurrentProfileByIdentifier(this.$stateParams["profile"]).then((profile: noosfero.Profile) => { | |
38 | + this.profileService.setCurrentProfileByIdentifier(this.profileIdentifier).then((profile: noosfero.Profile) => { | |
33 | 39 | return this.articleService.createInParent(this.parentId, this.article); |
34 | 40 | }).then((response: noosfero.RestResult<noosfero.Article>) => { |
35 | 41 | let article = (<noosfero.Article>response.data); |
36 | - this.$state.transitionTo('main.profile.page', { page: article.path, profile: article.profile.identifier }); | |
42 | + this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier }); | |
37 | 43 | this.notification.success({ title: "Good job!", message: "Article saved!" }); |
38 | 44 | }); |
39 | 45 | } |
40 | 46 | |
47 | + 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 | + } | |
53 | + } | |
54 | + | |
41 | 55 | } | ... | ... |
src/app/article/basic-editor/basic-editor.html
... | ... | @@ -11,6 +11,7 @@ |
11 | 11 | <textarea ckeditor="vm.editorOptions" class="form-control" id="bodyInput" rows="10" ng-model="vm.article.body"></textarea> |
12 | 12 | </div> |
13 | 13 | <button type="submit" class="btn btn-default" ng-click="vm.save()">Save</button> |
14 | + <button type="button" class="btn btn-danger" ng-click="vm.cancel()">Cancel</button> | |
14 | 15 | </form> |
15 | 16 | </div> |
16 | 17 | <div class="col-md-2"></div> | ... | ... |