]>
Commit | Line | Data |
---|---|---|
4635f59d C |
1 | import { Component, Input, OnInit } from '@angular/core' |
2 | import { NotificationsService } from 'angular2-notifications' | |
3 | import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model' | |
4 | import { AuthService } from '../../../core/auth' | |
5 | import { ComponentPagination } from '../../../shared/rest/component-pagination.model' | |
6 | import { User } from '../../../shared/users' | |
7 | import { SortField } from '../../../shared/video/sort-field.type' | |
8 | import { Video } from '../../../shared/video/video.model' | |
9 | import { VideoComment } from './video-comment.model' | |
10 | import { VideoCommentService } from './video-comment.service' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-video-comments', | |
14 | templateUrl: './video-comments.component.html', | |
15 | styleUrls: ['./video-comments.component.scss'] | |
16 | }) | |
17 | export class VideoCommentsComponent implements OnInit { | |
18 | @Input() video: Video | |
19 | @Input() user: User | |
20 | ||
21 | comments: VideoComment[] = [] | |
22 | sort: SortField = '-createdAt' | |
23 | componentPagination: ComponentPagination = { | |
24 | currentPage: 1, | |
7416fbf3 | 25 | itemsPerPage: 10, |
4635f59d C |
26 | totalItems: null |
27 | } | |
28 | inReplyToCommentId: number | |
29 | threadComments: { [ id: number ]: VideoCommentThreadTree } = {} | |
30 | threadLoading: { [ id: number ]: boolean } = {} | |
31 | ||
32 | constructor ( | |
33 | private authService: AuthService, | |
34 | private notificationsService: NotificationsService, | |
35 | private videoCommentService: VideoCommentService | |
36 | ) {} | |
37 | ||
38 | ngOnInit () { | |
7416fbf3 | 39 | this.loadMoreComments() |
4635f59d C |
40 | } |
41 | ||
42 | viewReplies (comment: VideoComment) { | |
43 | this.threadLoading[comment.id] = true | |
44 | ||
45 | this.videoCommentService.getVideoThreadComments(this.video.id, comment.id) | |
46 | .subscribe( | |
47 | res => { | |
48 | this.threadComments[comment.id] = res | |
49 | this.threadLoading[comment.id] = false | |
50 | }, | |
51 | ||
52 | err => this.notificationsService.error('Error', err.message) | |
53 | ) | |
54 | } | |
55 | ||
7416fbf3 C |
56 | loadMoreComments () { |
57 | this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort) | |
58 | .subscribe( | |
59 | res => { | |
60 | this.comments = this.comments.concat(res.comments) | |
61 | this.componentPagination.totalItems = res.totalComments | |
62 | }, | |
63 | ||
64 | err => this.notificationsService.error('Error', err.message) | |
65 | ) | |
66 | } | |
67 | ||
4635f59d C |
68 | onCommentThreadCreated (comment: VideoComment) { |
69 | this.comments.unshift(comment) | |
70 | } | |
71 | ||
72 | onWantedToReply (comment: VideoComment) { | |
73 | this.inReplyToCommentId = comment.id | |
74 | } | |
75 | ||
76 | onResetReply () { | |
77 | this.inReplyToCommentId = undefined | |
78 | } | |
79 | ||
80 | isUserLoggedIn () { | |
81 | return this.authService.isLoggedIn() | |
82 | } | |
7416fbf3 C |
83 | |
84 | onNearOfBottom () { | |
85 | this.componentPagination.currentPage++ | |
86 | ||
87 | if (this.hasMoreComments()) { | |
88 | this.loadMoreComments() | |
89 | } | |
90 | } | |
91 | ||
92 | protected hasMoreComments () { | |
93 | // No results | |
94 | if (this.componentPagination.totalItems === 0) return false | |
95 | ||
96 | // Not loaded yet | |
97 | if (!this.componentPagination.totalItems) return true | |
98 | ||
99 | const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage | |
100 | return maxPage > this.componentPagination.currentPage | |
101 | } | |
4635f59d | 102 | } |