source: trip-planner-front/src/app/locations-form/locations-form.component.ts@ fa375fe

Last change on this file since fa375fe was fa375fe, checked in by Ema <ema_spirova@…>, 3 years ago

adding new components

  • Property mode set to 100644
File size: 2.3 KB
Line 
1import { Component, Injectable, OnInit } from '@angular/core';
2import { FormControl } from '@angular/forms';
3import {map, startWith, switchMap} from 'rxjs/operators';
4import {forkJoin, Observable} from 'rxjs';
5import { CityService } from '../_services/city.service';
6import { City } from '../_models/city';
7import { Country } from '../_models/country';
8import { CountryService } from '../_services/country.service';
9import { Companion } from '../_models/companion';
10import { CompanionService } from '../_services/companion.service';
11import { Category } from '../_models/category';
12import { CategoryService } from '../_services/cateogry.service';
13import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
14import { MatChip } from '@angular/material/chips';
15
16@Component({
17 selector: 'app-locations-form',
18 templateUrl: './locations-form.component.html',
19 styleUrls: ['./locations-form.component.css']
20})
21export class LocationsFormComponent implements OnInit {
22
23 myControl = new FormControl();
24 cities: City[];
25 countries: Country[];
26 companions: Companion[];
27 categories: Category[];
28 filteredOptions: Observable<City[]>;
29 disableSelect = new FormControl(false);
30
31 constructor(private cityService : CityService, private countryService : CountryService,
32 private companionService : CompanionService, private categoryService : CategoryService){
33 this.filteredOptions = new Observable<City[]>();
34 this.cities = [];
35 this.countries = [];
36 this.companions = [];
37 this.categories = [];
38 }
39
40 ngOnInit() :void {
41 this.filteredOptions = this.myControl.valueChanges
42 .pipe(
43 startWith(''),
44 switchMap(val => {
45 return this.filter(val || '')
46 })
47 );
48
49 this.categoryService.getAllCategories().subscribe(
50 data => {
51 this.categories = data;
52 }
53 );
54
55 this.companionService.getAllCompanions().subscribe(
56 data => {
57 this.companions = data;
58 }
59 )
60 }
61
62 filter(val: string): Observable<City[]> {
63 // call the service which makes the http-request
64 return this.cityService.getAllCities()
65 .pipe(
66 map(response => response.filter(option => {
67 return option.name.toLowerCase().indexOf(val.toLowerCase()) === 0
68 }))
69 )
70
71 }
72
73 toggleSelection(chip: MatChip){
74 chip.toggleSelected();
75 }
76
77}
Note: See TracBrowser for help on using the repository browser.