source: trip-planner-front/src/app/planner/edit-planner/edit-planner.component.ts@ 6c1585f

Last change on this file since 6c1585f was 6c1585f, checked in by Ema <ema_spirova@…>, 3 years ago

edit planner form with angular

  • Property mode set to 100644
File size: 1.7 KB
Line 
1import { Component, OnInit } from '@angular/core';
2import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3import { ActivatedRoute, Router } from '@angular/router';
4import { PlannerDto } from 'src/app/_models/dto/plannerDto';
5import { Planner } from 'src/app/_models/planner';
6import { PlannerService } from 'src/app/_services/planner.service';
7
8@Component({
9 selector: 'app-edit-planner',
10 templateUrl: './edit-planner.component.html',
11 styleUrls: ['./edit-planner.component.css']
12})
13export class EditPlannerComponent implements OnInit {
14
15 planner: Planner;
16 planners: Planner[];
17 form: FormGroup;
18 plannerDto: PlannerDto;
19 id: number;
20
21
22 constructor(private router: Router, private route: ActivatedRoute ,private fb: FormBuilder, private plannerService: PlannerService) {
23 this.planner = new Planner();
24 this.planners = [];
25 this.form = fb.group({
26 title: fb.control('initial value', Validators.required)
27 });
28 this.plannerDto = new PlannerDto();
29 this.id = 1;
30 }
31
32 ngOnInit(): void {
33 this.id = this.route.snapshot.params['id'];
34
35 this.form = this.fb.group({
36 name: [''],
37 description: [''],
38 locationList: []
39 });
40
41 this.plannerService.getPlannerById(this.id)
42 .pipe()
43 .subscribe(x => this.form.patchValue(x));
44 }
45
46
47 onSubmit(){
48 this.updatePlanner();
49
50 }
51
52 onClickAddLocation()
53 {
54 this.router.navigate(['form']);
55 }
56
57 private updatePlanner() {
58 this.plannerService.updatePlanner(this.id, this.form.value)
59 .pipe()
60 .subscribe({
61 next: () => {
62 this.router.navigate(['planners']);
63 },
64 error: error => {
65 console.log("error");
66 }
67 });
68}
69
70}
Note: See TracBrowser for help on using the repository browser.