diff options
Diffstat (limited to 'client/src/app/videos/video-list/video-list.component.ts')
-rw-r--r-- | client/src/app/videos/video-list/video-list.component.ts | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/client/src/app/videos/video-list/video-list.component.ts b/client/src/app/videos/video-list/video-list.component.ts index 46263eb65..0ebf0ef5c 100644 --- a/client/src/app/videos/video-list/video-list.component.ts +++ b/client/src/app/videos/video-list/video-list.component.ts | |||
@@ -1,5 +1,7 @@ | |||
1 | import { Component, OnInit } from '@angular/core'; | 1 | import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; |
2 | import { Router, ROUTER_DIRECTIVES, RouteSegment } from '@angular/router'; | 2 | import { AsyncPipe } from '@angular/common'; |
3 | import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from '@angular/router'; | ||
4 | import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | ||
3 | 5 | ||
4 | import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; | 6 | import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination'; |
5 | 7 | ||
@@ -18,52 +20,64 @@ import { SearchService } from '../../shared'; | |||
18 | @Component({ | 20 | @Component({ |
19 | selector: 'my-videos-list', | 21 | selector: 'my-videos-list', |
20 | styles: [ require('./video-list.component.scss') ], | 22 | styles: [ require('./video-list.component.scss') ], |
23 | pipes: [ AsyncPipe ], | ||
21 | template: require('./video-list.component.html'), | 24 | template: require('./video-list.component.html'), |
22 | directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] | 25 | directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] |
23 | }) | 26 | }) |
24 | 27 | ||
25 | export class VideoListComponent implements OnInit { | 28 | export class VideoListComponent implements OnInit, OnDestroy { |
26 | loading = false; | 29 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false); |
27 | pagination: Pagination = { | 30 | pagination: Pagination = { |
28 | currentPage: 1, | 31 | currentPage: 1, |
29 | itemsPerPage: 9, | 32 | itemsPerPage: 9, |
30 | total: 0 | 33 | totalItems: null |
31 | }; | 34 | }; |
32 | sort: SortField; | 35 | sort: SortField; |
33 | user: User = null; | 36 | user: User = null; |
34 | videos: Video[] = []; | 37 | videos: Video[] = []; |
35 | 38 | ||
36 | private search: Search; | 39 | private search: Search; |
40 | private sub: any; | ||
37 | 41 | ||
38 | constructor( | 42 | constructor( |
39 | private authService: AuthService, | 43 | private authService: AuthService, |
44 | private changeDetector: ChangeDetectorRef, | ||
40 | private router: Router, | 45 | private router: Router, |
41 | private routeSegment: RouteSegment, | 46 | private route: ActivatedRoute, |
42 | private videoService: VideoService, | 47 | private videoService: VideoService, |
43 | private searchService: SearchService // Temporary | 48 | private searchService: SearchService |
44 | ) {} | 49 | ) {} |
45 | 50 | ||
46 | ngOnInit() { | 51 | ngOnInit() { |
47 | if (this.authService.isLoggedIn()) { | 52 | this.sub = this.route.params.subscribe(routeParams => { |
48 | this.user = User.load(); | 53 | if (this.authService.isLoggedIn()) { |
49 | } | 54 | this.user = User.load(); |
55 | } | ||
50 | 56 | ||
51 | this.search = { | 57 | this.search = { |
52 | value: this.routeSegment.getParam('search'), | 58 | value: routeParams['search'], |
53 | field: <SearchField>this.routeSegment.getParam('field') | 59 | field: <SearchField>routeParams['field'] |
54 | }; | 60 | }; |
55 | 61 | ||
56 | // Temporary | 62 | // Update the search service component |
57 | this.searchChanged(this.search); | 63 | this.searchService.searchChanged.next(this.search); |
58 | 64 | ||
59 | this.sort = <SortField>this.routeSegment.getParam('sort') || '-createdDate'; | 65 | this.sort = <SortField>routeParams['sort'] || '-createdDate'; |
66 | |||
67 | this.getVideos(); | ||
68 | }); | ||
69 | } | ||
60 | 70 | ||
61 | this.getVideos(); | 71 | ngOnDestroy() { |
72 | this.sub.unsubscribe(); | ||
62 | } | 73 | } |
63 | 74 | ||
64 | getVideos() { | 75 | getVideos(detectChanges = true) { |
65 | this.loading = true; | 76 | this.loading.next(true); |
66 | this.videos = []; | 77 | this.videos = []; |
78 | this.pagination.currentPage = 1; | ||
79 | |||
80 | this.changeDetector.detectChanges(); | ||
67 | 81 | ||
68 | let observable = null; | 82 | let observable = null; |
69 | 83 | ||
@@ -76,9 +90,9 @@ export class VideoListComponent implements OnInit { | |||
76 | observable.subscribe( | 90 | observable.subscribe( |
77 | ({ videos, totalVideos }) => { | 91 | ({ videos, totalVideos }) => { |
78 | this.videos = videos; | 92 | this.videos = videos; |
79 | this.pagination.total = totalVideos; | 93 | this.pagination.totalItems = totalVideos; |
80 | 94 | ||
81 | this.loading = false; | 95 | this.loading.next(false); |
82 | }, | 96 | }, |
83 | error => alert(error) | 97 | error => alert(error) |
84 | ); | 98 | ); |
@@ -89,7 +103,7 @@ export class VideoListComponent implements OnInit { | |||
89 | } | 103 | } |
90 | 104 | ||
91 | onRemoved(video: Video) { | 105 | onRemoved(video: Video) { |
92 | this.getVideos(); | 106 | this.getVideos(false); |
93 | } | 107 | } |
94 | 108 | ||
95 | onSort(sort: SortField) { | 109 | onSort(sort: SortField) { |
@@ -106,8 +120,4 @@ export class VideoListComponent implements OnInit { | |||
106 | 120 | ||
107 | this.router.navigate(['/videos/list', params]); | 121 | this.router.navigate(['/videos/list', params]); |
108 | } | 122 | } |
109 | |||
110 | searchChanged(search: Search) { | ||
111 | this.searchService.searchChanged.next(search); | ||
112 | } | ||
113 | } | 123 | } |