File

src/app/providers/services/report.service.ts

Index

Properties
Methods

Constructor

constructor(http: HttpClient)
Parameters :
Name Type Optional
http HttpClient No

Methods

clearCache
clearCache()
Returns : void
Private fetchUrls
fetchUrls()
Returns : Observable<IExportUrls>
getReportUrls
getReportUrls()
Returns : any

Properties

Private source
Default value : new BehaviorSubject<IExportUrls | null>(null)
Private urls
Type : IExportUrls
Public urls$
Default value : this.source.asObservable()
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Injectable } from '@angular/core';
import { tap, catchError, timeout, first } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { of } from 'rxjs/observable/of';
import { IExportUrls } from '../../models/i-export-urls.model';
import { REPORT_API } from '../../app.constants';

@Injectable()
export class ReportService {
  private urls: IExportUrls;
  private source = new BehaviorSubject<IExportUrls | null>(null);
  public urls$ = this.source.asObservable();

  constructor(private http: HttpClient) {}

  clearCache() {
    this.urls = null;
    this.source.next(this.urls);
  }

  getReportUrls() {
    console.log('getting report urls');
    if (this.urls) {
      console.log('report urls cache hit!', this.urls);
      this.source.next(this.urls);
      return this.urls$;
    }
    return this.fetchUrls();
  }

  private fetchUrls(): Observable<IExportUrls> {
    console.log('fetching report urls from', REPORT_API);
    return this.http.get<IExportUrls>(REPORT_API).pipe(
      first((urls) => !!urls),
      tap((urls) => {
        if (urls) {
          console.log('cached urls', urls);
          this.urls = urls;
          this.source.next(this.urls);
        }
      }),
      catchError((error) => {
        console.log(error);
        // todo handle error;
        return of({
          csv: '',
          pdf: '',
          xls: '',
        } as IExportUrls);
      })
    );
  }
}

results matching ""

    No results matching ""