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