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