File

src/app/_services/location.service.ts

Index

Properties
Methods

Constructor

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

Methods

getAreaNameByLocation
getAreaNameByLocation(location: string, areaNames: object)
Parameters :
Name Type Optional
location string No
areaNames object No
Returns : string
getAreaNames
getAreaNames()
Returns : void
getAreaTypeByArea
getAreaTypeByArea(area: string, areaTypes: object)
Parameters :
Name Type Optional
area string No
areaTypes object No
Returns : string
getAreaTypes
getAreaTypes()
Returns : void

Properties

areaNames
Type : object
Default value : {}
Private areaNamesList
Type : BehaviorSubject<object>
Default value : new BehaviorSubject<object>(this.areaNames)
areaNamesSubject
Type : Observable<object>
Default value : this.areaNamesList.asObservable()
areaTypes
Type : object
Default value : {}
Private areaTypesList
Type : BehaviorSubject<object>
Default value : new BehaviorSubject<object>(this.areaTypes)
areaTypesSubject
Type : Observable<object>
Default value : this.areaTypesList.asObservable()
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { environment } from '@src/environments/environment';
import { first } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root',
})
export class LocationService {
  areaNames: object = {};
  private areaNamesList: BehaviorSubject<object> = new BehaviorSubject<object>(this.areaNames);
  areaNamesSubject: Observable<object> = this.areaNamesList.asObservable();

  areaTypes: object = {};
  private areaTypesList: BehaviorSubject<object> = new BehaviorSubject<object>(this.areaTypes);
  areaTypesSubject: Observable<object> = this.areaTypesList.asObservable();

  constructor(private httpClient: HttpClient) {}

  getAreaNames(): void {
    this.httpClient
      .get(`${environment.cicMetaUrl}/areanames`)
      .pipe(first())
      .subscribe((res: object) => this.areaNamesList.next(res));
  }

  getAreaNameByLocation(location: string, areaNames: object): string {
    const keywords = location.toLowerCase().split(' ');
    for (const keyword of keywords) {
      const queriedAreaName: string = Object.keys(areaNames).find((key) =>
        areaNames[key].includes(keyword)
      );
      if (queriedAreaName) {
        return queriedAreaName;
      }
    }
  }

  getAreaTypes(): void {
    this.httpClient
      .get(`${environment.cicMetaUrl}/areatypes`)
      .pipe(first())
      .subscribe((res: object) => this.areaTypesList.next(res));
  }

  getAreaTypeByArea(area: string, areaTypes: object): string {
    const keywords = area.toLowerCase().split(' ');
    for (const keyword of keywords) {
      const queriedAreaType: string = Object.keys(areaTypes).find((key) =>
        areaTypes[key].includes(keyword)
      );
      if (queriedAreaType) {
        return queriedAreaType;
      }
    }
  }
}

result-matching ""

    No results matching ""