import { Component, OnInit, ViewChild } from '@angular/core'; import { MatButton } from '@angular/material/button'; import { ActivatedRoute, Router } from '@angular/router'; import { CommercialPremiseService, PremiseService } from '../data-object-service'; import { NULL_CommercialPremise, NULL_Premise, CommercialPremise, Premise } from '../data-objects'; import { MessageService } from '../message.service'; @Component({ selector: 'app-commercial-unit-details', templateUrl: './commercial-unit-details.component.html', styleUrls: ['./commercial-unit-details.component.css'] }) export class CommercialUnitDetailsComponent implements OnInit { @ViewChild('submitButton') submitButton: MatButton commercialPremise: CommercialPremise = NULL_CommercialPremise premise: Premise = NULL_Premise premises: Premise[] constructor( private commercialPremiseService: CommercialPremiseService, private premiseService: PremiseService, private messageService: MessageService, private route: ActivatedRoute, private router: Router ) { } async getCommercialPremise(): Promise { try { const id = +this.route.snapshot.paramMap.get('id') if (id != 0) { this.commercialPremise = await this.commercialPremiseService.getCommercialPremise(id) this.premise = await this.premiseService.getPremise(this.commercialPremise.premise) } } catch (err) { this.messageService.add(JSON.stringify(err, undefined, 4)) } } async getPremises(): Promise { try { this.messageService.add("Trying to load premises") this.premises = await this.premiseService.getPremises() this.messageService.add("Premises loaded") } catch (err) { this.messageService.add(JSON.stringify(err, undefined, 4)) } } async saveCommercialPremise() { try { this.submitButton.disabled = true this.messageService.add(`saveCommercialPremise: ${ JSON.stringify(this.commercialPremise, undefined, 4) }`) if (this.commercialPremise.id == 0) { this.messageService.add("about to insert new commercialPremise") this.commercialPremise = await this.commercialPremiseService.postCommercialPremise(this.commercialPremise) this.messageService.add(`Successfully added commercialPremise with id ${this.commercialPremise.id}`) } else { this.messageService.add("about to update existing commercialPremise") this.commercialPremise = await this.commercialPremiseService.putCommercialPremise(this.commercialPremise) this.messageService.add(`Successfully changed commercialPremise with id ${this.commercialPremise.id}`) } this.router.navigate(['/commercialunits']) } finally { this.submitButton.disabled = false } } ngOnInit(): void { this.getPremises() this.getCommercialPremise() } }