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