Commit edcd044256c6f2f8bae88268c309c95a90fd962f
Exists in
master
and in
15 other branches
Merge branch 'comment-count-update_2' into 'master'
Comment Paragraph - Comment count update Added a simple solution to the count updates and lists. A solution more simple than the proposed in the merge request https://softwarepublico.gov.br/gitlab/noosfero-themes/angular-theme/merge_requests/30 See merge request !32
Showing
5 changed files
with
48 additions
and
9 deletions
Show diff stats
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[] = []; |
... | ... | @@ -32,9 +32,22 @@ export class CommentsComponent { |
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
35 | - commentAdded(comment: noosfero.Comment): void { | |
35 | + commentAdded(comment: noosfero.CommentViewModel): void { | |
36 | + comment.__show_reply = false; | |
37 | + if (comment.reply_of) { | |
38 | + this.comments.forEach((commentOnList) => { | |
39 | + if (commentOnList.id == comment.reply_of.id) { | |
40 | + if (commentOnList.replies) { | |
41 | + commentOnList.replies.push(comment); | |
42 | + } else { | |
43 | + commentOnList.replies = [comment]; | |
44 | + } | |
45 | + } | |
46 | + }); | |
47 | + } | |
36 | 48 | this.comments.push(comment); |
37 | 49 | this.resetShowReply(); |
50 | + this.$scope.$apply(); | |
38 | 51 | } |
39 | 52 | |
40 | 53 | commentRemoved(comment: noosfero.Comment): void { |
... | ... | @@ -51,6 +64,7 @@ export class CommentsComponent { |
51 | 64 | if (this.parent) { |
52 | 65 | this.parent.__show_reply = false; |
53 | 66 | } |
67 | + | |
54 | 68 | } |
55 | 69 | |
56 | 70 | loadComments() { | ... | ... |
src/lib/ng-noosfero-api/http/restangular_service.ts
... | ... | @@ -223,7 +223,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
223 | 223 | restRequest = restangularObj.remove(queryParams, headers); |
224 | 224 | |
225 | 225 | restRequest |
226 | - .then(this.getHandleSuccessFunction(deferred, this.modelRemovedEventEmitter)) | |
226 | + .then(this.getHandleSuccessFunction(deferred, this.modelRemovedEventEmitter, obj)) | |
227 | 227 | .catch(this.getHandleErrorFunction(deferred)); |
228 | 228 | |
229 | 229 | return deferred.promise; |
... | ... | @@ -311,7 +311,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
311 | 311 | } |
312 | 312 | |
313 | 313 | /** HANDLERS */ |
314 | - protected getHandleSuccessFunction<C>(deferred: ng.IDeferred<noosfero.RestResult<C | T | any>>, successEmitter: EventEmitter<T> = null): (response: restangular.IResponse) => void { | |
314 | + protected getHandleSuccessFunction<C>(deferred: ng.IDeferred<noosfero.RestResult<C | T | any>>, successEmitter: EventEmitter<T> = null, currentModel: T = null): (response: restangular.IResponse) => void { | |
315 | 315 | let self = this; |
316 | 316 | |
317 | 317 | /** |
... | ... | @@ -328,7 +328,11 @@ 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 | + if (successEmitter !== this.modelRemovedEventEmitter) { | |
332 | + successEmitter.next(resultModel.data); | |
333 | + } else { | |
334 | + successEmitter.next(currentModel !== null ? currentModel : resultModel.data); | |
335 | + } | |
332 | 336 | } |
333 | 337 | }; |
334 | 338 | return successFunction; | ... | ... |
src/lib/ng-noosfero-api/interfaces/comment.ts
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,19 @@ 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 | + }; | |
40 | + }); | |
41 | + | |
42 | + this.commentService.subscribeToModelRemoved((comment: noosfero.CommentParagraph) => { | |
43 | + if (comment.paragraph_uuid === this.paragraphUuid) { | |
44 | + this.commentsCount -= (comment.replies) ? 1 + comment.replies.length : 1; | |
45 | + }; | |
31 | 46 | }); |
32 | 47 | } |
33 | 48 | ... | ... |
src/plugins/comment_paragraph/models/comment_paragraph.ts
0 → 100644