- Timestamp:
- 01/28/22 18:45:54 (3 years ago)
- Branches:
- master
- Children:
- 6fe77af
- Parents:
- b738035
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/src/app/explore/explore.component.ts
rb738035 r76712b2 1 1 import { Component, OnInit } from '@angular/core'; 2 import { City } from '../_models/city'; 3 import { CityService } from '../_services/city.service'; 2 import { FormControl } from '@angular/forms'; 3 import { Router } from '@angular/router'; 4 import { Observable, Subscription } from 'rxjs'; 5 import { map, startWith } from 'rxjs/operators'; 6 import { LocationService } from '../_services/location.service'; 7 import { RegionService } from '../_services/region.service'; 4 8 5 9 … … 11 15 export class ExploreComponent implements OnInit { 12 16 13 cities: City[];14 filteredCountries: any[];15 17 text: string; 16 18 loading = [false, false, false, false]; 19 regions: string[] = []; 20 filteredOptions: Observable<any[]> = new Observable<any[]>(); 21 myControl = new FormControl(); 22 cityName: string = ''; 23 selectedPlace: string = ''; 24 thirdSubscription: Subscription = new Subscription; 17 25 18 constructor(private cityService: CityService) { 19 this.cities = []; 20 this.filteredCountries = []; 26 constructor(private locationService: LocationService, 27 private regionService: RegionService, private router: Router) { 21 28 this.text = ''; 22 29 } 23 30 24 31 ngOnInit(): void { 25 this.cityService.getAllCities().subscribe( 26 cities => { 27 this.cities = cities; 32 33 this.regionService.getAllCitiesAndRegions().subscribe( 34 data => { 35 this.regions = data; 28 36 } 37 ); 38 39 this.filteredOptions = this.myControl.valueChanges.pipe( 40 startWith(''), 41 map(value => (typeof value === 'string' ? value : value.name)), 42 map(name => (name ? this._filter(name) : this.regions.slice())), 29 43 ); 30 44 } 31 45 32 search(event) { 33 let filtered: any[] = []; 34 let query = event.query; 35 for (let i = 0; i < this.cities.length; i++) { 36 let city = this.cities[i]; 37 if (city.name.toLowerCase().indexOf(query.toLowerCase()) == 0) { 38 filtered.push(city); 39 } 40 } 41 this.filteredCountries = filtered; 46 displayFn(city: string): string { 47 return city && city ? city : ''; 48 } 49 50 private _filter(name: string): any[] { 51 const filterValue = name.toLowerCase(); 52 return this.regions.filter(option => option.toLowerCase().includes(filterValue)); 42 53 } 43 54 … … 45 56 this.loading[index] = true; 46 57 setTimeout(() => this.loading[index] = false, 1000); 58 this.locationService.getAllLocationsSearch(this.selectedPlace).subscribe( 59 data => { 60 this.router.navigate(['results'], { queryParams: { place: this.selectedPlace } }); 61 } 62 ); 63 } 64 65 onPlaceSelected(selectedPlace) { 66 console.log(this.selectedPlace); // get from view 47 67 } 48 68 }
Note:
See TracChangeset
for help on using the changeset viewer.