From a49297aea59db758b6f3c33e9996e5b110ec8a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Fri, 28 Apr 2023 13:48:02 +0200 Subject: [PATCH] first version of location service --- src/app/home/home.component.ts | 33 ++++++++++++++++------------- src/app/service/location.service.ts | 25 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 src/app/service/location.service.ts diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index fb435c3..cee0c2a 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -1,11 +1,12 @@ import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {BehaviorSubject, distinctUntilChanged, fromEvent, Observable, Subject, Subscription} from "rxjs"; import {ReadjsonService} from "../service/readjson.service"; -import {Locations} from "../interface/data"; import {Router} from "@angular/router"; import {TranslateService} from '../service/translate.service'; import {ReadTranslateJsonService} from "../service/readTranslateJsonService"; import {homeTranslations} from "../interface/translations"; +import {LocationService} from "../service/location.service"; +import {LocationEntity} from "../interface/LocationEntity"; @Component({ @@ -18,13 +19,13 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy { @ViewChild('myInput') myInput?: ElementRef; - public locationsPopup: Subject = new Subject() + public locationsPopup: Subject = new Subject() subs: Subscription[] = []; backgroundColor: string | undefined; - locations: Locations[] = []; + locations: LocationEntity[] = []; allert: boolean = false; - locationsFiltrati: Locations[] = []; + locationsFiltrati: LocationEntity[] = []; luogoSelezionato: string = ''; suggerimentoAttivo: boolean = false; suggerimento: string = ''; @@ -32,22 +33,24 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy { 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 ngOnInit(): void { this.translations = this.readTranslationJsonService.getHomeTranslations(); console.log("translations loaded", this.translations) - - this.readjsonService.getLocations().subscribe(data => { - for (let i = 0; i < data.length; i++) { - this.locations.push(data[i]) - console.log(data[i]) - } - }); - - + this.locationService.getLocations() + .subscribe(locations => { + this.locations = locations; + console.log("locations loaded", this.locations) + }); this.allert = false; console.log("home init"); } @@ -84,7 +87,7 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy { setTimeout(() => { }, 1000); // 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) { // Show suggestion if at least one location is found this.suggerimentoAttivo = true; diff --git a/src/app/service/location.service.ts b/src/app/service/location.service.ts new file mode 100644 index 0000000..5916516 --- /dev/null +++ b/src/app/service/location.service.ts @@ -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("/progetto152/location"); + } + + + +}