hv2-all-in-one/ui/hv2-ui/src/app/premise-details/premise-details.component.ts

71 lines
2.3 KiB
TypeScript

import { Component, OnInit, ViewChild } from '@angular/core';
import { MatButton } from '@angular/material/button';
import { ActivatedRoute, Router } from '@angular/router';
import { AccountService, PremiseService } from '../data-object-service';
import { Account, NULL_Premise, 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 {
collapseDetails: boolean = false
collapseOverheadAccount: boolean = false
@ViewChild('submitButton') submitButton: MatButton
premise: Premise = NULL_Premise
overheadAccount: Account
overheadAccountId: number
constructor(
private premiseService: PremiseService,
private accountService: AccountService,
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)
this.overheadAccount = await this.accountService.getAccount(this.premise.account)
this.overheadAccountId = this.overheadAccount.id
}
} catch (err) {
this.messageService.add(JSON.stringify(err, undefined, 4))
}
}
async savePremise() {
try {
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.premise = await this.premiseService.putPremise(this.premise)
this.messageService.add(`Successfully changed premises with id ${this.premise.id}`)
}
this.router.navigate(['/premises'])
} finally {
this.submitButton.disabled = false
}
}
ngOnInit(): void {
this.getPremise()
}
}