]>
Commit | Line | Data |
---|---|---|
1 | import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; | |
2 | import { AsyncPipe } from '@angular/common'; | |
3 | import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from '@angular/router'; | |
4 | import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
5 | ||
6 | import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; | |
7 | ||
8 | import { | |
9 | LoaderComponent, | |
10 | Pagination, | |
11 | SortField, | |
12 | Video, | |
13 | VideoService | |
14 | } from '../shared'; | |
15 | import { AuthService, Search, SearchField, User } from '../../shared'; | |
16 | import { VideoMiniatureComponent } from './video-miniature.component'; | |
17 | import { VideoSortComponent } from './video-sort.component'; | |
18 | import { SearchService } from '../../shared'; | |
19 | ||
20 | @Component({ | |
21 | selector: 'my-videos-list', | |
22 | styles: [ require('./video-list.component.scss') ], | |
23 | pipes: [ AsyncPipe ], | |
24 | template: require('./video-list.component.html'), | |
25 | directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] | |
26 | }) | |
27 | ||
28 | export class VideoListComponent implements OnInit, OnDestroy { | |
29 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false); | |
30 | pagination: Pagination = { | |
31 | currentPage: 1, | |
32 | itemsPerPage: 9, | |
33 | totalItems: null | |
34 | }; | |
35 | sort: SortField; | |
36 | user: User = null; | |
37 | videos: Video[] = []; | |
38 | ||
39 | private search: Search; | |
40 | private sub: any; | |
41 | ||
42 | constructor( | |
43 | private authService: AuthService, | |
44 | private changeDetector: ChangeDetectorRef, | |
45 | private router: Router, | |
46 | private route: ActivatedRoute, | |
47 | private videoService: VideoService, | |
48 | private searchService: SearchService | |
49 | ) {} | |
50 | ||
51 | ngOnInit() { | |
52 | this.sub = this.route.params.subscribe(routeParams => { | |
53 | if (this.authService.isLoggedIn()) { | |
54 | this.user = User.load(); | |
55 | } | |
56 | ||
57 | this.search = { | |
58 | value: routeParams['search'], | |
59 | field: <SearchField>routeParams['field'] | |
60 | }; | |
61 | ||
62 | // Update the search service component | |
63 | this.searchService.searchChanged.next(this.search); | |
64 | ||
65 | this.sort = <SortField>routeParams['sort'] || '-createdDate'; | |
66 | ||
67 | this.getVideos(); | |
68 | }); | |
69 | } | |
70 | ||
71 | ngOnDestroy() { | |
72 | this.sub.unsubscribe(); | |
73 | } | |
74 | ||
75 | getVideos(detectChanges = true) { | |
76 | this.loading.next(true); | |
77 | this.videos = []; | |
78 | this.pagination.currentPage = 1; | |
79 | ||
80 | this.changeDetector.detectChanges(); | |
81 | ||
82 | let observable = null; | |
83 | ||
84 | if (this.search.value) { | |
85 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort); | |
86 | } else { | |
87 | observable = this.videoService.getVideos(this.pagination, this.sort); | |
88 | } | |
89 | ||
90 | observable.subscribe( | |
91 | ({ videos, totalVideos }) => { | |
92 | this.videos = videos; | |
93 | this.pagination.totalItems = totalVideos; | |
94 | ||
95 | this.loading.next(false); | |
96 | }, | |
97 | error => alert(error) | |
98 | ); | |
99 | } | |
100 | ||
101 | noVideo() { | |
102 | return !this.loading && this.videos.length === 0; | |
103 | } | |
104 | ||
105 | onRemoved(video: Video) { | |
106 | this.getVideos(false); | |
107 | } | |
108 | ||
109 | onSort(sort: SortField) { | |
110 | this.sort = sort; | |
111 | ||
112 | const params: any = { | |
113 | sort: this.sort | |
114 | }; | |
115 | ||
116 | if (this.search.value) { | |
117 | params.field = this.search.field; | |
118 | params.search = this.search.value; | |
119 | } | |
120 | ||
121 | this.router.navigate(['/videos/list', params]); | |
122 | } | |
123 | } |