First version with PostgreSQL
[oweals/peertube.git] / client / src / app / shared / search / search.component.ts
index ed1ce807ac0145f917aa04e396df9a35d2d42d58..9f7e156ecaeea7d77300cc293578e4d58686553e 100644 (file)
@@ -1,25 +1,21 @@
-import { Component, EventEmitter, Output, OnInit } from '@angular/core';
-
-import { DROPDOWN_DIRECTIVES} from  'ng2-bootstrap/components/dropdown';
+import { Component, OnInit } from '@angular/core';
+import { Router } from '@angular/router';
 
 import { Search } from './search.model';
 import { SearchField } from './search-field.type';
-import { SearchService } from './search.service'; // Temporary
+import { SearchService } from './search.service';
 
 @Component({
-    selector: 'my-search',
-    template: require('./search.component.html'),
-    directives: [ DROPDOWN_DIRECTIVES ]
+  selector: 'my-search',
+  templateUrl: './search.component.html'
 })
 
 export class SearchComponent implements OnInit {
-  @Output() search = new EventEmitter<Search>();
-
   fieldChoices = {
     name: 'Name',
     author: 'Author',
-    podUrl: 'Pod Url',
-    magnetUri: 'Magnet Uri',
+    host: 'Pod Host',
+    magnetUri: 'Magnet URI',
     tags: 'Tags'
   };
   searchCriterias: Search = {
@@ -27,10 +23,12 @@ export class SearchComponent implements OnInit {
     value: ''
   };
 
-  constructor(private searchService: SearchService) {}
+  constructor(private searchService: SearchService, private router: Router) {}
 
   ngOnInit() {
-    this.searchService.searchChanged.subscribe(
+    // Subscribe if the search changed
+    // Usually changed by videos list component
+    this.searchService.updateSearch.subscribe(
       newSearchCriterias => {
         // Put a field by default
         if (!newSearchCriterias.field) {
@@ -51,11 +49,18 @@ export class SearchComponent implements OnInit {
     $event.stopPropagation();
 
     this.searchCriterias.field = choice;
-    this.doSearch();
+
+    if (this.searchCriterias.value) {
+      this.doSearch();
+    }
   }
 
   doSearch() {
-    this.search.emit(this.searchCriterias);
+    if (this.router.url.indexOf('/videos/list') === -1) {
+      this.router.navigate([ '/videos/list' ]);
+    }
+
+    this.searchService.searchUpdated.next(this.searchCriterias);
   }
 
   getStringChoice(choiceKey: SearchField) {