source: trip-planner-front/src/app/location/add-location/add-location.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.7 KB
Line 
1import { Component, OnInit } from '@angular/core';
2import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
3import { MatChip } from '@angular/material/chips';
4import { Category } from 'src/app/_models/category';
5import { City } from 'src/app/_models/city';
6import { Companion } from 'src/app/_models/companion';
7import { LocationDto } from 'src/app/_models/dto/locationDto';
8import { Region } from 'src/app/_models/region';
9import { CategoryService } from 'src/app/_services/cateogry.service';
10import { CityService } from 'src/app/_services/city.service';
11import { CompanionService } from 'src/app/_services/companion.service';
12import { LocationService } from 'src/app/_services/location.service';
13import { RegionService } from 'src/app/_services/region.service';
14
15@Component({
16 selector: 'app-add-location',
17 templateUrl: './add-location.component.html',
18 styleUrls: ['./add-location.component.css']
19})
20export class AddLocationComponent implements OnInit {
21 firstFormGroup: FormGroup;
22 secondFormGroup: FormGroup;
23 thirdFormGroup : FormGroup;
24 isEditable = false;
25 categories : Category[] = [];
26 companions : Companion[] = [];
27 cities : City[] = [];
28 cityId : number = 0;
29 regionId : number = 0;
30 regions : Region[] = [];
31 priority: string = '';
32 name: string = '';
33 desc: string = '';
34 address: string = '';
35 duration: number = 0;
36 trivia: string = '';
37 isCompleted = false;
38 locationDto : LocationDto = new LocationDto();
39 companionForm = new FormControl();
40 chipsSeletion: number[] = [];
41 toggle = true;
42 isCompletedSecond = false;
43
44 priorities: any[] = [
45 {value: 'high', viewValue: 'High'},
46 {value: 'medium', viewValue: 'Medium'},
47 {value: 'low', viewValue: 'Low'},
48 ];
49
50 constructor(private fb: FormBuilder, private categoryService : CategoryService,
51 private companionService : CompanionService, private cityService : CityService, private regionService : RegionService,
52 private locationService : LocationService) {
53 this.firstFormGroup = fb.group({
54 title: fb.control('initial value', Validators.required)
55 });
56 this.secondFormGroup = fb.group({
57 title: fb.control('initial value', Validators.required)
58 });
59 this.thirdFormGroup = fb.group({
60 title: fb.control('initial value', Validators.required)
61
62 })
63 }
64
65 ngOnInit() {
66 this.firstFormGroup = this.fb.group({
67 name: ['', Validators.required],
68 desc: ['', Validators.required],
69 address: ['', Validators.required],
70 priority: ['', Validators.required],
71 trivia: ['', Validators.required],
72 duration: ['', Validators.required],
73 });
74
75 this.secondFormGroup = this.fb.group({
76 secondCtrl: ['', Validators.required],
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 this.cityService.getAllCities().subscribe(
93 data => {
94 this.cities = data;
95 }
96 );
97
98 this.regionService.getAllRegions().subscribe(
99 data => {
100 this.regions = data;
101 }
102 );
103 }
104
105 submitLocation(){
106 this.locationDto.name = this.name;
107 this.locationDto.description = this.desc;
108 this.locationDto.address = this.address;
109 this.locationDto.duration = this.duration;
110 this.locationDto.trivia = this.trivia;
111 this.locationDto.priority = this.priority;
112 this.locationDto.city = this.cityId;
113 this.locationDto.region = this.regionId;
114 if(this.name !== null && this.desc !== null && this.address !== null
115 && this.trivia != null && this.priority !== null && this.cityId !== 0 && this.regionId !== 0){
116 this.isCompleted = true;
117 this.locationService.save(this.locationDto).subscribe(
118 data => {
119 console.log(data);
120 }
121 );
122
123 }
124 }
125
126 toggleSelection(chip: MatChip, category: Category) {
127 chip.toggleSelected();
128
129 if (this.chipsSeletion.length > 0) {
130 if (this.chipsSeletion.indexOf(category.id) <= -1) {
131 this.chipsSeletion.push(category.id);
132 } else {
133 const index = this.chipsSeletion.indexOf(category.id);
134 this.chipsSeletion.splice(index, 1);
135 }
136 } else {
137 this.chipsSeletion.push(category.id);
138 }
139 console.log(this.chipsSeletion);
140 }
141 onClickSecondForm(){
142 if(this.companionForm.value.length >= 1){
143 this.isCompletedSecond = true;
144 }
145 console.log(this.companionForm.value.length)
146 }
147}
Note: See TracBrowser for help on using the repository browser.