]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video/abstract-video-list.ts
Improve video upload guard a little bit
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
index 84ca5cbe4081cb367a8db7692fcf6121cfb108b1..354373776245daf6bc8d1e7ca40f577ada62bc92 100644 (file)
@@ -2,12 +2,13 @@ import { OnInit } from '@angular/core'
 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
@@ -17,9 +18,10 @@ export abstract class AbstractVideoList implements OnInit {
   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
 
@@ -28,10 +30,15 @@ export abstract class AbstractVideoList implements OnInit {
 
   abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}>
 
+  get user () {
+    return this.authService.getUser()
+  }
+
   ngOnInit () {
     // Subscribe to route changes
     const routeParams = this.route.snapshot.params
     this.loadRouteParams(routeParams)
+
     if (this.loadOnInit === true) this.loadMoreVideos('after')
   }
 
@@ -60,6 +67,13 @@ export abstract class AbstractVideoList implements OnInit {
 
     observable.subscribe(
       ({ videos, totalVideos }) => {
+        // Paging is too high, return to the first one
+        if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
+          this.pagination.currentPage = 1
+          this.setNewRouteParams()
+          return this.reloadVideos()
+        }
+
         this.loadedPages[this.pagination.currentPage] = true
         this.pagination.totalItems = totalVideos
 
@@ -69,11 +83,15 @@ export abstract class AbstractVideoList implements OnInit {
           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