File

app/core/services/taxonomy-services/country-taxonomy.service.ts

Index

Properties
Methods

Constructor

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

Methods

Public compare
compare(current: any, next: any)
Parameters :
Name Type Optional
current any No
next any No
Returns : 1 | -1 | 0
Public getBehaviorCountries
getBehaviorCountries()
Returns : Observable<any>
Public getBehaviorRegions
getBehaviorRegions()
Returns : Observable<any>
Public getBehaviorStates
getBehaviorStates()
Returns : Observable<any>
Public getCountries
getCountries()
Returns : void
Public getLoader
getLoader()
Returns : Observable<boolean>
Public getRegionStates
getRegionStates(selectedCountry: string)
Parameters :
Name Type Optional
selectedCountry string No
Returns : void
ngOnInit
ngOnInit()
Returns : void
Public setBehaviorStates
setBehaviorStates(states: States)
Parameters :
Name Type Optional
states States No
Returns : void
Public setPID
setPID(id: Number)
Parameters :
Name Type Optional
id Number No
Returns : void

Properties

Private _headers
Default value : new HttpHeaders({ 'Content-Type': 'application/json' })
Private _TAXONOMYSERVICEURL
Type : string
Public behaveCountries
Default value : new BehaviorSubject<Countries>(null)
Public behaveRegion
Default value : new BehaviorSubject<Regions>(null)
Public behaveState
Default value : new BehaviorSubject<States>(null)
countryList
Type : Countries
Public http
Type : HttpClient
Public loader
Default value : new BehaviorSubject<boolean>(false)
stateList
Type : States
import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, BehaviorSubject, of } from 'rxjs';
import { map} from 'rxjs/operators';
import { AppConfig } from 'src/app/app.config';

export interface Countries extends Array<{}> {
  ID: string;
  Title: string;
}

export interface States extends Array<{}> {
  ID: string;
  Title: string;
  Children: [];
}

export interface Regions extends Array<{}> {
  ID: string;
  Title: string;
  Children: [];
}

@Injectable({
  providedIn: 'root'
})
export class CountryTaxonomyService implements OnInit {

  private _TAXONOMYSERVICEURL: string;
  private _headers = new HttpHeaders({ 'Content-Type': 'application/json' });

  countryList: Countries;
  stateList: States;
  public behaveCountries = new BehaviorSubject<Countries>(null);
  public behaveState = new BehaviorSubject<States>(null);
  public behaveRegion = new BehaviorSubject<Regions>(null);
  public loader = new BehaviorSubject<boolean>(false);

  constructor(public http: HttpClient) { 
    /*
		 Right now PublicationID is set in service directly, latter it suppose to be
		 setted by passing publication ID of respective site. Like 68 is the pubID for UK publication.
		 we can get that from 'this.publicationService.getPublicationId()'
		 */
    this._TAXONOMYSERVICEURL = AppConfig.settings.countryTaxonomyService.url;
    this.setPID(AppConfig.settings.countryTaxonomyService.publicationId);
  }

  ngOnInit() { }

  public setPID(id: Number) {
    this._TAXONOMYSERVICEURL += id;
  }

  public getBehaviorCountries(): Observable<any> {
    this.getCountries();
    return this.behaveCountries.asObservable();
  }

  public getBehaviorStates(): Observable<any> {
    return this.behaveState.asObservable();
  }

  public getBehaviorRegions(): Observable<any> {
    return this.behaveRegion.asObservable();
  }

  public setBehaviorStates(states: States) {
    this.behaveState.next(states);
  }

  public getLoader(): Observable<boolean> {
    return this.loader.asObservable();
  }

  public getCountries() {
    this.loader.next(true);
    const countrySub = this.http.get(this._TAXONOMYSERVICEURL, { headers: this._headers })
      .pipe(map(response => {
        const country: Countries = response['Children'];
        return country;
      })).subscribe((countries) => {
        this.countryList = countries;
        this.behaveCountries.next(countries);
      }, (error) => { console.log(error); },
        () => { this.loader.next(false); countrySub.unsubscribe(); });
  }

  public getRegionStates(selectedCountry: string) {
    this.behaveRegion.next(null);
    this.behaveState.next(null);
    if(selectedCountry !== ''){
      this.loader.next(true);
      of(this.countryList.filter(function (o) { return o['Title'] === selectedCountry; }).pop()['ID'])
        .subscribe(countryTCM => {
          const RegionStateListUrl: string = this._TAXONOMYSERVICEURL + '/' + countryTCM;
          this.http.get(RegionStateListUrl, { headers: this._headers }).subscribe(res => {
  
            const regions: Regions = (res['regionList']) ? res['regionList'] : [];
            this.stateList = regions;
            this.behaveRegion.next(regions);
  
            const states: States = (res['stateList']) ? res['stateList'] : [];
            this.stateList = states;
            this.behaveState.next(states);
          }, (error) => { console.log(error);},
            () => { this.loader.next(false); });
        }, (error) => { console.log(error); },
          () => { });
    }    
  }

  public compare(current: any, next: any) {
    return (current.Title < next.Title) ? -1 : (current.Title > next.Title) ? 1 : 0;
  }

}

result-matching ""

    No results matching ""