import {mergeMap, distinctUntilChanged, debounceTime} from 'rxjs/operators'; import { Component, OnInit } from "@angular/core"; import { AuthService } from '../../../core/services/auth/auth.service'; import { FormControl } from "@angular/forms"; import { SearchBarService } from '../../../core/services/search-bar/search-bar.service'; import { Router } from '@angular/router'; import { Observable } from "rxjs"; import { SearchBarItem } from "../../../core/model/dashboard/search-bar-item"; export enum SearchBarType { Dataset = 0, Dmp = 1, Grant = 2 } @Component({ selector: "app-search", templateUrl: "./search.component.html", styleUrls: ["./search.component.css"] }) export class SearchComponent implements OnInit { public search = false; public searchControl = new FormControl(); filteredOptions: Observable; constructor(private authentication: AuthService, private router: Router, private searchBarService: SearchBarService) {} ngOnInit() { this.filteredOptions = this.searchControl.valueChanges.pipe(debounceTime(500),distinctUntilChanged(),mergeMap(x => { return this.searchBarService.search(x); }),); } public isAuthenticated(): boolean { return !!this.authentication.current(); } onOptionSelected(event: any) { this.search = false; this.searchControl.patchValue(null); const selectedSearchBarItem = event.option.value; if (selectedSearchBarItem.type === SearchBarType.Dataset) { this.router.navigate(['datasets/edit/' + selectedSearchBarItem.id]); } if (selectedSearchBarItem.type === SearchBarType.Grant) { this.router.navigate(['grants/edit/' + selectedSearchBarItem.id]); } if (selectedSearchBarItem.type === SearchBarType.Dmp) { this.router.navigate(['plans/edit/' + selectedSearchBarItem.id]); } } transformType(type) { switch (type) { case SearchBarType.Dataset: return 'Dataset'; case SearchBarType.Dmp: return 'DMP'; case SearchBarType.Grant: return 'Grant'; } } }