src/app/services/delivery-date.service.ts
Properties |
EarliestStandardElectronicScheduleForDate |
EarliestStandardElectronicScheduleForDate:
|
Type : string
|
Defined in src/app/services/delivery-date.service.ts:14
|
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);
}
}