import { Inject , Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { CookieService } from 'ngx-cookie-service' ; import { DOCUMENT } from '@angular/common' ; import { LOCAL_STORAGE } from '@ng-web-apis/common' ; import { IframeService } from 'src/app/core/services/iframe/iframe.service' ; import { Store } from 'src/app/core/services/store/models/store' ; @ Injectable ( { providedIn : 'root' } ) export class StoreService { /**
     * Data stored.
     */ private readonly storageSubject$ : BehaviorSubject < Store > ; /**
     * Key to store data.
     */ private readonly storageKey = 'rubicData' ; /**
     * Is current app placed in iframe (In iframe localStorage using is not allow)
     */ private get isIframe ( ) : boolean { return this . iframeService . isIframe ; } constructor ( private readonly cookieService , @ Inject ( DOCUMENT ) private document , @ Inject ( LOCAL_STORAGE ) private localStorage , private readonly iframeService ) { const store = this . fetchData ( ) ; this . storageSubject$ = new BehaviorSubject < Store > ( store ) ; } param key param value public setItem < T extends keyof Store > ( key , value ) { const newData = { ... this . storageSubject$ . value , [ key ] : value } ; try { const jsonData = JSON . stringify ( newData ) ; if ( ! this . isIframe ) { this . localStorage ?. setItem ( this . storageKey , jsonData ) ; } else { this . document . cookie = `${ this . storageKey }=${ jsonData }` ; } } catch ( err ) { console . debug ( err ) ; } finally { this . storageSubject$ . next ( newData ) ; } } param key public getItem < T extends keyof Store > ( key ) { return this . storageSubject$ . value ?. [ key ] ; } /**
     * Fetch stored data from local storage or cookies.
     */ public fetchData ( ) { try { const cookie = this . cookieService . get ( this . storageKey ) ; const data = JSON . parse ( this . isIframe && cookie ? cookie : this . localStorage ?. getItem ( this . storageKey ) ) ; return ( data || { } ) as Store ; } catch ( err ) { console . debug ( err ) ; return { } as Store ; } } /**
     * Delete stored data.
     */ public deleteData ( ) { try { if ( ! this . isIframe ) { this . localStorage ?. removeItem ( this . storageKey ) ; } else { this . cookieService . delete ( this . storageKey ) ; } } catch ( err ) { console . debug ( err ) ; } finally { this . storageSubject$ . next ( null ) ; } } param key public deleteItem ( key ) { const newData = { ... this . storageSubject$ . value , [ key ] : undefined } ; try { const jsonData = JSON . stringify ( newData ) ; if ( ! this . isIframe ) { this . localStorage ?. setItem ( this . storageKey , jsonData ) ; } else { this . cookieService . set ( this . storageKey , jsonData ) ; } } catch ( err ) { console . debug ( err ) ; } finally { this . storageSubject$ . next ( newData ) ; } } /**
     * Clear all data in user storage.
     */ public clearStorage ( ) { try { if ( ! this . isIframe ) { this . localStorage ?. clear ( ) ; } } catch ( err ) { console . debug ( err ) ; } finally { this . storageSubject$ . next ( null ) ; } } }