1 | import { Component, OnInit } from '@angular/core';
|
---|
2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
---|
3 | import { ActivatedRoute, Router } from '@angular/router';
|
---|
4 | import { PlannerDto } from 'src/app/_models/dto/plannerDto';
|
---|
5 | import { PlannerLocationDto } from 'src/app/_models/dto/plannerLocationDto';
|
---|
6 | import { Location } from 'src/app/_models/location';
|
---|
7 | import { Planner } from 'src/app/_models/planner';
|
---|
8 | import { LocationService } from 'src/app/_services/location.service';
|
---|
9 | import { PlannerService } from 'src/app/_services/planner.service';
|
---|
10 |
|
---|
11 | @Component({
|
---|
12 | selector: 'app-edit-planner',
|
---|
13 | templateUrl: './edit-planner.component.html',
|
---|
14 | styleUrls: ['./edit-planner.component.css']
|
---|
15 | })
|
---|
16 | export class EditPlannerComponent implements OnInit {
|
---|
17 |
|
---|
18 | planner: Planner;
|
---|
19 | planners: Planner[];
|
---|
20 | form: FormGroup;
|
---|
21 | plannerDto: PlannerDto;
|
---|
22 | id: number;
|
---|
23 | locations: Location[];
|
---|
24 | plannerLocationDto: PlannerLocationDto;
|
---|
25 |
|
---|
26 | constructor(private router: Router, private route: ActivatedRoute, private fb: FormBuilder, private plannerService: PlannerService,
|
---|
27 | private locationService: LocationService) {
|
---|
28 | this.planner = new Planner();
|
---|
29 | this.planners = [];
|
---|
30 | this.form = fb.group({
|
---|
31 | title: fb.control('initial value', Validators.required)
|
---|
32 | });
|
---|
33 | this.plannerDto = new PlannerDto();
|
---|
34 | this.id = 1;
|
---|
35 | this.locations = [];
|
---|
36 | this.plannerLocationDto = new PlannerLocationDto();
|
---|
37 | }
|
---|
38 |
|
---|
39 | ngOnInit(): void {
|
---|
40 | this.id = this.route.snapshot.params['id'];
|
---|
41 |
|
---|
42 | this.form = this.fb.group({
|
---|
43 | name: [''],
|
---|
44 | description: [''],
|
---|
45 | locationList: []
|
---|
46 | });
|
---|
47 |
|
---|
48 | this.plannerService.getPlannerById(this.id)
|
---|
49 | .pipe()
|
---|
50 | .subscribe(x => this.form.patchValue(x));
|
---|
51 |
|
---|
52 | this.locationService.getLocationsForPlanner(this.id).subscribe(
|
---|
53 | data => {
|
---|
54 | this.locations = data;
|
---|
55 | }
|
---|
56 | );
|
---|
57 | }
|
---|
58 |
|
---|
59 | onSubmit() {
|
---|
60 | this.updatePlanner();
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | onClickAddLocation() {
|
---|
65 | this.router.navigate(['form']);
|
---|
66 | }
|
---|
67 |
|
---|
68 | updatePlanner() {
|
---|
69 | this.plannerService.updatePlanner(this.id, this.form.value)
|
---|
70 | .pipe()
|
---|
71 | .subscribe({
|
---|
72 | next: () => {
|
---|
73 | this.router.navigate(['planners']);
|
---|
74 | },
|
---|
75 | error: error => {
|
---|
76 | console.log("error");
|
---|
77 | }
|
---|
78 | });
|
---|
79 | }
|
---|
80 |
|
---|
81 | onClickBack() {
|
---|
82 | this.router.navigate(['planners']);
|
---|
83 | }
|
---|
84 |
|
---|
85 | onClickRemoveLocation(planner: Planner, location: Location) {
|
---|
86 | planner.id = this.id;
|
---|
87 |
|
---|
88 | this.plannerLocationDto.plannerId = planner.id;
|
---|
89 | this.plannerLocationDto.locationId = location.id;
|
---|
90 | console.log(this.plannerLocationDto.plannerId);
|
---|
91 | console.log(this.plannerLocationDto.locationId);
|
---|
92 | this.plannerService.deleteLocationFromPlanner(this.plannerLocationDto).subscribe(
|
---|
93 | data => {
|
---|
94 | console.log("deleted")
|
---|
95 | }
|
---|
96 | );
|
---|
97 | window.location.reload();
|
---|
98 | }
|
---|
99 | }
|
---|