1 import { OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs/Subscription'
4 import { BehaviorSubject } from 'rxjs/BehaviorSubject'
5 import { Observable } from 'rxjs/Observable'
7 import { NotificationsService } from 'angular2-notifications'
15 export abstract class AbstractVideoList implements OnInit, OnDestroy {
16 loading: BehaviorSubject<boolean> = new BehaviorSubject(false)
17 pagination: VideoPagination = {
25 protected notificationsService: NotificationsService
26 protected router: Router
27 protected route: ActivatedRoute
29 protected subActivatedRoute: Subscription
31 abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}>
34 // Subscribe to route changes
35 this.subActivatedRoute = this.route.params.subscribe(routeParams => {
36 this.loadRouteParams(routeParams)
43 this.subActivatedRoute.unsubscribe()
47 this.loading.next(true)
50 const observable = this.getVideosObservable()
53 ({ videos, totalVideos }) => {
55 this.pagination.totalItems = totalVideos
57 this.loading.next(false)
59 error => this.notificationsService.error('Error', error.text)
64 return !this.loading.getValue() && this.videos.length === 0
67 onPageChanged (event: { page: number }) {
68 // Be sure the current page is set
69 this.pagination.currentPage = event.page
71 this.navigateToNewParams()
74 onSort (sort: SortField) {
77 this.navigateToNewParams()
80 protected buildRouteParams () {
81 // There is always a sort and a current page
84 page: this.pagination.currentPage
90 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
91 this.sort = routeParams['sort'] as SortField || '-createdAt'
93 if (routeParams['page'] !== undefined) {
94 this.pagination.currentPage = parseInt(routeParams['page'], 10)
96 this.pagination.currentPage = 1
100 protected navigateToNewParams () {
101 const routeParams = this.buildRouteParams()
102 this.router.navigate([ '/videos/list', routeParams ])