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

Last change on this file was 571e0df, checked in by Ema <ema_spirova@…>, 2 years ago

final presentation

  • Property mode set to 100644
File size: 4.8 KB
Line 
1import { Component, OnInit } from '@angular/core';
2import { FormBuilder, FormControl } from '@angular/forms';
3import { map, startWith, switchMap } from 'rxjs/operators';
4import { Observable } from 'rxjs';
5import { CityService } from '../_services/city.service';
6import { City } from '../_models/city';
7import { Companion } from '../_models/companion';
8import { CompanionService } from '../_services/companion.service';
9import { Category } from '../_models/category';
10import { CategoryService } from '../_services/cateogry.service';
11import { MatChip } from '@angular/material/chips';
12import { LocationService } from '../_services/location.service';
13import { Region } from '../_models/region';
14import { RegionService } from '../_services/region.service';
15import { ActivatedRoute, Router } from '@angular/router';
16
17
18
19@Component({
20 selector: 'app-locations-form',
21 templateUrl: './locations-form.component.html',
22 styleUrls: ['./locations-form.component.css']
23})
24export class LocationsFormComponent implements OnInit {
25
26 myControl = new FormControl();
27 cities: City[];
28 regions: Region[];
29 companions: Companion[];
30 categories: Category[];
31 filteredOptions: Observable<City[]>;
32 disableSelect = new FormControl(false);
33 chipsSeletion: number[];
34 categoryIds: string;
35 locationId: number;
36 regionId: number;
37 cityId: number;
38 companionId: number;
39 lengthOfStay: number;
40 cityOption: boolean = false;
41 regionOption: boolean = false;
42 value: number;
43 max: number;
44 toggle = true;
45 status = 'Enable';
46 proba: any[];
47
48
49 constructor(private cityService: CityService, private regionService: RegionService,
50 private companionService: CompanionService, private categoryService: CategoryService,
51 private locationService: LocationService, private router: Router, private fb: FormBuilder, private route: ActivatedRoute) {
52 this.filteredOptions = new Observable<City[]>();
53 this.cities = [];
54 this.regions = [];
55 this.companions = [];
56 this.categories = [];
57 this.chipsSeletion = [];
58 this.locationId = 0;
59 this.companionId = 0;
60 this.lengthOfStay = 1;
61 this.categoryIds = '';
62 this.regionId = 0;
63 this.cityId = 0;
64 this.value = 0;
65 this.max = 30;
66 this.proba = [];
67 }
68
69 ngOnInit(): void {
70
71 this.filteredOptions = this.myControl.valueChanges
72 .pipe(
73 startWith(''),
74 switchMap(val => {
75 return this.filter(val || '')
76 })
77 );
78
79 this.cityService.getAllCities().subscribe(
80 data => {
81 this.cities = data;
82 }
83 );
84
85 this.regionService.getAllRegions().subscribe(
86 data => {
87 this.regions = data;
88 }
89 );
90
91 this.categoryService.getAllCategories().subscribe(
92 data => {
93 this.categories = data;
94 }
95 );
96
97 this.companionService.getAllCompanions().subscribe(
98 data => {
99 this.companions = data;
100 }
101 );
102
103 }
104
105 filter(val: string): Observable<City[]> {
106 // call the service which makes the http-request
107 return this.cityService.getAllCities()
108 .pipe(
109 map(response => response.filter(option => {
110 return option.name.toLowerCase().indexOf(val.toLowerCase()) === 0
111 }))
112 )
113 }
114
115 toggleSelection(chip: MatChip, category: Category) {
116 chip.toggleSelected();
117
118 if (this.chipsSeletion.length > 0) {
119 if (this.chipsSeletion.indexOf(category.id) <= -1) {
120 this.chipsSeletion.push(category.id);
121 } else {
122 const index = this.chipsSeletion.indexOf(category.id);
123 this.chipsSeletion.splice(index, 1);
124 }
125 } else {
126 this.chipsSeletion.push(category.id);
127 }
128 console.log(this.chipsSeletion);
129 }
130
131
132 createMyPlanner() {
133 this.categoryIds = this.chipsSeletion.join(',');
134 console.log(this.categoryIds);
135
136 if (this.cityOption) {
137 this.locationService.getLocationsFromCity(this.cityId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
138 result => {
139 console.log(result);
140 this.proba = result;
141 this.router.navigate(['locations'], { queryParams: { cityId: this.cityId, companionId: this.companionId, lengthOfStay: this.lengthOfStay, categoryIds: this.categoryIds } });
142 }
143 );
144 } else
145 if (this.regionOption) {
146 this.locationService.getLocationsFromRegion(this.regionId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
147 result => {
148 console.log(result);
149 this.router.navigate(['locations'], { queryParams: { regionId: this.regionId, companionId: this.companionId, lengthOfStay: this.lengthOfStay, categoryIds: this.categoryIds } });
150 }
151 );
152 }
153 }
154
155 chooseCityOption() {
156 this.cityOption = true;
157 this.regionOption = false;
158 }
159 chooseRegionOption() {
160 this.regionOption = true;
161 this.cityOption = false;
162 }
163
164 constraintMaxNumberDays() {
165 if (this.value > this.max) {
166 this.value = this.max;
167 }
168 }
169
170
171}
Note: See TracBrowser for help on using the repository browser.