import { ActivatedRoute, Router } from '@angular/router'
import { NotificationsService } from 'angular2-notifications'
import { Observable } from 'rxjs/Observable'
+import { AuthService } from '../../core/auth'
+import { ComponentPagination } from '../rest/component-pagination.model'
import { SortField } from './sort-field.type'
-import { VideoPagination } from './video-pagination.model'
import { Video } from './video.model'
export abstract class AbstractVideoList implements OnInit {
- pagination: VideoPagination = {
+ pagination: ComponentPagination = {
currentPage: 1,
itemsPerPage: 25,
totalItems: null
videos: Video[] = []
loadOnInit = true
- protected notificationsService: NotificationsService
- protected router: Router
- protected route: ActivatedRoute
+ protected abstract notificationsService: NotificationsService
+ protected abstract authService: AuthService
+ protected abstract router: Router
+ protected abstract route: ActivatedRoute
protected abstract currentRoute: string
abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}>
+ get user () {
+ return this.authService.getUser()
+ }
+
ngOnInit () {
// Subscribe to route changes
const routeParams = this.route.snapshot.params
observable.subscribe(
({ videos, totalVideos }) => {
// Paging is too high, return to the first one
- if (totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
+ if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
this.pagination.currentPage = 1
this.setNewRouteParams()
return this.reloadVideos()
this.videos = this.videos.concat(videos)
}
},
- error => this.notificationsService.error('Error', error.text)
+ error => this.notificationsService.error('Error', error.message)
)
}
protected hasMoreVideos () {
+ // No results
+ if (this.pagination.totalItems === 0) return false
+
+ // Not loaded yet
if (!this.pagination.totalItems) return true
const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage