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