diff --git a/src/app/article/comment/comment.component.spec.ts b/src/app/article/comment/comment.component.spec.ts index 3955702..332e08e 100644 --- a/src/app/article/comment/comment.component.spec.ts +++ b/src/app/article/comment/comment.component.spec.ts @@ -30,12 +30,11 @@ describe("Components", () => { }); }); - it("render a post comment tag when click in reply", done => { + it("set show reply to true when click reply", done => { helpers.createComponentFromClass(ContainerComponent).then(fixture => { let component: CommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; component.reply(); - fixture.debugElement.getLocal("$rootScope").$apply(); - expect(fixture.debugElement.queryAll("noosfero-post-comment").length).toEqual(1); + expect(component.showReply).toBeTruthy(1); done(); }); }); diff --git a/src/app/article/comment/comment.component.ts b/src/app/article/comment/comment.component.ts index b891cfd..2d041bb 100644 --- a/src/app/article/comment/comment.component.ts +++ b/src/app/article/comment/comment.component.ts @@ -1,9 +1,11 @@ -import { Input, Component } from 'ng-forward'; +import { Inject, Input, Component } from 'ng-forward'; +import { PostCommentComponent } from "./post-comment/post-comment.component"; @Component({ selector: 'noosfero-comment', templateUrl: 'app/article/comment/comment.html' }) +@Inject("$scope") export class CommentComponent { @Input() comment: noosfero.Comment; @@ -11,6 +13,12 @@ export class CommentComponent { showReply: boolean = false; + constructor(private $scope: ng.IScope) { + $scope.$on(PostCommentComponent.EVENT_COMMENT_RECEIVED, (event: ng.IAngularEvent, comment: noosfero.Comment) => { + this.showReply = false; + }); + } + reply() { this.showReply = true; } diff --git a/src/app/article/comment/comment.html b/src/app/article/comment/comment.html index 5bf0eaf..4f312bd 100644 --- a/src/app/article/comment/comment.html +++ b/src/app/article/comment/comment.html @@ -19,7 +19,5 @@
{{ctrl.comment.title}}
{{ctrl.comment.body}}
- - - + diff --git a/src/app/article/comment/comments.component.spec.ts b/src/app/article/comment/comments.component.spec.ts index e17664f..d3a3dfe 100644 --- a/src/app/article/comment/comments.component.spec.ts +++ b/src/app/article/comment/comments.component.spec.ts @@ -22,33 +22,39 @@ describe("Components", () => { new Provider('NotificationService', { useValue: helpers.mocks.notificationService }) ].concat(helpers.provideFilters("translateFilter")); - @Component({ selector: 'test-container-component', directives: [CommentsComponent], template: htmlTemplate, providers: providers }) - class ContainerComponent { - article = { id: 1 }; + let properties = { article: { id: 1 }, parent: null }; + function createComponent() { + return helpers.quickCreateComponent({ + providers: providers, + directives: [CommentsComponent], + template: htmlTemplate, + properties: properties + }); } + it("render comments associated to an article", done => { - helpers.createComponentFromClass(ContainerComponent).then(fixture => { + createComponent().then(fixture => { expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(2); done(); }); }); it("render a post comment tag", done => { - helpers.createComponentFromClass(ContainerComponent).then(fixture => { + createComponent().then(fixture => { expect(fixture.debugElement.queryAll("noosfero-post-comment").length).toEqual(1); done(); }); }); - it("update comments list when receive an event", done => { - helpers.createComponentFromClass(ContainerComponent).then(fixture => { - fixture.debugElement.getLocal("$rootScope").$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, { id: 1 }); + it("update comments list when receive an reply", done => { + properties.parent = { id: 2 }; + createComponent().then(fixture => { + fixture.debugElement.getLocal("$rootScope").$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, { id: 1, reply_of: properties.parent }); fixture.debugElement.getLocal("$rootScope").$apply(); expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3); done(); }); }); - }); }); diff --git a/src/app/article/comment/comments.component.ts b/src/app/article/comment/comments.component.ts index f182807..30cc96b 100644 --- a/src/app/article/comment/comments.component.ts +++ b/src/app/article/comment/comments.component.ts @@ -12,17 +12,26 @@ import { CommentComponent } from "./comment.component"; export class CommentsComponent { comments: noosfero.Comment[] = []; + @Input() showForm = true; @Input() article: noosfero.Article; + @Input() parent: noosfero.Comment; constructor(private commentService: CommentService, private $rootScope: ng.IScope) { $rootScope.$on(PostCommentComponent.EVENT_COMMENT_RECEIVED, (event: ng.IAngularEvent, comment: noosfero.Comment) => { - this.comments.push(comment); + if ((!this.parent && !comment.reply_of) || (comment.reply_of && this.parent && comment.reply_of.id === this.parent.id)) { + if (!this.comments) this.comments = []; + this.comments.push(comment); + } }); } ngOnInit() { - this.commentService.getByArticle(this.article).then((result: noosfero.RestResult) => { - this.comments = result.data; - }); + if (this.parent) { + this.comments = this.parent.replies; + } else { + this.commentService.getByArticle(this.article).then((result: noosfero.RestResult) => { + this.comments = result.data; + }); + } } } diff --git a/src/app/article/comment/comments.html b/src/app/article/comment/comments.html index 88dd8b2..3253b2d 100644 --- a/src/app/article/comment/comments.html +++ b/src/app/article/comment/comments.html @@ -1,5 +1,5 @@
- +
diff --git a/src/app/article/comment/post-comment/post-comment.component.spec.ts b/src/app/article/comment/post-comment/post-comment.component.spec.ts index e7beab4..b981aca 100644 --- a/src/app/article/comment/post-comment/post-comment.component.spec.ts +++ b/src/app/article/comment/post-comment/post-comment.component.spec.ts @@ -33,9 +33,9 @@ describe("Components", () => { helpers.createComponentFromClass(ContainerComponent).then(fixture => { let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; commentService.createInArticle = jasmine.createSpy("createInArticle").and.returnValue(helpers.mocks.promiseResultTemplate({ data: {} })); - component["$rootScope"].$emit = jasmine.createSpy("$emit"); + component["$scope"].$emit = jasmine.createSpy("$emit"); component.save(); - expect(component["$rootScope"].$emit).toHaveBeenCalledWith(PostCommentComponent.EVENT_COMMENT_RECEIVED, jasmine.any(Object)); + expect(component["$scope"].$emit).toHaveBeenCalledWith(PostCommentComponent.EVENT_COMMENT_RECEIVED, jasmine.any(Object)); done(); }); }); @@ -55,9 +55,9 @@ describe("Components", () => { helpers.createComponentFromClass(ContainerComponent).then(fixture => { let component: PostCommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance; component.comment = { reply_of_id: null }; - component.replyOf = { id: 10 }; + component.parent = { id: 10 }; component.save(); - expect(component.comment.reply_of_id).toEqual(component.replyOf.id); + expect(component.comment.reply_of_id).toEqual(component.parent.id); done(); }); }); diff --git a/src/app/article/comment/post-comment/post-comment.component.ts b/src/app/article/comment/post-comment/post-comment.component.ts index 5ab47cf..1bbc5eb 100644 --- a/src/app/article/comment/post-comment/post-comment.component.ts +++ b/src/app/article/comment/post-comment/post-comment.component.ts @@ -6,24 +6,25 @@ import { NotificationService } from "../../../shared/services/notification.servi selector: 'noosfero-post-comment', templateUrl: 'app/article/comment/post-comment/post-comment.html' }) -@Inject(CommentService, NotificationService, "$rootScope") +@Inject(CommentService, NotificationService, "$scope") export class PostCommentComponent { public static EVENT_COMMENT_RECEIVED = "comment.received"; @Input() article: noosfero.Article; - @Input() replyOf: noosfero.Comment; + @Input() parent: noosfero.Comment; - comment: noosfero.Comment; + comment = {}; - constructor(private commentService: CommentService, private notificationService: NotificationService, private $rootScope: ng.IScope) { } + constructor(private commentService: CommentService, private notificationService: NotificationService, private $scope: ng.IScope) { } save() { - if (this.replyOf && this.comment) { - this.comment.reply_of_id = this.replyOf.id; + if (this.parent && this.comment) { + this.comment.reply_of_id = this.parent.id; } this.commentService.createInArticle(this.article, this.comment).then((result: noosfero.RestResult) => { - this.$rootScope.$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, result.data); + this.$scope.$emit(PostCommentComponent.EVENT_COMMENT_RECEIVED, result.data); + this.comment.body = ""; this.notificationService.success({ title: "Good job!", message: "Comment saved!" }); }); } diff --git a/src/lib/ng-noosfero-api/http/comment.service.spec.ts b/src/lib/ng-noosfero-api/http/comment.service.spec.ts index 13dd8dd..74bafa6 100644 --- a/src/lib/ng-noosfero-api/http/comment.service.spec.ts +++ b/src/lib/ng-noosfero-api/http/comment.service.spec.ts @@ -22,8 +22,8 @@ describe("Services", () => { it("should return comments by article", (done) => { let articleId = 1; - $httpBackend.expectGET(`/api/v1/articles/${articleId}/comments`).respond(200, { comments: [{ name: "comment1" }] }); - commentService.getByArticle({id: articleId}).then((result: noosfero.RestResult) => { + $httpBackend.expectGET(`/api/v1/articles/${articleId}/comments?without_reply=true`).respond(200, { comments: [{ name: "comment1" }] }); + commentService.getByArticle({ id: articleId }).then((result: noosfero.RestResult) => { expect(result.data).toEqual([{ name: "comment1" }]); done(); }); @@ -32,9 +32,9 @@ describe("Services", () => { it("should create a comment in an article", (done) => { let articleId = 1; - let comment: noosfero.Comment = { id: null}; - $httpBackend.expectPOST(`/api/v1/articles/${articleId}/comments`, comment ).respond(200, {comment: { id: 2 }}); - commentService.createInArticle({id: articleId}, comment).then((result: noosfero.RestResult) => { + let comment: noosfero.Comment = { id: null }; + $httpBackend.expectPOST(`/api/v1/articles/${articleId}/comments`, comment).respond(200, { comment: { id: 2 } }); + commentService.createInArticle({ id: articleId }, comment).then((result: noosfero.RestResult) => { expect(result.data).toEqual({ id: 2 }); done(); }); diff --git a/src/lib/ng-noosfero-api/http/comment.service.ts b/src/lib/ng-noosfero-api/http/comment.service.ts index 514797c..62e7146 100644 --- a/src/lib/ng-noosfero-api/http/comment.service.ts +++ b/src/lib/ng-noosfero-api/http/comment.service.ts @@ -21,9 +21,10 @@ export class CommentService extends RestangularService { }; } - getByArticle(article: noosfero.Article, params?: any): ng.IPromise> { + getByArticle(article: noosfero.Article, params: any = {}): ng.IPromise> { + params['without_reply'] = true; let articleElement = this.articleService.getElement(article.id); - return this.list(articleElement); + return this.list(articleElement, params); } createInArticle(article: noosfero.Article, comment: noosfero.Comment): ng.IPromise> { diff --git a/src/lib/ng-noosfero-api/interfaces/comment.ts b/src/lib/ng-noosfero-api/interfaces/comment.ts index b14ac0c..f31ea3b 100644 --- a/src/lib/ng-noosfero-api/interfaces/comment.ts +++ b/src/lib/ng-noosfero-api/interfaces/comment.ts @@ -1,5 +1,8 @@ namespace noosfero { export interface Comment extends RestModel { reply_of_id: number; + reply_of: Comment; + replies: Comment[]; + body: string; } } -- libgit2 0.21.2