import { Client } from '../api/client.service' ; import { BehaviorSubject } from 'rxjs' ; import { Injectable , Inject } from '@angular/core' ; import { RedirectService } from './redirect.service' ; import { Location } from '@angular/common' ; @ Injectable ( { providedIn : 'root' } ) export class ConfigsService { private configs = { } ; public isReady$ = new BehaviorSubject ( false ) ; constructor ( private client , @ Inject ( 'QUERY_STRING' ) private queryString , private redirectService , private location ) { } async loadFromRemote ( ) { try { this . configs = await this . client . get ( `api/v1/minds/config${ this . queryString }` ) ; this . isReady$ . next ( true ) ; this . redirectToRootIfInvalidDomain ( ) ; } catch ( err ) { console . error ( err ) ; } } get ( key ) { return this . configs [ key ] || null ; } set ( key , value ) { this . configs [ key ] = value ; } return private redirectToRootIfInvalidDomain ( ) { if ( this . get ( 'redirect_to_root_on_init' ) === true ) { const redirectTo = this . get ( 'site_url' ) + this . location . path ( ) . substr ( 1 ) ; this . redirectService . redirect ( redirectTo ) ; throw `Invalid domain. Redirecting to ${ redirectTo }` ; } } }