import { HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { Component, EventEmitter, Inject, Output } from '@angular/core'; import { Router } from '@angular/router'; import { Subscription, timer } from 'rxjs'; import { ArticleDiscoveryService } from '../../article-discovery.service'; import { IAppConstants } from '../../helpers/constants/app-constants.interface'; import { AppConstants } from '../../helpers/constants/app.constants'; import { ScriptService } from '../../helpers/script/script.helper'; import { QueryParamsModel } from '../../models/query-params.model'; import { SearchFilterModel } from '../../models/search-filter.model'; import { BaseService } from '../../services/base.service'; import { GlobalDataService } from '../../services/global-data.service'; import { GoogleTagManagerService } from '../../services/gtm.service'; import { SharedService } from '../../services/shared.service'; @Component({ // tslint:disable-next-line: component-selector selector: 'app-header', templateUrl: './header.component.html', }) export class HeaderComponent { @Output() articleHandler: EventEmitter = new EventEmitter(); // #region Private Fields private searchTermMaxLimit = 100; private searchSubscription: Subscription; private searchDelaySubscription: Subscription; // #endregion Private Fields // #region Constructor public constructor( public router: Router, public baseService: BaseService, public globalData: GlobalDataService, public sharedService: SharedService, public scriptService: ScriptService, public gtmService: GoogleTagManagerService, public articleService: ArticleDiscoveryService, @Inject(AppConstants) private constants: IAppConstants) { } // #endregion Constructor // #region EventListners public searchTriggerHandler(event?: any): void { const keyCode = event ? event.keyCode : 0; if (keyCode === 13 || keyCode === 8) { if (this.isSearchTermUpdated()) { this.triggerSearch(event); } } } public triggerSearch(event?: any): void { let keyCode = event ? event.keyCode : 0; keyCode = keyCode || 0; const query = this.globalData.SearchQuery.trim() || this.constants.DEFAULT_STRING_VALUE; if (this.sharedService.IsSearchTermLimitExceeded()) { return; } this.managePageSettings(); const previousSearchTerm = this.globalData.SearchTerm || this.constants.DEFAULT_STRING_VALUE; const isSearchTermUpdate = previousSearchTerm.length > 0; this.sharedService.ToggleLandingPage(); this.articleHandler.emit({ isSearchTermUpdate }); const isValidQuery = this.sharedService.IsValidSearchTerm(query); if (isValidQuery) { this.globalData.IsHintIndicatorVisible = false; if (this.isSearchTermUpdated()) { this.sharedService.ManageSearchSpinner(true); if (query.length === 0) { this.getSearchResult(); } else { this.activateCallBack(keyCode); } } } else { this.cancelSearchRequest(); this.sharedService.ManageSearchSpinner(false); this.articleHandler.emit(); } this.globalData.SearchTerm = query; this.globalData.JournalFilter = this.constants.DEFAULT_STRING_VALUE; } // #endregion EventListners // #region Public Methods // #endregion Public Methods // #region Private Methods private isSearchTermUpdated(): boolean { const query = this.globalData.SearchQuery.trim() || this.constants.DEFAULT_STRING_VALUE; return this.globalData.SearchTerm !== query; } private activateCallBack(keyCode: number): void { if (keyCode === 13) { this.setSearchDelaySubscription(0); } else { this.setSearchDelaySubscription(this.globalData.KeyStrokeDelay); } } private setSearchDelaySubscription(keystrokeDelay: number): void { this.cancelSearchDelayRequest(); this.searchDelaySubscription = timer(keystrokeDelay).subscribe(() => { this.getSearchResult(); this.sharedService.trackUserActionSearchTriggered(); }); } private cancelSearchRequest(): void { if (this.searchSubscription) { this.searchSubscription.unsubscribe(); this.searchSubscription = null; } } private cancelSearchDelayRequest(): void { if (this.searchDelaySubscription) { this.searchDelaySubscription.unsubscribe(); this.searchDelaySubscription = null; } } private isValidSearchQuery(): boolean { const query = this.globalData.SearchQuery.trim() || this.constants.DEFAULT_STRING_VALUE; const isValid = (query.length > 0 && query.length < 3) ? false : true; return isValid; } private managePageSettings(): void { const query = this.globalData.SearchQuery.trim() || this.constants.DEFAULT_STRING_VALUE; this.sharedService.ManageHintIndicators(); this.globalData.IsSearchContainerActive = query.length > 0 ? true : false; } private generateSearchFilters(): SearchFilterModel { const filter = new SearchFilterModel(); filter.Skip = 0; filter.Top = this.globalData.DefaultPageSize; filter.Search = this.globalData.SearchQuery.trim(); filter.PageNumber = this.globalData.QueryPageNumber; return filter; } private managePageDefaults(flag?: boolean) { flag = flag || false; this.sharedService.ManageRequest(flag); this.globalData.IsPageInitialization = flag; this.sharedService.ManageSearchSpinner(flag); this.globalData.IsPageFilterUpdated = false; } private manageArticleRedirection() { const querParam = new QueryParamsModel(); querParam.Query = this.globalData.SearchTerm; const siteUrl = this.baseService.GetUpdatedUrl(querParam); this.router.navigateByUrl(siteUrl); } // #endregion Private Methods // #region Service Methods private getSearchResult(): void { this.baseService.ManageArticleRedirection(); this.cancelSearchRequest(); if (this.isValidSearchQuery()) { this.sharedService.ManageRequest(true); this.searchSubscription = this.articleService.getSearch(this.generateSearchFilters()) .subscribe( (response: HttpResponse) => { if (response.ok) { const data = response.body; this.sharedService.ManageSearchData(data); this.sharedService.ManagePagination(); if (this.globalData.Summary.Count > 0) { this.sharedService.trackUserActionSearchResultsLoaded(); } } this.sharedService.LoadAltMetricScript('https://d1bxh8uas1mnw7.cloudfront.net/assets/embed.js'); this.managePageDefaults(); this.articleHandler.emit({ isReplaceHistory: true }); }, (error: HttpErrorResponse) => { let message = ''; if (error.status === this.constants.API_RESPONSE_STATUS_NOT_FOUND) { this.sharedService.LoadScript(this.globalData.QueryPageNumber > 0 ? this.globalData.QueryPageNumber : 1, error.status); if (this.globalData.QueryPageNumber > 0) { message = this.constants.MESSAGE_NO_MATCH_FOUND; } else { message = this.constants.MESSAGE_NO_RESULTS_FOUND; } } else { message = this.constants.MESSAGE_SEARCH_ERROR; } if (message.length > 0) { this.sharedService.ManageHintIndicators(message, true); } else { this.sharedService.ManageSearchData(); } this.managePageDefaults(); } ); } } }