first version of location service

This commit is contained in:
2023-04-28 13:48:02 +02:00
parent 4ccd2fa70a
commit a49297aea5
2 changed files with 43 additions and 15 deletions

View File

@@ -1,11 +1,12 @@
import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {BehaviorSubject, distinctUntilChanged, fromEvent, Observable, Subject, Subscription} from "rxjs"; import {BehaviorSubject, distinctUntilChanged, fromEvent, Observable, Subject, Subscription} from "rxjs";
import {ReadjsonService} from "../service/readjson.service"; import {ReadjsonService} from "../service/readjson.service";
import {Locations} from "../interface/data";
import {Router} from "@angular/router"; import {Router} from "@angular/router";
import {TranslateService} from '../service/translate.service'; import {TranslateService} from '../service/translate.service';
import {ReadTranslateJsonService} from "../service/readTranslateJsonService"; import {ReadTranslateJsonService} from "../service/readTranslateJsonService";
import {homeTranslations} from "../interface/translations"; import {homeTranslations} from "../interface/translations";
import {LocationService} from "../service/location.service";
import {LocationEntity} from "../interface/LocationEntity";
@Component({ @Component({
@@ -18,13 +19,13 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild('myInput') myInput?: ElementRef; @ViewChild('myInput') myInput?: ElementRef;
public locationsPopup: Subject<Locations[]> = new Subject<Locations[]>() public locationsPopup: Subject<LocationEntity[]> = new Subject<LocationEntity[]>()
subs: Subscription[] = []; subs: Subscription[] = [];
backgroundColor: string | undefined; backgroundColor: string | undefined;
locations: Locations[] = []; locations: LocationEntity[] = [];
allert: boolean = false; allert: boolean = false;
locationsFiltrati: Locations[] = []; locationsFiltrati: LocationEntity[] = [];
luogoSelezionato: string = ''; luogoSelezionato: string = '';
suggerimentoAttivo: boolean = false; suggerimentoAttivo: boolean = false;
suggerimento: string = ''; suggerimento: string = '';
@@ -32,22 +33,24 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
translations: homeTranslations = {} as homeTranslations; translations: homeTranslations = {} as homeTranslations;
constructor(private readjsonService: ReadjsonService, private router: Router, private translateService: TranslateService, private readTranslationJsonService: ReadTranslateJsonService) { constructor(
private readjsonService: ReadjsonService,
private router: Router,
private translateService: TranslateService,
private readTranslationJsonService: ReadTranslateJsonService,
private locationService: LocationService
){
} }
// Initializes the component and loads translations and locations // Initializes the component and loads translations and locations
ngOnInit(): void { ngOnInit(): void {
this.translations = this.readTranslationJsonService.getHomeTranslations(); this.translations = this.readTranslationJsonService.getHomeTranslations();
console.log("translations loaded", this.translations) console.log("translations loaded", this.translations)
this.locationService.getLocations()
this.readjsonService.getLocations().subscribe(data => { .subscribe(locations => {
for (let i = 0; i < data.length; i++) { this.locations = locations;
this.locations.push(<Locations>data[i]) console.log("locations loaded", this.locations)
console.log(data[i])
}
}); });
this.allert = false; this.allert = false;
console.log("home init"); console.log("home init");
} }
@@ -84,7 +87,7 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
setTimeout(() => { setTimeout(() => {
}, 1000); }, 1000);
// Filter locations and store in a variable // Filter locations and store in a variable
this.locationsFiltrati = this.locations.filter((l: Locations) => l.location.toLowerCase().startsWith(locations.toLowerCase())); this.locationsFiltrati = this.locations.filter((l: LocationEntity) => l.location.toLowerCase().startsWith(locations.toLowerCase()));
if (this.locationsFiltrati.length > 0) { if (this.locationsFiltrati.length > 0) {
// Show suggestion if at least one location is found // Show suggestion if at least one location is found
this.suggerimentoAttivo = true; this.suggerimentoAttivo = true;

View File

@@ -0,0 +1,25 @@
import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {catchError, Observable} from "rxjs";
import {LocationEntity} from "../interface/LocationEntity";
const BASE_URL = "localhost:8080/progetto152/";
const GET_LOCATIONS = BASE_URL + "location";
@Injectable({
providedIn: 'root',
})
export class LocationService {
constructor(
private http: HttpClient,
) {}
getLocations(){
return this.http.get<LocationEntity[]>("/progetto152/location");
}
}