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