Commit 9dc72ea1143c78263cc69ecb0c726acd38cd0077

Authored by ABNER SILVA DE OLIVEIRA
1 parent d5a304a4

added event listeners on comments and discussion comments to update comment coun…

…ts and add new comments to the list
src/app/article/comment/comments.component.ts
@@ -9,7 +9,7 @@ import { CommentComponent } from "./comment.component"; @@ -9,7 +9,7 @@ import { CommentComponent } from "./comment.component";
9 directives: [PostCommentComponent, CommentComponent], 9 directives: [PostCommentComponent, CommentComponent],
10 outputs: ['commentAdded'] 10 outputs: ['commentAdded']
11 }) 11 })
12 -@Inject(CommentService, "$element") 12 +@Inject(CommentService, "$scope")
13 export class CommentsComponent { 13 export class CommentsComponent {
14 14
15 comments: noosfero.CommentViewModel[] = []; 15 comments: noosfero.CommentViewModel[] = [];
@@ -30,11 +30,23 @@ export class CommentsComponent { @@ -30,11 +30,23 @@ export class CommentsComponent {
30 } else { 30 } else {
31 this.loadNextPage(); 31 this.loadNextPage();
32 } 32 }
  33 + this.commentService.subscribeToModelAdded((comment: noosfero.Comment) => {
  34 + if (comment.source_id === this.article.id) {
  35 + this.commentAdded(comment);
  36 + }
  37 + });
  38 + this.commentService.subscribeToModelRemoved((comment: noosfero.Comment) => {
  39 + if (comment.source_id === this.article.id) {
  40 + this.commentRemoved(comment);
  41 + }
  42 + });
33 } 43 }
34 44
35 - commentAdded(comment: noosfero.Comment): void { 45 + commentAdded(comment: noosfero.CommentViewModel): void {
  46 + comment.__show_reply = false;
36 this.comments.push(comment); 47 this.comments.push(comment);
37 this.resetShowReply(); 48 this.resetShowReply();
  49 + this.$scope.$apply();
38 } 50 }
39 51
40 commentRemoved(comment: noosfero.Comment): void { 52 commentRemoved(comment: noosfero.Comment): void {
@@ -51,6 +63,7 @@ export class CommentsComponent { @@ -51,6 +63,7 @@ export class CommentsComponent {
51 if (this.parent) { 63 if (this.parent) {
52 this.parent.__show_reply = false; 64 this.parent.__show_reply = false;
53 } 65 }
  66 +
54 } 67 }
55 68
56 loadComments() { 69 loadComments() {
src/lib/ng-noosfero-api/http/restangular_service.ts
@@ -328,7 +328,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { @@ -328,7 +328,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> {
328 deferred.resolve(resultModel); 328 deferred.resolve(resultModel);
329 // emits the event if a successEmiter was provided in the successEmitter parameter 329 // emits the event if a successEmiter was provided in the successEmitter parameter
330 if (successEmitter !== null) { 330 if (successEmitter !== null) {
331 - successEmitter.next(resultModel); 331 + successEmitter.next(resultModel.data);
332 } 332 }
333 }; 333 };
334 return successFunction; 334 return successFunction;
src/lib/ng-noosfero-api/interfaces/comment.ts
1 namespace noosfero { 1 namespace noosfero {
2 export interface Comment extends RestModel { 2 export interface Comment extends RestModel {
  3 + source_id: number;
3 reply_of_id: number; 4 reply_of_id: number;
4 reply_of: Comment; 5 reply_of: Comment;
5 replies: Comment[]; 6 replies: Comment[];
src/plugins/comment_paragraph/allow-comment/allow-comment.component.ts
@@ -2,24 +2,27 @@ import {Component, Input, Inject} from "ng-forward"; @@ -2,24 +2,27 @@ import {Component, Input, Inject} from "ng-forward";
2 import {SideCommentsComponent} from "../side-comments/side-comments.component"; 2 import {SideCommentsComponent} from "../side-comments/side-comments.component";
3 import {CommentParagraphEventService} from "../events/comment-paragraph-event.service"; 3 import {CommentParagraphEventService} from "../events/comment-paragraph-event.service";
4 import {CommentParagraphService} from "../http/comment-paragraph.service"; 4 import {CommentParagraphService} from "../http/comment-paragraph.service";
  5 +import {CommentService} from "./../../../lib/ng-noosfero-api/http/comment.service";
5 6
6 @Component({ 7 @Component({
7 selector: "comment-paragraph-plugin-allow-comment", 8 selector: "comment-paragraph-plugin-allow-comment",
8 templateUrl: "plugins/comment_paragraph/allow-comment/allow-comment.html", 9 templateUrl: "plugins/comment_paragraph/allow-comment/allow-comment.html",
9 directives: [SideCommentsComponent] 10 directives: [SideCommentsComponent]
10 }) 11 })
11 -@Inject("$scope", CommentParagraphEventService, CommentParagraphService) 12 +@Inject("$scope", CommentParagraphEventService, CommentParagraphService, CommentService)
12 export class AllowCommentComponent { 13 export class AllowCommentComponent {
13 14
14 @Input() content: string; 15 @Input() content: string;
15 @Input() paragraphUuid: string; 16 @Input() paragraphUuid: string;
16 @Input() article: noosfero.Article; 17 @Input() article: noosfero.Article;
17 - commentsCount: number; 18 + commentsCount: number = 0;
18 display = false; 19 display = false;
19 20
20 constructor(private $scope: ng.IScope, 21 constructor(private $scope: ng.IScope,
21 private commentParagraphEventService: CommentParagraphEventService, 22 private commentParagraphEventService: CommentParagraphEventService,
22 - private commentParagraphService: CommentParagraphService) { } 23 + private commentParagraphService: CommentParagraphService,
  24 + private commentService: CommentService
  25 + ) { }
23 26
24 ngOnInit() { 27 ngOnInit() {
25 this.commentParagraphEventService.subscribeToggleCommentParagraph((article: noosfero.Article) => { 28 this.commentParagraphEventService.subscribeToggleCommentParagraph((article: noosfero.Article) => {
@@ -27,7 +30,21 @@ export class AllowCommentComponent { @@ -27,7 +30,21 @@ export class AllowCommentComponent {
27 this.$scope.$apply(); 30 this.$scope.$apply();
28 }); 31 });
29 this.commentParagraphService.commentParagraphCount(this.article, this.paragraphUuid).then((count: number) => { 32 this.commentParagraphService.commentParagraphCount(this.article, this.paragraphUuid).then((count: number) => {
30 - this.commentsCount = count; 33 + this.commentsCount = count ? count : 0;
  34 + });
  35 +
  36 + this.commentService.subscribeToModelAdded((comment: noosfero.CommentParagraph) => {
  37 + if (comment.paragraph_uuid === this.paragraphUuid) {
  38 + this.commentsCount += 1;
  39 + this.$scope.$apply();
  40 + };
  41 + });
  42 +
  43 + this.commentService.subscribeToModelRemoved((comment: noosfero.CommentParagraph) => {
  44 + if (comment.paragraph_uuid === this.paragraphUuid) {
  45 + this.commentsCount -= 1;
  46 + this.$scope.$apply();
  47 + };
31 }); 48 });
32 } 49 }
33 50
src/plugins/comment_paragraph/models/comment_paragraph.ts 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +namespace noosfero {
  2 + export interface CommentParagraph extends noosfero.Comment {
  3 + paragraph_uuid?: string;
  4 + }
  5 +}
0 \ No newline at end of file 6 \ No newline at end of file