1 import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
2 import { ActivatedRoute, Router } from '@angular/router';
3 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
5 import { NotificationsService } from 'angular2-notifications';
12 import { AuthService, AuthUser } from '../../core';
13 import { RestPagination, Search, SearchField } from '../../shared';
14 import { SearchService } from '../../shared';
17 selector: 'my-videos-list',
18 styleUrls: [ './video-list.component.scss' ],
19 templateUrl: './video-list.component.html'
21 export class VideoListComponent implements OnInit, OnDestroy {
22 loading: BehaviorSubject<boolean> = new BehaviorSubject(false);
23 pagination: RestPagination = {
29 user: AuthUser = null;
32 private search: Search;
33 private subActivatedRoute: any;
34 private subSearch: any;
37 private notificationsService: NotificationsService,
38 private authService: AuthService,
39 private changeDetector: ChangeDetectorRef,
40 private router: Router,
41 private route: ActivatedRoute,
42 private videoService: VideoService,
43 private searchService: SearchService
47 if (this.authService.isLoggedIn()) {
48 this.user = AuthUser.load();
51 // Subscribe to route changes
52 this.subActivatedRoute = this.route.params.subscribe(routeParams => {
53 this.loadRouteParams(routeParams);
55 // Update the search service component
56 this.searchService.updateSearch.next(this.search);
60 // Subscribe to search changes
61 this.subSearch = this.searchService.searchUpdated.subscribe(search => {
64 this.pagination.currentPage = 1;
66 this.navigateToNewParams();
71 this.subActivatedRoute.unsubscribe();
72 this.subSearch.unsubscribe();
76 this.loading.next(true);
79 let observable = null;
80 if (this.search.value) {
81 observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
83 observable = this.videoService.getVideos(this.pagination, this.sort);
87 ({ videos, totalVideos }) => {
89 this.pagination.totalItems = totalVideos;
91 this.loading.next(false);
93 error => this.notificationsService.error('Error', error.text)
98 return !this.loading.getValue() && this.videos.length === 0;
101 onPageChanged(event: any) {
102 // Be sure the current page is set
103 this.pagination.currentPage = event.page;
105 this.navigateToNewParams();
108 onSort(sort: SortField) {
111 this.navigateToNewParams();
114 private buildRouteParams() {
115 // There is always a sort and a current page
116 const params: any = {
118 page: this.pagination.currentPage
121 // Maybe there is a search
122 if (this.search.value) {
123 params.field = this.search.field;
124 params.search = this.search.value;
130 private loadRouteParams(routeParams) {
131 if (routeParams['search'] !== undefined) {
133 value: routeParams['search'],
134 field: <SearchField>routeParams['field']
143 this.sort = <SortField>routeParams['sort'] || '-createdAt';
145 if (routeParams['page'] !== undefined) {
146 this.pagination.currentPage = parseInt(routeParams['page']);
148 this.pagination.currentPage = 1;
152 private navigateToNewParams() {
153 const routeParams = this.buildRouteParams();
154 this.router.navigate(['/videos/list', routeParams]);