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

71 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-09-01 17:58:49 +02:00
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatButton } from '@angular/material/button';
import { ActivatedRoute, Router } from '@angular/router';
2021-11-08 21:04:21 +01:00
import { AccountService, PremiseService } from '../data-object-service';
import { Account, NULL_Premise, Premise } from '../data-objects';
2021-09-01 17:58:49 +02:00
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 {
2021-11-08 21:04:21 +01:00
collapseDetails: boolean = false
collapseOverheadAccount: boolean = false
2021-09-01 17:58:49 +02:00
@ViewChild('submitButton') submitButton: MatButton
2021-11-08 21:04:21 +01:00
premise: Premise = NULL_Premise
overheadAccount: Account
overheadAccountId: number
2021-09-01 17:58:49 +02:00
constructor(
private premiseService: PremiseService,
2021-11-08 21:04:21 +01:00
private accountService: AccountService,
2021-09-01 17:58:49 +02:00
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)
2021-11-08 21:04:21 +01:00
this.overheadAccount = await this.accountService.getAccount(this.premise.account)
this.overheadAccountId = this.overheadAccount.id
2021-09-01 17:58:49 +02:00
}
} 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
2021-09-01 17:58:49 +02:00
}
}
ngOnInit(): void {
this.getPremise()
}
}