Ignore:
Timestamp:
11/04/21 23:10:39 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
ceaed42
Parents:
6a80231
Message:

edit planner form with angular

Location:
trip-planner-front/src/app/planner
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trip-planner-front/src/app/planner/edit-planner/edit-planner.component.html

    r6a80231 r6c1585f  
    33<h3>Here you can edit your planner</h3>
    44
    5 <form>
     5<form [formGroup] = "form" (ngSubmit)="onSubmit()">
    66    <div class="form-gorup">
    77        <p>Planner name</p>
    88        <mat-form-field appearance="fill">
    9           <input matInput required   type="text" name="name"  placeholder="Planner name">
     9          <input matInput required type="text" formControlName="name"  placeholder="Planner name">
    1010        </mat-form-field>
    1111        <p>Planner description</p>
    1212        <mat-form-field appearance="fill">
    13           <textarea matInput required name="description"  type="text" placeholder="Planner description"></textarea>
     13          <textarea matInput required formControlName="description"  type="text" placeholder="Planner description"></textarea>
    1414        </mat-form-field>
    1515    </div>
     
    2525                        </thead>
    2626                        <tbody>
    27                         <tr>
    28                            
     27                        <tr>                           
    2928                            <td> <button mat-raised-button color="primary" (click)="onClickAddLocation()">
    3029                                 Add locations
     
    3837        </div>
    3938    </div>
    40     <button mat-raised-button color="primary" (click)="onClickSavePlanner()">
     39    <button mat-raised-button color="primary" >
    4140        <mat-icon>edit</mat-icon> Save
    4241      </button>
  • trip-planner-front/src/app/planner/edit-planner/edit-planner.component.ts

    r6a80231 r6c1585f  
    11import { Component, OnInit } from '@angular/core';
    2 import { Router } from '@angular/router';
     2import { FormBuilder, FormGroup, Validators } from '@angular/forms';
     3import { ActivatedRoute, Router } from '@angular/router';
     4import { PlannerDto } from 'src/app/_models/dto/plannerDto';
    35import { Planner } from 'src/app/_models/planner';
     6import { PlannerService } from 'src/app/_services/planner.service';
    47
    58@Component({
     
    1114
    1215  planner: Planner;
     16  planners: Planner[];
     17  form: FormGroup;
     18  plannerDto: PlannerDto;
     19  id: number;
    1320
    1421
    15   constructor(private router: Router) {
     22  constructor(private router: Router, private route: ActivatedRoute ,private fb: FormBuilder, private plannerService: PlannerService) {
    1623    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;
    1730  }
    1831
    1932  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));
    2044  }
    2145
    2246
    23   onClickSavePlanner(){
    24       this.router.navigate(['planners']);
     47  onSubmit(){
     48    this.updatePlanner();
     49     
    2550  }
    2651
    27   onClickAddLocation(){
     52  onClickAddLocation()
     53  {
    2854    this.router.navigate(['form']);
    2955  }
     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        });
    3068}
     69 
     70}
  • trip-planner-front/src/app/planner/planner.component.html

    r6a80231 r6c1585f  
    1111        </mat-card-content>
    1212        <mat-card-actions class="mt-auto">
    13           <button mat-raised-button color="primary" (click)="onClickEditPlanner(planner.id)">
     13          <button mat-raised-button color="primary" (click)="onClickEditPlannerGet(planner.id)">
    1414            <mat-icon>edit</mat-icon> Edit
    15           </button>
    16          
     15          </button>       
    1716          <button mat-raised-button color="accent">
    1817            <mat-icon>remove_red_eye</mat-icon>Details
  • trip-planner-front/src/app/planner/planner.component.ts

    r6a80231 r6c1585f  
    55import { CreateInitialPlannerComponent } from '../create-initial-planner/create-initial-planner.component';
    66import { Router } from '@angular/router';
     7import { PlannerDto } from '../_models/dto/plannerDto';
     8import { FormBuilder, FormGroup, Validators } from '@angular/forms';
    79
    810
     
    1517
    1618  planners: Planner[];
    17   plannerId: number;
     19  plannerDto: PlannerDto;
     20  editForm: FormGroup;
    1821
    19   constructor(private plannerService: PlannerService, public dialog: MatDialog, private router: Router) {
     22
     23
     24  constructor(private plannerService: PlannerService, public dialog: MatDialog, private router: Router,
     25    private fb : FormBuilder) {
    2026    this.planners = [];
    21     this.plannerId = 1;
     27    this.plannerDto = new PlannerDto();
     28    this.editForm = fb.group({
     29      title: fb.control('initial value', Validators.required)
     30  });
    2231  };
    2332 
     
    3746  }
    3847 
    39   onClickEditPlanner(id: number){
     48  onClickEditPlannerGet(id: number){
    4049    console.log(id);
    41    
    42          
    43           this.router.navigate(['edit/planner/', this.plannerId])
     50        this.plannerService.getPlannerById(id).subscribe(
     51            data => {
     52             
     53              this.router.navigate(['edit/planner/', id])
     54            }
     55     );
    4456   
    4557  }
     58
     59 
    4660}
Note: See TracChangeset for help on using the changeset viewer.