63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
||
|
import { MatButton } from '@angular/material/button';
|
||
|
import { ActivatedRoute, Router } from '@angular/router';
|
||
|
import { PremiseService } from '../data-object-service';
|
||
|
import { Premise } from '../data-objects';
|
||
|
import { MessageService } from '../message.service';
|
||
|
|
||
|
@Component({
|
||
|
selector: 'app-premise-details',
|
||
|
templateUrl: './premise-details.component.html',
|
||
|
styleUrls: ['./premise-details.component.css']
|
||
|
})
|
||
|
export class PremiseDetailsComponent implements OnInit {
|
||
|
|
||
|
@ViewChild('submitButton') submitButton: MatButton
|
||
|
|
||
|
premise: Premise = {
|
||
|
id: 0,
|
||
|
description: '',
|
||
|
street: '',
|
||
|
zip: '',
|
||
|
city: ''
|
||
|
}
|
||
|
|
||
|
constructor(
|
||
|
private premiseService: PremiseService,
|
||
|
private messageService: MessageService,
|
||
|
private route: ActivatedRoute,
|
||
|
private router: Router
|
||
|
) { }
|
||
|
|
||
|
async getPremise(): Promise<void> {
|
||
|
try {
|
||
|
const id = +this.route.snapshot.paramMap.get('id')
|
||
|
if (id != 0) {
|
||
|
this.premise = await this.premiseService.getPremise(id)
|
||
|
}
|
||
|
} catch (err) {
|
||
|
this.messageService.add(JSON.stringify(err, undefined, 4))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async savePremise() {
|
||
|
this.submitButton.disabled = true
|
||
|
this.messageService.add("savePremise")
|
||
|
this.messageService.add(JSON.stringify(this.premise, undefined, 4))
|
||
|
if (this.premise.id == 0) {
|
||
|
this.messageService.add("about to insert new premise")
|
||
|
this.premise = await this.premiseService.postPremise(this.premise)
|
||
|
this.messageService.add(`Successfully added premises with id ${this.premise.id}`)
|
||
|
} else {
|
||
|
this.messageService.add("about to update existing premise")
|
||
|
}
|
||
|
this.router.navigate(['/premises'])
|
||
|
// this.submitButton.disabled = false
|
||
|
}
|
||
|
|
||
|
ngOnInit(): void {
|
||
|
this.getPremise()
|
||
|
}
|
||
|
|
||
|
}
|