]>
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 | 24 | currentPage: 1, |
383bfc83 | 25 | itemsPerPage: 25, |
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 | 78 | |
98b01bac | 79 | let observable = null; |
00a44645 | 80 | if (this.search.value) { |
ccf6ed16 | 81 | observable = this.videoService.searchVideos(this.search, this.pagination, this.sort); |
98b01bac | 82 | } else { |
ccf6ed16 | 83 | observable = this.videoService.getVideos(this.pagination, this.sort); |
98b01bac C |
84 | } |
85 | ||
86 | observable.subscribe( | |
32294074 C |
87 | ({ videos, totalVideos }) => { |
88 | this.videos = videos; | |
0629423c | 89 | this.pagination.totalItems = totalVideos; |
4fd8aa32 | 90 | |
0629423c | 91 | this.loading.next(false); |
32294074 | 92 | }, |
7ddd02c9 | 93 | error => this.notificationsService.error('Error', error.text) |
dc8bc31b C |
94 | ); |
95 | } | |
96 | ||
bddab65a C |
97 | isThereNoVideo() { |
98 | return !this.loading.getValue() && this.videos.length === 0; | |
99 | } | |
100 | ||
101 | onPageChanged(event: any) { | |
102 | // Be sure the current page is set | |
103 | this.pagination.currentPage = event.page; | |
104 | ||
105 | this.navigateToNewParams(); | |
9bfe96e1 C |
106 | } |
107 | ||
cf20596c C |
108 | onSort(sort: SortField) { |
109 | this.sort = sort; | |
a99593ed | 110 | |
bddab65a C |
111 | this.navigateToNewParams(); |
112 | } | |
113 | ||
114 | private buildRouteParams() { | |
115 | // There is always a sort and a current page | |
a99593ed | 116 | const params: any = { |
bddab65a C |
117 | sort: this.sort, |
118 | page: this.pagination.currentPage | |
a99593ed C |
119 | }; |
120 | ||
bddab65a | 121 | // Maybe there is a search |
a99593ed | 122 | if (this.search.value) { |
a99593ed | 123 | params.field = this.search.field; |
4fd8aa32 | 124 | params.search = this.search.value; |
a99593ed C |
125 | } |
126 | ||
bddab65a C |
127 | return params; |
128 | } | |
129 | ||
130 | private loadRouteParams(routeParams) { | |
131 | if (routeParams['search'] !== undefined) { | |
132 | this.search = { | |
133 | value: routeParams['search'], | |
134 | field: <SearchField>routeParams['field'] | |
135 | }; | |
136 | } else { | |
137 | this.search = { | |
138 | value: '', | |
139 | field: 'name' | |
140 | }; | |
141 | } | |
142 | ||
feb4bdfd | 143 | this.sort = <SortField>routeParams['sort'] || '-createdAt'; |
bddab65a | 144 | |
7eef9535 C |
145 | if (routeParams['page'] !== undefined) { |
146 | this.pagination.currentPage = parseInt(routeParams['page']); | |
147 | } else { | |
148 | this.pagination.currentPage = 1; | |
149 | } | |
bddab65a C |
150 | } |
151 | ||
152 | private navigateToNewParams() { | |
153 | const routeParams = this.buildRouteParams(); | |
154 | this.router.navigate(['/videos/list', routeParams]); | |
00a44645 | 155 | } |
dc8bc31b | 156 | } |