import {Injectable} from '@angular/core'; import {HttpClient} from "@angular/common/http"; import {from, Observable, Subscription} from 'rxjs'; import {EnvProperties} from "../properties/env-properties"; import {Portal} from "../entities/adminTool/portal"; import {Page} from "../entities/adminTool/page"; import {properties} from "../../../../environments/environment"; import {AdvancedAsyncSubject} from "../AdvancedAsyncSubject"; @Injectable({providedIn: 'root'}) export class ConfigurationService { private portal: AdvancedAsyncSubject = new AdvancedAsyncSubject(); private sub: Subscription = null; private promise: Promise = null; constructor(private http: HttpClient) { } ngOnDestroy() { this.clearSubscriptions(); } clearSubscriptions() { if (this.sub) { this.sub.unsubscribe(); } } public initPortal(properties: EnvProperties, pid: string) { if (pid == null) return; let url = properties.adminToolsAPIURL + "/" + properties.adminToolsPortalType + "/" + pid + "/full"; this.promise = new Promise((resolve => { this.sub = this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url).subscribe( (communityInformation: Portal) => { this.portal.next(communityInformation); resolve(); }, error => { this.portal.error(error); resolve(); }); })); } public get portalAsObservable(): Observable { return this.portal.asObservable(); } async isPageEnabledAsync(properties: EnvProperties, portal: string, page_route: string) { if (!this.promise || (this.portal && this.portal.getValue() && portal !== this.portal.getValue().pid)) { this.initPortal(properties, portal); } await this.promise; if (this.sub) { this.sub.unsubscribe(); } return this.filtering(page_route); } isPageEnabled(properties: EnvProperties, portal: string, router: string): Observable { let page_route: string = router.split('?')[0].substring(1); return from(this.isPageEnabledAsync(properties, portal, page_route)); } filtering(page_route: string) { let community: Portal = this.portal.getValue(); let pages: Page[] = community.pages; if (pages) { let page = pages.find((page: Page) => page.route == page_route); return page && page.isEnabled; } else { return false; } } }