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

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

editing location-form on frontend and backend

  • Property mode set to 100644
File size: 3.9 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';
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 companionId: number;
38 lengthOfStay: number;
39 cityOption: boolean = false;
40 regionOption: boolean = false;
41
42 constructor(private cityService : CityService, private regionService: RegionService,
43 private companionService : CompanionService, private categoryService : CategoryService,
44 private locationService: LocationService){
45 this.filteredOptions = new Observable<City[]>();
46 this.cities = [];
47 this.regions = [];
48 this.companions = [];
49 this.categories = [];
50 this.chipsSeletion = [];
51 this.locationId = 0;
52 this.companionId = 0;
53 this.lengthOfStay = 1;
54 this.categoryIds = '';
55 this.regionId = 0;
56 }
57
58 ngOnInit() :void {
59 this.filteredOptions = this.myControl.valueChanges
60 .pipe(
61 startWith(''),
62 switchMap(val => {
63 return this.filter(val || '')
64 })
65 );
66
67 this.cityService.getAllCities().subscribe(
68 data => {
69 this.cities = data;
70 }
71 );
72
73 this.regionService.getAllRegions().subscribe(
74 data => {
75 this.regions = data;
76 }
77 );
78
79 this.categoryService.getAllCategories().subscribe(
80 data => {
81 this.categories = data;
82 }
83 );
84
85 this.companionService.getAllCompanions().subscribe(
86 data => {
87 this.companions = data;
88 }
89 )
90 }
91
92 filter(val: string): Observable<City[]> {
93 // call the service which makes the http-request
94 return this.cityService.getAllCities()
95 .pipe(
96 map(response => response.filter(option => {
97 return option.name.toLowerCase().indexOf(val.toLowerCase()) === 0
98 }))
99 )
100
101 }
102
103 toggleSelection(chip: MatChip, category: Category){
104 chip.toggleSelected();
105 if(this.chipsSeletion.length > 0){
106 if(this.chipsSeletion.indexOf(category.id) <= -1){
107 this.chipsSeletion.push(category.id);
108 }else{
109 const index = this.chipsSeletion.indexOf(category.id);
110 this.chipsSeletion.splice(index, 1);
111 }
112 }else{
113 this.chipsSeletion.push(category.id);
114 }
115 console.log(this.chipsSeletion);
116 }
117
118
119 createMyPlanner(){
120 this.categoryIds = this.chipsSeletion.join(',');
121 console.log(this.companionId);
122 this.locationService.getAllPlaces(this.locationId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
123 result => {
124 console.log(result);
125 }
126 );
127 }
128 chooseCityOption(){
129 this.cityOption = true;
130 this.regionOption = false;
131 }
132 chooseRegionOption() {
133 this.regionOption = true;
134 this.cityOption = false;
135 }
136}
Note: See TracBrowser for help on using the repository browser.