Fixes für input fields, #60 -> AuditService
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { BehaviorSubject, lastValueFrom, Observable } from 'rxjs';
|
||||
import { CityAndStateResult, CountyResult, GeoResult, IpInfo } from '../../../../bizmatch-server/src/models/main.model';
|
||||
import { Place } from '../../../../bizmatch-server/src/models/server.model';
|
||||
import { environment } from '../../environments/environment';
|
||||
@@ -11,6 +11,9 @@ import { environment } from '../../environments/environment';
|
||||
export class GeoService {
|
||||
private apiBaseUrl = environment.apiBaseUrl;
|
||||
private baseUrl: string = 'https://nominatim.openstreetmap.org/search';
|
||||
private ipInfo$ = new BehaviorSubject<IpInfo | null>(null);
|
||||
private fetchingData: Observable<IpInfo> | null = null;
|
||||
private readonly storageKey = 'ipInfo';
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
findCitiesStartingWith(prefix: string, state?: string): Observable<GeoResult[]> {
|
||||
@@ -27,7 +30,49 @@ export class GeoService {
|
||||
let headers = new HttpHeaders().set('X-Hide-Loading', 'true').set('Accept-Language', 'en-US');
|
||||
return this.http.get(`${this.baseUrl}?q=${prefix},US&format=json&addressdetails=1&limit=5`, { headers }) as Observable<Place[]>;
|
||||
}
|
||||
fetchIpAndGeoLocation(): Observable<IpInfo> {
|
||||
private fetchIpAndGeoLocation(): Observable<IpInfo> {
|
||||
return this.http.get<IpInfo>(`${this.apiBaseUrl}/bizmatch/geo/ipinfo/georesult/wysiwyg`);
|
||||
}
|
||||
|
||||
// getIpInfo(): Observable<IpInfo | null> {
|
||||
// if (this.ipInfo$.getValue() !== null) {
|
||||
// // Wenn wir bereits Daten haben, geben wir sie sofort zurück
|
||||
// return this.ipInfo$.asObservable();
|
||||
// } else if (this.fetchingData) {
|
||||
// // Wenn wir gerade Daten abrufen, geben wir diesen Observable zurück
|
||||
// return this.fetchingData;
|
||||
// } else {
|
||||
// // Ansonsten initiieren wir den Abruf
|
||||
// this.fetchingData = this.fetchIpAndGeoLocation().pipe(
|
||||
// tap(data => this.ipInfo$.next(data)),
|
||||
// catchError(error => {
|
||||
// console.error('Error fetching IP info:', error);
|
||||
// this.ipInfo$.next(null);
|
||||
// return of(null);
|
||||
// }),
|
||||
// shareReplay(1),
|
||||
// );
|
||||
// return this.fetchingData;
|
||||
// }
|
||||
// }
|
||||
async getIpInfo(): Promise<IpInfo | null> {
|
||||
// Versuche zuerst, die Daten aus dem sessionStorage zu holen
|
||||
const storedData = sessionStorage.getItem(this.storageKey);
|
||||
if (storedData) {
|
||||
return JSON.parse(storedData);
|
||||
}
|
||||
|
||||
try {
|
||||
// Wenn keine Daten im Storage, hole sie vom Server
|
||||
const data = await lastValueFrom(this.http.get<IpInfo>(`${this.apiBaseUrl}/bizmatch/geo/ipinfo/georesult/wysiwyg`));
|
||||
|
||||
// Speichere die Daten im sessionStorage
|
||||
sessionStorage.setItem(this.storageKey, JSON.stringify(data));
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching IP info:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user