import { HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { Inject, Injectable } from '@angular/core'; import { Title } from '@angular/platform-browser'; import { ActivatedRoute, convertToParamMap, Router } from '@angular/router'; import { ArticleDiscoveryService } from '../../public-api'; import { IAppConstants } from '../helpers/constants/app-constants.interface'; import { AppConstants } from '../helpers/constants/app.constants'; import { NumberParsePipe } from '../helpers/pipes/parse-numeric.pipe'; import { TokenReplacePipe } from '../helpers/pipes/token-replace.pipe'; import { ScriptService } from '../helpers/script/script.helper'; import { BaseService } from './base.service'; import { GlobalDataService } from './global-data.service'; import { GoogleTagManagerService } from './gtm.service'; import { SharedService } from './shared.service'; @Injectable({ providedIn: 'root' }) export class SubjectService { // #region Constructor public constructor( public router: Router, public location: Location, public titleService: Title, public route: ActivatedRoute, public baseService: BaseService, public tokenPipe: TokenReplacePipe, public sharedService: SharedService, public scriptService: ScriptService, public globalData: GlobalDataService, public numberParsePipe: NumberParsePipe, public gtmService: GoogleTagManagerService, public articleService: ArticleDiscoveryService, @Inject(AppConstants) private constants: IAppConstants, ) { this.ManageRequest(); } // #region Constructor // #region Public Methods public ManageRequest(): void { let articleSlug: string = this.constants.DEFAULT_STRING_VALUE; this.globalData.IsPageInitialization = true; this.sharedService.ManagePageLoader(true); this.sharedService.ManageSearchSpinner(true); this.route.params.subscribe(params => { const map = convertToParamMap(params); if (map.keys.length > 0) { articleSlug = params[this.constants.QUERY_PARAMETER_ARTICLE_SLUG] || ''; } }); if (articleSlug.length > 0) { this.getArticleSubjects(articleSlug); } } public getDocumentTitle(subject: string): string { return this.tokenPipe.transform(this.constants.SUBJECTS_PAGE_TITLE_MESSAGE, this.constants.SUBJECTS_PAGE_TITLE_TOKEN, subject); } // #endregion Public Methods // #region Private Methods private showLoader(flag): void { flag = flag || false; this.globalData.Settings.IsContentLoaderVisible = flag; } private generateMetaTag(metTag: string, content: string): void { const metaTitle = document.createElement('meta'); metaTitle.name = metTag; metaTitle.content = content; document.head.appendChild(metaTitle); } // #endregion Private Methods // #region Ajax Methods private getArticleSubjects(slug: string): void { this.articleService.getArticleSubjects(slug) .subscribe( (response: HttpResponse) => { let subject = ''; let title = this.constants.ERROR_PAGE_TITLE; const data = response.body; if (response.ok) { subject = response.status === this.constants.API_RESPONSE_STATUS_CONTENT ? data.Subject : ''; } console.log(response); if (subject.trim().length > 0) { this.globalData.Settings.SubjectModel = Object.assign({}, data); title = this.getDocumentTitle(subject); this.generateMetaTag(this.constants.ELEMENT_ATTRIBUTE_META_DESCRIPTION, data.Description); this.globalData.IsNotFoundVisible = false; } else { this.globalData.IsPageInitialization = false; this.globalData.IsNotFoundVisible = true; } this.titleService.setTitle(title); }, (error: HttpErrorResponse) => { this.globalData.IsPageInitialization = false; this.globalData.IsNotFoundVisible = true; this.titleService.setTitle(this.constants.ERROR_PAGE_TITLE); } ); } // #endregions Ajax Methods }