src/app/services/report.service.ts
Properties |
Methods |
|
constructor(http: HttpClient)
|
||||||
Defined in src/app/services/report.service.ts:14
|
||||||
Parameters :
|
Private fetchUrls |
fetchUrls()
|
Defined in src/app/services/report.service.ts:27
|
Returns :
Observable<IExportUrls>
|
getReportUrls |
getReportUrls()
|
Defined in src/app/services/report.service.ts:18
|
Returns :
any
|
Private source |
source:
|
Default value : new BehaviorSubject<IExportUrls | null>(null)
|
Defined in src/app/services/report.service.ts:13
|
Private urls |
urls:
|
Type : IExportUrls
|
Defined in src/app/services/report.service.ts:12
|
Public urls$ |
urls$:
|
Default value : this.source.asObservable()
|
Defined in src/app/services/report.service.ts:14
|
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);
})
);
}
}