changes for details
This commit is contained in:
83
ui/hv2-ui/src/app/flat-details/flat-details.component.ts
Normal file
83
ui/hv2-ui/src/app/flat-details/flat-details.component.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import { Component, OnInit, ViewChild } from '@angular/core';
|
||||
import { MatButton } from '@angular/material/button';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { FlatService, PremiseService } from '../data-object-service';
|
||||
import { Flat, Premise } from '../data-objects';
|
||||
import { MessageService } from '../message.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-flat-details',
|
||||
templateUrl: './flat-details.component.html',
|
||||
styleUrls: ['./flat-details.component.css']
|
||||
})
|
||||
export class FlatDetailsComponent implements OnInit {
|
||||
|
||||
flat: Flat = {
|
||||
id: 0,
|
||||
description: '',
|
||||
premise: 0,
|
||||
area: 0.0,
|
||||
flat_no: 0
|
||||
}
|
||||
|
||||
premise: Premise = {
|
||||
id: 0,
|
||||
description: '',
|
||||
street: '',
|
||||
zip: '',
|
||||
city: ''
|
||||
}
|
||||
|
||||
premises: Premise[]
|
||||
|
||||
@ViewChild('submitButton') submitButton: MatButton
|
||||
|
||||
constructor(
|
||||
private flatService: FlatService,
|
||||
private premiseService: PremiseService,
|
||||
private messageService: MessageService,
|
||||
private route: ActivatedRoute,
|
||||
private router: Router
|
||||
) { }
|
||||
|
||||
async getFlat(): Promise<void> {
|
||||
try {
|
||||
const id = +this.route.snapshot.paramMap.get('id')
|
||||
if (id != 0) {
|
||||
this.flat = await this.flatService.getFlat(id)
|
||||
this.premise = await this.premiseService.getPremise(this.flat.premise)
|
||||
}
|
||||
} catch (err) {
|
||||
this.messageService.add(JSON.stringify(err, undefined, 4))
|
||||
}
|
||||
}
|
||||
|
||||
async getPremises(): Promise<void> {
|
||||
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 saveFlat() {
|
||||
this.submitButton.disabled = true
|
||||
this.messageService.add(`saveFlat: ${ JSON.stringify(this.flat, undefined, 4) }`)
|
||||
if (this.flat.id == 0) {
|
||||
this.messageService.add("about to insert new flat")
|
||||
this.flat = await this.flatService.postFlat(this.flat)
|
||||
this.messageService.add(`Successfully added flat with id ${this.flat.id}`)
|
||||
} else {
|
||||
this.messageService.add("about to update existing flat")
|
||||
}
|
||||
this.router.navigate(['/flats'])
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.getPremises()
|
||||
this.getFlat()
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user