src/app/services/delivery-date.service.ts
Properties |
Methods |
|
constructor(http: HttpClient, errorService: ErrorStore)
|
|||||||||
Defined in src/app/services/delivery-date.service.ts:20
|
|||||||||
Parameters :
|
Private handleError |
handleError(error: any, title: string, msg: string)
|
Defined in src/app/services/delivery-date.service.ts:44
|
Returns :
any
|
loadDeliveryDate | ||||||
loadDeliveryDate(domain: string)
|
||||||
Defined in src/app/services/delivery-date.service.ts:27
|
||||||
Parameters :
Returns :
Observable<string>
|
Public date$ |
date$:
|
Default value : this.publisher.asObservable()
|
Defined in src/app/services/delivery-date.service.ts:20
|
Private publisher |
publisher:
|
Default value : new BehaviorSubject<string>(null)
|
Defined in src/app/services/delivery-date.service.ts:19
|
import { HttpClient} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators/catchError';
import { map } from 'rxjs/operators/map';
import { filter } from 'rxjs/operators';
import { ErrorStore } from '../stores/error.store';
import { BehaviorSubject } from 'rxjs';
import { DELIVERY_DATE_API, ERROR_MESSAGE } from '../app.constants';
export interface IDeliveryDates {
EarliestStandardElectronicScheduleForDate: string;
}
@Injectable()
export class DeliveryDateService {
private publisher = new BehaviorSubject<string>(null);
public date$ = this.publisher.asObservable();
constructor(
private http: HttpClient,
private errorService: ErrorStore
) {}
loadDeliveryDate(domain: string): Observable<string> {
return this.http
.get<IDeliveryDates>(`${DELIVERY_DATE_API}?domain=${domain}`)
.pipe(
map((response) => {
filter((response) => !!response),
console.log(response);
this.publisher.next(response.EarliestStandardElectronicScheduleForDate);
return response.EarliestStandardElectronicScheduleForDate;
}),
catchError((error) => {
const title = 'Failed to get delivery dates.';
return this.handleError(error, title);
})
);
}
private handleError(error: any, title: string, msg: string = null) {
let message = ERROR_MESSAGE;
if (!!msg) {
message = msg;
}
this.errorService.showError(title, message);
return Observable.of(null);
}
}