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

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

create initial planner and routing with angular

  • Property mode set to 100644
File size: 4.4 KB
Line 
1import { Component, Injectable, OnInit } from '@angular/core';
2import { FormControl, NgForm } 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';
15import { LocationService } from '../_services/location.service';
16import { Region } from '../_models/region';
17import { RegionService } from '../_services/region.service';
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
47 constructor(private cityService : CityService, private regionService: RegionService,
48 private companionService : CompanionService, private categoryService : CategoryService,
49 private locationService: LocationService){
50 this.filteredOptions = new Observable<City[]>();
51 this.cities = [];
52 this.regions = [];
53 this.companions = [];
54 this.categories = [];
55 this.chipsSeletion = [];
56 this.locationId = 0;
57 this.companionId = 0;
58 this.lengthOfStay = 1;
59 this.categoryIds = '';
60 this.regionId = 0;
61 this.cityId = 0;
62 this.value = 0;
63 this.max = 30;
64 }
65
66 ngOnInit() :void {
67 this.filteredOptions = this.myControl.valueChanges
68 .pipe(
69 startWith(''),
70 switchMap(val => {
71 return this.filter(val || '')
72 })
73 );
74
75 this.cityService.getAllCities().subscribe(
76 data => {
77 this.cities = data;
78 }
79 );
80
81 this.regionService.getAllRegions().subscribe(
82 data => {
83 this.regions = data;
84 }
85 );
86
87 this.categoryService.getAllCategories().subscribe(
88 data => {
89 this.categories = data;
90 }
91 );
92
93 this.companionService.getAllCompanions().subscribe(
94 data => {
95 this.companions = data;
96 }
97 )
98 }
99
100 filter(val: string): Observable<City[]> {
101 // call the service which makes the http-request
102 return this.cityService.getAllCities()
103 .pipe(
104 map(response => response.filter(option => {
105 return option.name.toLowerCase().indexOf(val.toLowerCase()) === 0
106 }))
107 )
108
109 }
110
111 toggleSelection(chip: MatChip, category: Category){
112 chip.toggleSelected();
113
114 if(this.chipsSeletion.length > 0){
115 if(this.chipsSeletion.indexOf(category.id) <= -1){
116 this.chipsSeletion.push(category.id);
117 }else{
118 const index = this.chipsSeletion.indexOf(category.id);
119 this.chipsSeletion.splice(index, 1);
120 }
121 }else{
122 this.chipsSeletion.push(category.id);
123 }
124 console.log(this.chipsSeletion);
125 }
126
127
128 createMyPlanner(){
129 this.categoryIds = this.chipsSeletion.join(',');
130 console.log(this.categoryIds);
131
132 if(this.cityOption){
133 this.locationService.getLocationsFromCity(this.cityId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
134 result => {
135 console.log(result);
136 }
137 );
138 }else if(this.regionOption){
139
140 this.locationService.getLocationsFromRegion(this.regionId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
141 result => {
142 console.log(result);
143 }
144 );
145 }
146 }
147
148 chooseCityOption(){
149 this.cityOption = true;
150 this.regionOption = false;
151 }
152 chooseRegionOption() {
153 this.regionOption = true;
154 this.cityOption = false;
155 }
156
157 constraintMaxNumberDays(){
158 if(this.value > this.max){
159 this.value = this.max;
160 }
161 }
162
163}
Note: See TracBrowser for help on using the repository browser.