]>
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 | SortField, | |
11 | Video, | |
12 | VideoService | |
13 | } from '../shared'; | |
14 | import { AuthService, AuthUser, RestPagination, Search, SearchField } from '../../shared'; | |
15 | import { VideoMiniatureComponent } from './video-miniature.component'; | |
16 | import { VideoSortComponent } from './video-sort.component'; | |
17 | import { SearchService } from '../../shared'; | |
18 | ||
19 | @Component({ | |
20 | selector: 'my-videos-list', | |
21 | styles: [ require('./video-list.component.scss') ], | |
22 | pipes: [ AsyncPipe ], | |
23 | template: require('./video-list.component.html'), | |
24 | directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ] | |
25 | }) | |
26 | ||
27 | export class VideoListComponent implements OnInit, OnDestroy { | |
28 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false); | |
29 | pagination: RestPagination = { | |
30 | currentPage: 1, | |
31 | itemsPerPage: 9, | |
32 | totalItems: null | |
33 | }; | |
34 | sort: SortField; | |
35 | user: AuthUser = null; | |
36 | videos: Video[] = []; | |
37 | ||
38 | private search: Search; | |
39 | private subActivatedRoute: any; | |
40 | private subSearch: 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 | if (this.authService.isLoggedIn()) { | |
53 | this.user = AuthUser.load(); | |
54 | } | |
55 | ||
56 | // Subscribe to route changes | |
57 | this.subActivatedRoute = this.route.params.subscribe(routeParams => { | |
58 | this.loadRouteParams(routeParams); | |
59 | ||
60 | // Update the search service component | |
61 | this.searchService.updateSearch.next(this.search); | |
62 | this.getVideos(); | |
63 | }); | |
64 | ||
65 | // Subscribe to search changes | |
66 | this.subSearch = this.searchService.searchUpdated.subscribe(search => { | |
67 | this.search = search; | |
68 | // Reset pagination | |
69 | this.pagination.currentPage = 1; | |
70 | ||
71 | this.navigateToNewParams(); | |
72 | }); | |
73 | } | |
74 | ||
75 | ngOnDestroy() { | |
76 | this.subActivatedRoute.unsubscribe(); | |
77 | this.subSearch.unsubscribe(); | |
78 | } | |
79 | ||
80 | getVideos() { | |
81 | this.loading.next(true); | |
82 | this.videos = []; | |
83 | ||
84 | this.changeDetector.detectChanges(); | |
85 | ||
86 | let observable = null; | |
87 | ||
88 | if (this.search.value) { | |
89 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort); | |
90 | } else { | |
91 | observable = this.videoService.getVideos(this.pagination, this.sort); | |
92 | } | |
93 | ||
94 | observable.subscribe( | |
95 | ({ videos, totalVideos }) => { | |
96 | this.videos = videos; | |
97 | this.pagination.totalItems = totalVideos; | |
98 | ||
99 | this.loading.next(false); | |
100 | }, | |
101 | error => alert(error.text) | |
102 | ); | |
103 | } | |
104 | ||
105 | isThereNoVideo() { | |
106 | return !this.loading.getValue() && this.videos.length === 0; | |
107 | } | |
108 | ||
109 | onPageChanged(event: any) { | |
110 | // Be sure the current page is set | |
111 | this.pagination.currentPage = event.page; | |
112 | ||
113 | this.navigateToNewParams(); | |
114 | } | |
115 | ||
116 | onRemoved(video: Video) { | |
117 | this.getVideos(); | |
118 | } | |
119 | ||
120 | onSort(sort: SortField) { | |
121 | this.sort = sort; | |
122 | ||
123 | this.navigateToNewParams(); | |
124 | } | |
125 | ||
126 | private buildRouteParams() { | |
127 | // There is always a sort and a current page | |
128 | const params: any = { | |
129 | sort: this.sort, | |
130 | page: this.pagination.currentPage | |
131 | }; | |
132 | ||
133 | // Maybe there is a search | |
134 | if (this.search.value) { | |
135 | params.field = this.search.field; | |
136 | params.search = this.search.value; | |
137 | } | |
138 | ||
139 | return params; | |
140 | } | |
141 | ||
142 | private loadRouteParams(routeParams) { | |
143 | if (routeParams['search'] !== undefined) { | |
144 | this.search = { | |
145 | value: routeParams['search'], | |
146 | field: <SearchField>routeParams['field'] | |
147 | }; | |
148 | } else { | |
149 | this.search = { | |
150 | value: '', | |
151 | field: 'name' | |
152 | }; | |
153 | } | |
154 | ||
155 | this.sort = <SortField>routeParams['sort'] || '-createdDate'; | |
156 | ||
157 | if (routeParams['page'] !== undefined) { | |
158 | this.pagination.currentPage = parseInt(routeParams['page']); | |
159 | } else { | |
160 | this.pagination.currentPage = 1; | |
161 | } | |
162 | ||
163 | this.changeDetector.detectChanges(); | |
164 | } | |
165 | ||
166 | private navigateToNewParams() { | |
167 | const routeParams = this.buildRouteParams(); | |
168 | this.router.navigate(['/videos/list', routeParams]); | |
169 | } | |
170 | } |