File

src/app/services/report.service.ts

Index

Properties
Methods

Constructor

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

Methods

Private fetchUrls
fetchUrls()
getReportUrls
getReportUrls()
Returns : any

Properties

Private source
source:
Default value : new BehaviorSubject<IExportUrls | null>(null)
Private urls
urls: IExportUrls
Type : IExportUrls
Public urls$
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 { REPORT_API } from '../app.constants';
import { HttpClient } from '@angular/common/http';
import { of } from 'rxjs/observable/of';
import { IExportUrls } from '../models/i-export-urls.model';

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

  constructor(private http: HttpClient) {}

  getReportUrls() {
    console.log('getting report urls');
    if (this.urls) {
      console.log('report urls cache hit!');
      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 ""