import { Component , OnInit , Input , ChangeDetectorRef } from '@angular/core' ; import { AnalyticsDashboardService } from '../../dashboard.service' ; import { RecentService } from '../../../../../services/ux/recent' ; import { Session } from '../../../../../services/session' ; import { Client } from '../../../../../services/api' ; import { Filter , Option } from '../../../../../interfaces/dashboard' ; @ Component ( { selector : 'm-analytics__searchSuggestions' , templateUrl : './search-suggestions.component.html' , } ) export class AnalyticsSearchSuggestionsComponent implements OnInit { suggestions : Array < any > = [ ] ; recent : any [ ] ; q : string = '' ; private searchTimeout ; @ Input ( ) active : boolean ; @ Input ( 'q' ) set _q ( value ) { if ( this . searchTimeout ) { clearTimeout ( this . searchTimeout ) ; } this . q = value || '' ; if ( ! value ) { this . loadRecent ( ) ; this . suggestions = [ ] ; return ; } this . searchTimeout = setTimeout ( async ( ) => { this . loadRecent ( ) ; try { const response = await this . client . get ( 'api/v2/search/suggest' , { q : value , limit : 4 , } ) ; this . suggestions = response . entities ; console . log ( response . entities ) ; } catch ( e ) { console . error ( e ) ; this . suggestions = [ ] ; } } , 300 ) ; } constructor ( private analyticsService , public session , public client , public recentService , private cd ) { } ngOnInit ( ) { this . loadRecent ( ) ; } applyChannelFilter ( suggestion ) { let selection = suggestion . guid || 'test' ; const selectedFilterStr = `channel::${ selection }` ; this . analyticsService . updateFilter ( selectedFilterStr ) ; } loadRecent ( ) { if ( this . session . getLoggedInUser ( ) ) { this . recent = this . recentService . fetch ( 'recent' , 6 ) ; } } mousedown ( e ) { e . preventDefault ( ) ; setTimeout ( ( ) => { this . active = false ; this . detectChanges ( ) ; } , 300 ) ; } detectChanges ( ) { this . cd . markForCheck ( ) ; this . cd . detectChanges ( ) ; } }