You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
argos/dmp-frontend/src/app/ui/misc/search/search.component.ts

56 lines
1.9 KiB
TypeScript

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<SearchBarItem[]>;
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';
}
}
}