]>
Commit | Line | Data |
---|---|---|
0629423c | 1 | import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; |
ab32b0fc | 2 | import { ActivatedRoute, Router } from '@angular/router'; |
0629423c | 3 | import { BehaviorSubject } from 'rxjs/BehaviorSubject'; |
dc8bc31b | 4 | |
7ddd02c9 C |
5 | import { NotificationsService } from 'angular2-notifications'; |
6 | ||
41a2aee3 | 7 | import { |
41a2aee3 C |
8 | SortField, |
9 | Video, | |
10 | VideoService | |
4a6995be | 11 | } from '../shared'; |
e2a2d6c8 C |
12 | import { AuthService, AuthUser } from '../../core'; |
13 | import { RestPagination, Search, SearchField } from '../../shared'; | |
00a44645 | 14 | import { SearchService } from '../../shared'; |
dc8bc31b C |
15 | |
16 | @Component({ | |
17 | selector: 'my-videos-list', | |
ec8d8440 C |
18 | styleUrls: [ './video-list.component.scss' ], |
19 | templateUrl: './video-list.component.html' | |
dc8bc31b | 20 | }) |
0629423c C |
21 | export class VideoListComponent implements OnInit, OnDestroy { |
22 | loading: BehaviorSubject<boolean> = new BehaviorSubject(false); | |
de59c48f | 23 | pagination: RestPagination = { |
32294074 C |
24 | currentPage: 1, |
25 | itemsPerPage: 9, | |
0629423c | 26 | totalItems: null |
aff038cd | 27 | }; |
cf20596c | 28 | sort: SortField; |
7da18e44 | 29 | user: AuthUser = null; |
4fd8aa32 | 30 | videos: Video[] = []; |
dc8bc31b | 31 | |
471bc22f | 32 | private search: Search; |
bddab65a C |
33 | private subActivatedRoute: any; |
34 | private subSearch: any; | |
98b01bac | 35 | |
dc8bc31b | 36 | constructor( |
7ddd02c9 | 37 | private notificationsService: NotificationsService, |
ccf6ed16 | 38 | private authService: AuthService, |
0629423c | 39 | private changeDetector: ChangeDetectorRef, |
4fd8aa32 | 40 | private router: Router, |
0629423c | 41 | private route: ActivatedRoute, |
00a44645 | 42 | private videoService: VideoService, |
0629423c | 43 | private searchService: SearchService |
00a44645 | 44 | ) {} |
dc8bc31b C |
45 | |
46 | ngOnInit() { | |
bddab65a | 47 | if (this.authService.isLoggedIn()) { |
7da18e44 | 48 | this.user = AuthUser.load(); |
bddab65a | 49 | } |
1553e15d | 50 | |
bddab65a C |
51 | // Subscribe to route changes |
52 | this.subActivatedRoute = this.route.params.subscribe(routeParams => { | |
53 | this.loadRouteParams(routeParams); | |
00a44645 | 54 | |
0629423c | 55 | // Update the search service component |
bddab65a C |
56 | this.searchService.updateSearch.next(this.search); |
57 | this.getVideos(); | |
58 | }); | |
00a44645 | 59 | |
bddab65a C |
60 | // Subscribe to search changes |
61 | this.subSearch = this.searchService.searchUpdated.subscribe(search => { | |
62 | this.search = search; | |
7eef9535 C |
63 | // Reset pagination |
64 | this.pagination.currentPage = 1; | |
0629423c | 65 | |
bddab65a | 66 | this.navigateToNewParams(); |
0629423c C |
67 | }); |
68 | } | |
00a44645 | 69 | |
0629423c | 70 | ngOnDestroy() { |
bddab65a C |
71 | this.subActivatedRoute.unsubscribe(); |
72 | this.subSearch.unsubscribe(); | |
dc8bc31b C |
73 | } |
74 | ||
7eef9535 | 75 | getVideos() { |
0629423c | 76 | this.loading.next(true); |
157cb9c9 | 77 | this.videos = []; |
0629423c C |
78 | |
79 | this.changeDetector.detectChanges(); | |
157cb9c9 | 80 | |
98b01bac C |
81 | let observable = null; |
82 | ||
00a44645 | 83 | if (this.search.value) { |
ccf6ed16 | 84 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort); |
98b01bac | 85 | } else { |
ccf6ed16 | 86 | observable = this.videoService.getVideos(this.pagination, this.sort); |
98b01bac C |
87 | } |
88 | ||
89 | observable.subscribe( | |
32294074 C |
90 | ({ videos, totalVideos }) => { |
91 | this.videos = videos; | |
0629423c | 92 | this.pagination.totalItems = totalVideos; |
4fd8aa32 | 93 | |
0629423c | 94 | this.loading.next(false); |
32294074 | 95 | }, |
7ddd02c9 | 96 | error => this.notificationsService.error('Error', error.text) |
dc8bc31b C |
97 | ); |
98 | } | |
99 | ||
bddab65a C |
100 | isThereNoVideo() { |
101 | return !this.loading.getValue() && this.videos.length === 0; | |
102 | } | |
103 | ||
104 | onPageChanged(event: any) { | |
105 | // Be sure the current page is set | |
106 | this.pagination.currentPage = event.page; | |
107 | ||
108 | this.navigateToNewParams(); | |
9bfe96e1 C |
109 | } |
110 | ||
cf20596c C |
111 | onSort(sort: SortField) { |
112 | this.sort = sort; | |
a99593ed | 113 | |
bddab65a C |
114 | this.navigateToNewParams(); |
115 | } | |
116 | ||
117 | private buildRouteParams() { | |
118 | // There is always a sort and a current page | |
a99593ed | 119 | const params: any = { |
bddab65a C |
120 | sort: this.sort, |
121 | page: this.pagination.currentPage | |
a99593ed C |
122 | }; |
123 | ||
bddab65a | 124 | // Maybe there is a search |
a99593ed | 125 | if (this.search.value) { |
a99593ed | 126 | params.field = this.search.field; |
4fd8aa32 | 127 | params.search = this.search.value; |
a99593ed C |
128 | } |
129 | ||
bddab65a C |
130 | return params; |
131 | } | |
132 | ||
133 | private loadRouteParams(routeParams) { | |
134 | if (routeParams['search'] !== undefined) { | |
135 | this.search = { | |
136 | value: routeParams['search'], | |
137 | field: <SearchField>routeParams['field'] | |
138 | }; | |
139 | } else { | |
140 | this.search = { | |
141 | value: '', | |
142 | field: 'name' | |
143 | }; | |
144 | } | |
145 | ||
feb4bdfd | 146 | this.sort = <SortField>routeParams['sort'] || '-createdAt'; |
bddab65a | 147 | |
7eef9535 C |
148 | if (routeParams['page'] !== undefined) { |
149 | this.pagination.currentPage = parseInt(routeParams['page']); | |
150 | } else { | |
151 | this.pagination.currentPage = 1; | |
152 | } | |
bddab65a C |
153 | |
154 | this.changeDetector.detectChanges(); | |
155 | } | |
156 | ||
157 | private navigateToNewParams() { | |
158 | const routeParams = this.buildRouteParams(); | |
159 | this.router.navigate(['/videos/list', routeParams]); | |
00a44645 | 160 | } |
dc8bc31b | 161 | } |