]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/videos/video-list/video-list.component.ts
Client: Update to Angular RC4
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-list / video-list.component.ts
index b1ce5584526843dec4ada62c04edcaace8281dce..0ebf0ef5c459016e9a2bc9edafe7f25366eb311f 100644 (file)
@@ -1,5 +1,7 @@
-import { Component, OnInit } from '@angular/core';
-import { Router, ROUTER_DIRECTIVES, RouteParams } from '@angular/router-deprecated';
+import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
+import { AsyncPipe } from '@angular/common';
+import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from '@angular/router';
+import { BehaviorSubject } from 'rxjs/BehaviorSubject';
 
 import { PAGINATION_DIRECTIVES } from 'ng2-bootstrap/components/pagination';
 
@@ -13,56 +15,73 @@ import {
 import { AuthService, Search, SearchField, User } from '../../shared';
 import { VideoMiniatureComponent } from './video-miniature.component';
 import { VideoSortComponent } from './video-sort.component';
+import { SearchService } from '../../shared';
 
 @Component({
   selector: 'my-videos-list',
   styles: [ require('./video-list.component.scss') ],
+  pipes: [ AsyncPipe ],
   template: require('./video-list.component.html'),
   directives: [ LoaderComponent, PAGINATION_DIRECTIVES, ROUTER_DIRECTIVES, VideoMiniatureComponent, VideoSortComponent ]
 })
 
-export class VideoListComponent implements OnInit {
-  loading = false;
+export class VideoListComponent implements OnInit, OnDestroy {
+  loading: BehaviorSubject<boolean> = new BehaviorSubject(false);
   pagination: Pagination = {
     currentPage: 1,
     itemsPerPage: 9,
-    total: 0
+    totalItems: null
   };
   sort: SortField;
   user: User = null;
   videos: Video[] = [];
 
   private search: Search;
+  private sub: any;
 
   constructor(
     private authService: AuthService,
+    private changeDetector: ChangeDetectorRef,
     private router: Router,
-    private routeParams: RouteParams,
-    private videoService: VideoService
-  ) {
-    this.search = {
-      value: this.routeParams.get('search'),
-      field: <SearchField>this.routeParams.get('field')
-    };
-
-    this.sort = <SortField>this.routeParams.get('sort') || '-createdDate';
-  }
+    private route: ActivatedRoute,
+    private videoService: VideoService,
+    private searchService: SearchService
+  ) {}
 
   ngOnInit() {
-    if (this.authService.isLoggedIn()) {
-      this.user = User.load();
-    }
+    this.sub = this.route.params.subscribe(routeParams => {
+      if (this.authService.isLoggedIn()) {
+        this.user = User.load();
+      }
+
+      this.search = {
+        value: routeParams['search'],
+        field: <SearchField>routeParams['field']
+      };
+
+      // Update the search service component
+      this.searchService.searchChanged.next(this.search);
 
-    this.getVideos();
+      this.sort = <SortField>routeParams['sort'] || '-createdDate';
+
+      this.getVideos();
+    });
+  }
+
+  ngOnDestroy() {
+    this.sub.unsubscribe();
   }
 
-  getVideos() {
-    this.loading = true;
+  getVideos(detectChanges = true) {
+    this.loading.next(true);
     this.videos = [];
+    this.pagination.currentPage = 1;
+
+    this.changeDetector.detectChanges();
 
     let observable = null;
 
-    if (this.search.value !== null) {
+    if (this.search.value) {
       observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
     } else {
       observable = this.videoService.getVideos(this.pagination, this.sort);
@@ -71,9 +90,9 @@ export class VideoListComponent implements OnInit {
     observable.subscribe(
       ({ videos, totalVideos }) => {
         this.videos = videos;
-        this.pagination.total = totalVideos;
+        this.pagination.totalItems = totalVideos;
 
-        this.loading = false;
+        this.loading.next(false);
       },
       error => alert(error)
     );
@@ -84,7 +103,7 @@ export class VideoListComponent implements OnInit {
   }
 
   onRemoved(video: Video) {
-    this.videos.splice(this.videos.indexOf(video), 1);
+    this.getVideos(false);
   }
 
   onSort(sort: SortField) {
@@ -99,7 +118,6 @@ export class VideoListComponent implements OnInit {
       params.search = this.search.value;
     }
 
-    this.router.navigate(['VideosList', params]);
-    this.getVideos();
+    this.router.navigate(['/videos/list', params]);
   }
 }