app/core/services/taxonomy-services/country-taxonomy.service.ts
Array
Properties |
Children |
Children:
|
ID |
ID:
|
Type : string
|
Title |
Title:
|
Type : string
|
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;
}
}