import {Component, OnInit} from '@angular/core'; import {FormControl, FormGroup, Validators} from "@angular/forms"; import {ActivatedRoute, Router} from "@angular/router"; import {ApiService} from "../../Services/api.service"; @Component({ selector: 'app-edit', templateUrl: './edit.component.html', styleUrls: ['./edit.component.scss'] }) export class EditComponent implements OnInit{ errorWarning: string = ""; editFormGroup!: FormGroup; index: string; loading: boolean = true; constructor(private activeRoute: ActivatedRoute, private apiService: ApiService, private router: Router) { this.index = this.activeRoute.snapshot.paramMap.get('id') ?? ''; } ngOnInit(): void { this.editFormGroup = new FormGroup({ title: new FormControl('', Validators.required), type: new FormControl('', Validators.required), description: new FormControl('', Validators.required), date: new FormControl('', Validators.required) }) this.apiService.getOneCard(+this.index).subscribe({ next: (data: any): void => { data = data.data.attributes this.editFormGroup.setValue({title: data.title, type: data.type, description: data.description, date: data.date}) this.loading = false; }, error: (): void => { this.router.navigate(["admin"]) } }) } onSubmit(): void{ if(this.editFormGroup.status === "VALID"){ this.loading = true; this.apiService.editCard(+this.index, this.editFormGroup.value).subscribe({ next: (): void=>{ this.router.navigate(["admin"]) } }) }else{ this.errorWarning = "Nav korekti aizpildīti ievadlauki!" } } protected readonly localStorage = localStorage; }