File

src/app/services/delivery-date.service.ts

Index

Properties
Methods

Constructor

constructor(http: HttpClient, errorService: ErrorStore)
Parameters :
Name Type Optional
http HttpClient no
errorService ErrorStore no

Methods

Private handleError
handleError(error: any, title: string, msg: string)
Parameters :
Name Type Optional Default value
error any no
title string no
msg string no null
Returns : any
loadDeliveryDate
loadDeliveryDate(domain: string)
Parameters :
Name Type Optional
domain string no
Returns : Observable<string>

Properties

Public date$
date$:
Default value : this.publisher.asObservable()
Private publisher
publisher:
Default value : new BehaviorSubject<string>(null)
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);
  }
}

results matching ""

    No results matching ""