1 | import { Component, OnInit } from '@angular/core';
|
---|
2 | import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
|
---|
3 | import { MatChip } from '@angular/material/chips';
|
---|
4 | import { Category } from 'src/app/_models/category';
|
---|
5 | import { City } from 'src/app/_models/city';
|
---|
6 | import { Companion } from 'src/app/_models/companion';
|
---|
7 | import { LocationDto } from 'src/app/_models/dto/locationDto';
|
---|
8 | import { Region } from 'src/app/_models/region';
|
---|
9 | import { CategoryService } from 'src/app/_services/cateogry.service';
|
---|
10 | import { CityService } from 'src/app/_services/city.service';
|
---|
11 | import { CompanionService } from 'src/app/_services/companion.service';
|
---|
12 | import { LocationService } from 'src/app/_services/location.service';
|
---|
13 | import { 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 | })
|
---|
20 | export 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 |
|
---|
118 | this.locationService.save(this.locationDto).subscribe(
|
---|
119 | data => {
|
---|
120 | console.log(data);
|
---|
121 | }
|
---|
122 | );
|
---|
123 |
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | toggleSelection(chip: MatChip, category: Category) {
|
---|
128 | chip.toggleSelected();
|
---|
129 |
|
---|
130 | if (this.chipsSeletion.length > 0) {
|
---|
131 | if (this.chipsSeletion.indexOf(category.id) <= -1) {
|
---|
132 | this.chipsSeletion.push(category.id);
|
---|
133 | } else {
|
---|
134 | const index = this.chipsSeletion.indexOf(category.id);
|
---|
135 | this.chipsSeletion.splice(index, 1);
|
---|
136 | }
|
---|
137 | } else {
|
---|
138 | this.chipsSeletion.push(category.id);
|
---|
139 | }
|
---|
140 | console.log(this.chipsSeletion);
|
---|
141 | }
|
---|
142 | onClickSecondForm(){
|
---|
143 | if(this.companionForm.value.length >= 1){
|
---|
144 | this.isCompletedSecond = true;
|
---|
145 | }
|
---|
146 | console.log(this.companionForm.value.length)
|
---|
147 | }
|
---|
148 | }
|
---|