1 | import { Component, OnInit } from '@angular/core';
|
---|
2 | import { ActivatedRoute, Router } from '@angular/router';
|
---|
3 | import { MessageService } from 'primeng/api';
|
---|
4 | import { DialogService, DynamicDialogRef } from 'primeng/dynamicdialog';
|
---|
5 | import { Planner } from '../_models/planner';
|
---|
6 | import { LocationService } from '../_services/location.service';
|
---|
7 | import { AddLocationToPlannerPanelComponent } from './add-location-to-planner-panel/add-location-to-planner-panel.component';
|
---|
8 | import { Location } from '../_models/location';
|
---|
9 | import { PlannerLocationDto } from '../_models/dto/plannerLocationDto';
|
---|
10 |
|
---|
11 |
|
---|
12 | @Component({
|
---|
13 | selector: 'app-location',
|
---|
14 | templateUrl: './location.component.html',
|
---|
15 | styleUrls: ['./location.component.css'],
|
---|
16 | providers: [DialogService, MessageService]
|
---|
17 | })
|
---|
18 | export class LocationComponent implements OnInit {
|
---|
19 |
|
---|
20 | categoryIds: string;
|
---|
21 | cityId: number;
|
---|
22 | companionId: number;
|
---|
23 | lengthOfStay: number;
|
---|
24 | listLocations: any[];
|
---|
25 | cityOption: boolean = false;
|
---|
26 | regionOption: boolean = false;
|
---|
27 | regionId: number;
|
---|
28 | locationId: number;
|
---|
29 | plannerLocationDto: PlannerLocationDto;
|
---|
30 | ref: DynamicDialogRef;
|
---|
31 | locationIdsPlanner: number[];
|
---|
32 |
|
---|
33 |
|
---|
34 | constructor(private route: ActivatedRoute, private locationService: LocationService,
|
---|
35 | private router: Router, private dialogService: DialogService, private messageService: MessageService) {
|
---|
36 | this.cityId = 1;
|
---|
37 | this.companionId = 1;
|
---|
38 | this.lengthOfStay = 1;
|
---|
39 | this.categoryIds = '';
|
---|
40 | this.listLocations = [];
|
---|
41 | this.regionId = 1;
|
---|
42 | this.locationId = 1;
|
---|
43 | this.ref = new DynamicDialogRef();
|
---|
44 | this.plannerLocationDto = new PlannerLocationDto();
|
---|
45 | this.locationIdsPlanner = [];
|
---|
46 | }
|
---|
47 |
|
---|
48 | ngOnInit(): void {
|
---|
49 |
|
---|
50 | this.route.queryParams
|
---|
51 | .subscribe(params => {
|
---|
52 | console.log(params);
|
---|
53 | this.cityId = params.cityId;
|
---|
54 | this.regionId = params.regionId;
|
---|
55 | this.companionId = params.companionId;
|
---|
56 | this.lengthOfStay = params.lengthOfStay;
|
---|
57 | this.categoryIds = params.categoryIds;
|
---|
58 | }
|
---|
59 | );
|
---|
60 |
|
---|
61 | if (this.route.snapshot.queryParams['cityId']) {
|
---|
62 | this.locationService.getLocationsFromCity(this.cityId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
|
---|
63 | result => {
|
---|
64 | console.log(result);
|
---|
65 | this.listLocations = result;
|
---|
66 | }
|
---|
67 | );
|
---|
68 | } else
|
---|
69 | if (this.route.snapshot.queryParams['regionId']) {
|
---|
70 | console.log("I am in region console");
|
---|
71 | this.locationService.getLocationsFromRegion(this.regionId, this.companionId, this.lengthOfStay, this.categoryIds).subscribe(
|
---|
72 | result => {
|
---|
73 | console.log(result);
|
---|
74 | this.listLocations = result;
|
---|
75 | }
|
---|
76 | );
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | onClickSeeDetails(id: number) {
|
---|
82 | this.router.navigate(['location'], { queryParams: { id: id } });
|
---|
83 | }
|
---|
84 |
|
---|
85 | show(location: Location) {
|
---|
86 | console.log(location.id);
|
---|
87 | this.ref = this.dialogService.open(AddLocationToPlannerPanelComponent, {
|
---|
88 | header: 'Choose a Planner',
|
---|
89 | width: '70%',
|
---|
90 | contentStyle: { "max-height": "500px", "overflow": "auto" },
|
---|
91 | baseZIndex: 10000
|
---|
92 | });
|
---|
93 |
|
---|
94 | this.ref.onClose.subscribe((planner: Planner) => {
|
---|
95 | if (planner) {
|
---|
96 | this.plannerLocationDto.locationId = location.id;
|
---|
97 | this.plannerLocationDto.plannerId = planner.id;
|
---|
98 | console.log("LOC ID: " + this.plannerLocationDto.locationId);
|
---|
99 | console.log("PLANNER ID: " + this.plannerLocationDto.plannerId);
|
---|
100 |
|
---|
101 | this.locationService.getAllLocationIdsForPlanner(planner.id).subscribe(
|
---|
102 | lid => {
|
---|
103 | if (lid.length == 0) {
|
---|
104 | this.locationService.postLocationToPlanner(this.plannerLocationDto).subscribe(
|
---|
105 | data => {
|
---|
106 | console.log(data);
|
---|
107 | }
|
---|
108 | );
|
---|
109 | this.messageService.add({ severity: 'success', summary: 'Location ' + location.name + ' has been added to planner: ', detail: planner.name });
|
---|
110 |
|
---|
111 | } else if (lid.length > 0) {
|
---|
112 | if (lid.indexOf(this.plannerLocationDto.locationId) !== -1) {
|
---|
113 | console.log("LOKACIJATA VEKE JA IMA VO PLANEROT");
|
---|
114 | this.messageService.add({ severity: 'error', summary: 'Location ' + location.name + ' already exists in the planner.' });
|
---|
115 | } else {
|
---|
116 | this.locationService.postLocationToPlanner(this.plannerLocationDto).subscribe(
|
---|
117 | data => {
|
---|
118 | console.log(data);
|
---|
119 | }
|
---|
120 | );
|
---|
121 | this.messageService.add({ severity: 'success', summary: 'Location ' + location.name + ' has been added to planner: ', detail: planner.name });
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|
125 | }
|
---|
126 | );
|
---|
127 | }
|
---|
128 | });
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | ngOnDestroy() {
|
---|
133 | if (this.ref) {
|
---|
134 | this.ref.close();
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | onClickBackToMyPlanners() {
|
---|
139 | this.router.navigate(['planners']);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | }
|
---|