[fa375fe] | 1 | import { Component, Injectable, OnInit } from '@angular/core';
|
---|
| 2 | import { FormControl } from '@angular/forms';
|
---|
| 3 | import {map, startWith, switchMap} from 'rxjs/operators';
|
---|
| 4 | import {forkJoin, Observable} from 'rxjs';
|
---|
| 5 | import { CityService } from '../_services/city.service';
|
---|
| 6 | import { City } from '../_models/city';
|
---|
| 7 | import { Country } from '../_models/country';
|
---|
| 8 | import { CountryService } from '../_services/country.service';
|
---|
| 9 | import { Companion } from '../_models/companion';
|
---|
| 10 | import { CompanionService } from '../_services/companion.service';
|
---|
| 11 | import { Category } from '../_models/category';
|
---|
| 12 | import { CategoryService } from '../_services/cateogry.service';
|
---|
| 13 | import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
|
---|
| 14 | import { MatChip } from '@angular/material/chips';
|
---|
[eed0bf8] | 15 | import { LocationService } from '../_services/location.service';
|
---|
| 16 | import { Region } from '../_models/region';
|
---|
| 17 | import { RegionService } from '../_services/region.service';
|
---|
[fa375fe] | 18 |
|
---|
| 19 | @Component({
|
---|
| 20 | selector: 'app-locations-form',
|
---|
| 21 | templateUrl: './locations-form.component.html',
|
---|
| 22 | styleUrls: ['./locations-form.component.css']
|
---|
| 23 | })
|
---|
| 24 | export 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;
|
---|
| 37 | companionId: number;
|
---|
| 38 | lengthOfStay: number;
|
---|
| 39 | cityOption: boolean = false;
|
---|
| 40 | regionOption: boolean = false;
|
---|
[fa375fe] | 41 |
|
---|
[eed0bf8] | 42 | constructor(private cityService : CityService, private regionService: RegionService,
|
---|
| 43 | private companionService : CompanionService, private categoryService : CategoryService,
|
---|
| 44 | private locationService: LocationService){
|
---|
[fa375fe] | 45 | this.filteredOptions = new Observable<City[]>();
|
---|
| 46 | this.cities = [];
|
---|
[eed0bf8] | 47 | this.regions = [];
|
---|
[fa375fe] | 48 | this.companions = [];
|
---|
| 49 | this.categories = [];
|
---|
[eed0bf8] | 50 | this.chipsSeletion = [];
|
---|
| 51 | this.locationId = 0;
|
---|
| 52 | this.companionId = 0;
|
---|
| 53 | this.lengthOfStay = 1;
|
---|
| 54 | this.categoryIds = '';
|
---|
| 55 | this.regionId = 0;
|
---|
[fa375fe] | 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 |
|
---|
[eed0bf8] | 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 |
|
---|
[fa375fe] | 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 |
|
---|
[eed0bf8] | 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);
|
---|
[fa375fe] | 116 | }
|
---|
| 117 |
|
---|
[eed0bf8] | 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 | }
|
---|
[fa375fe] | 136 | }
|
---|