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

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

disabling to add location if it already exists in the planner

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[e29cc2e]1import { Component, OnInit } from '@angular/core';
[ceaed42]2import { FormBuilder, FormControl } from '@angular/forms';
3import { map, startWith, switchMap } from 'rxjs/operators';
4import { Observable } from 'rxjs';
[fa375fe]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';
[eed0bf8]12import { LocationService } from '../_services/location.service';
13import { Region } from '../_models/region';
14import { RegionService } from '../_services/region.service';
[e29cc2e]15import { ActivatedRoute, Router } from '@angular/router';
[ceaed42]16
17
[fa375fe]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[];
[eed0bf8]28 regions: Region[];
[fa375fe]29 companions: Companion[];
30 categories: Category[];
31 filteredOptions: Observable<City[]>;
32 disableSelect = new FormControl(false);
[eed0bf8]33 chipsSeletion: number[];
34 categoryIds: string;
35 locationId: number;
36 regionId: number;
[188ee53]37 cityId: number;
[eed0bf8]38 companionId: number;
39 lengthOfStay: number;
40 cityOption: boolean = false;
41 regionOption: boolean = false;
[ceaed42]42 value: number;
[188ee53]43 max: number;
[6a80231]44 toggle = true;
45 status = 'Enable';
[ceaed42]46 proba: any[];
[fa375fe]47
[ceaed42]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) {
[fa375fe]52 this.filteredOptions = new Observable<City[]>();
53 this.cities = [];
[eed0bf8]54 this.regions = [];
[fa375fe]55 this.companions = [];
56 this.categories = [];
[eed0bf8]57 this.chipsSeletion = [];
58 this.locationId = 0;
59 this.companionId = 0;
60 this.lengthOfStay = 1;
61 this.categoryIds = '';
62 this.regionId = 0;
[188ee53]63 this.cityId = 0;
64 this.value = 0;
65 this.max = 30;
[ceaed42]66 this.proba = [];
[fa375fe]67 }
[ceaed42]68
69 ngOnInit(): void {
[8d391a1]70
[fa375fe]71 this.filteredOptions = this.myControl.valueChanges
[ceaed42]72 .pipe(
73 startWith(''),
74 switchMap(val => {
75 return this.filter(val || '')
76 })
77 );
[fa375fe]78
[eed0bf8]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
[fa375fe]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 }
[ceaed42]101 );
102
[fa375fe]103 }
[ceaed42]104
[fa375fe]105 filter(val: string): Observable<City[]> {
[6c1585f]106 // call the service which makes the http-request
[fa375fe]107 return this.cityService.getAllCities()
[6c1585f]108 .pipe(
109 map(response => response.filter(option => {
110 return option.name.toLowerCase().indexOf(val.toLowerCase()) === 0
111 }))
112 )
[ceaed42]113 }
114
115 toggleSelection(chip: MatChip, category: Category) {
116 chip.toggleSelected();
[6c1585f]117
[ceaed42]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);
[6c1585f]129 }
[fa375fe]130
131
[eed0bf8]132
[6c1585f]133 createMyPlanner() {
134 this.categoryIds = this.chipsSeletion.join(',');
135 console.log(this.categoryIds);
136
137 if (this.cityOption) {
138 this.locationService.getLocationsFromCity(this.cityId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
139 result => {
140 console.log(result);
[ceaed42]141 this.proba = result;
[8d391a1]142 this.router.navigate(['locations'], { queryParams: { cityId: this.cityId, companionId: this.companionId, lengthOfStay: this.lengthOfStay, categoryIds: this.categoryIds } });
[6c1585f]143 }
144 );
[ceaed42]145 } else
146 if (this.regionOption) {
147 this.locationService.getLocationsFromRegion(this.regionId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
148 result => {
149 console.log(result);
[8d391a1]150 this.router.navigate(['locations'], { queryParams: { regionId: this.regionId, companionId: this.companionId, lengthOfStay: this.lengthOfStay, categoryIds: this.categoryIds } });
[ceaed42]151 }
152 );
153 }
[6c1585f]154 }
155
156 chooseCityOption() {
157 this.cityOption = true;
158 this.regionOption = false;
159 }
[eed0bf8]160 chooseRegionOption() {
161 this.regionOption = true;
162 this.cityOption = false;
163 }
[188ee53]164
[6c1585f]165 constraintMaxNumberDays() {
166 if (this.value > this.max) {
167 this.value = this.max;
168 }
[188ee53]169 }
[6a80231]170
[ceaed42]171
[fa375fe]172}
Note: See TracBrowser for help on using the repository browser.