1 import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs/Subscription'
4 import { BehaviorSubject } from 'rxjs/BehaviorSubject'
6 import { NotificationsService } from 'angular2-notifications'
14 import { AuthService, AuthUser } from '../../core'
15 import { Search, SearchField, SearchService } from '../../shared'
16 import { } from '../../shared'
19 selector: 'my-videos-list',
20 styleUrls: [ './video-list.component.scss' ],
21 templateUrl: './video-list.component.html'
23 export class VideoListComponent implements OnInit, OnDestroy {
24 loading: BehaviorSubject<boolean> = new BehaviorSubject(false)
25 pagination: VideoPagination = {
34 private search: Search
35 private subActivatedRoute: Subscription
36 private subSearch: Subscription
39 private notificationsService: NotificationsService,
40 private authService: AuthService,
41 private changeDetector: ChangeDetectorRef,
42 private router: Router,
43 private route: ActivatedRoute,
44 private videoService: VideoService,
45 private searchService: SearchService
49 if (this.authService.isLoggedIn()) {
50 this.user = AuthUser.load()
53 // Subscribe to route changes
54 this.subActivatedRoute = this.route.params.subscribe(routeParams => {
55 this.loadRouteParams(routeParams)
57 // Update the search service component
58 this.searchService.updateSearch.next(this.search)
62 // Subscribe to search changes
63 this.subSearch = this.searchService.searchUpdated.subscribe(search => {
66 this.pagination.currentPage = 1
68 this.navigateToNewParams()
73 this.subActivatedRoute.unsubscribe()
74 this.subSearch.unsubscribe()
78 this.loading.next(true)
82 if (this.search.value) {
83 observable = this.videoService.searchVideos(this.search, this.pagination, this.sort)
85 observable = this.videoService.getVideos(this.pagination, this.sort)
89 ({ videos, totalVideos }) => {
91 this.pagination.totalItems = totalVideos
93 this.loading.next(false)
95 error => this.notificationsService.error('Error', error.text)
100 return !this.loading.getValue() && this.videos.length === 0
103 onPageChanged (event: { page: number }) {
104 // Be sure the current page is set
105 this.pagination.currentPage = event.page
107 this.navigateToNewParams()
110 onSort (sort: SortField) {
113 this.navigateToNewParams()
116 private buildRouteParams () {
117 // There is always a sort and a current page
120 page: this.pagination.currentPage
123 // Maybe there is a search
124 if (this.search.value) {
125 params['field'] = this.search.field
126 params['search'] = this.search.value
132 private loadRouteParams (routeParams: { [ key: string ]: any }) {
133 if (routeParams['search'] !== undefined) {
135 value: routeParams['search'],
136 field: routeParams['field'] as SearchField
145 this.sort = routeParams['sort'] as SortField || '-createdAt'
147 if (routeParams['page'] !== undefined) {
148 this.pagination.currentPage = parseInt(routeParams['page'], 10)
150 this.pagination.currentPage = 1
154 private navigateToNewParams () {
155 const routeParams = this.buildRouteParams()
156 this.router.navigate([ '/videos/list', routeParams ])