81 lines
2.0 KiB
TypeScript
81 lines
2.0 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { ToasterService } from '../../core/toaster.service';
|
|
import { GuestBook } from '../guest-book';
|
|
import { GuestBookService } from '../guest-book.service';
|
|
|
|
@Component({
|
|
selector: 'app-guest-book-detail',
|
|
templateUrl: './guest-book-detail.component.html',
|
|
styleUrls: ['./guest-book-detail.component.css'],
|
|
})
|
|
export class GuestBookDetailComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('name', { static: true }) nameElement: ElementRef;
|
|
form: FormGroup;
|
|
item: GuestBook;
|
|
|
|
constructor(
|
|
private fb: FormBuilder,
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private toaster: ToasterService,
|
|
private ser: GuestBookService,
|
|
) {
|
|
this.createForm();
|
|
}
|
|
|
|
createForm() {
|
|
this.form = this.fb.group({
|
|
name: [null, Validators.required],
|
|
phone: [null, Validators.required],
|
|
pax: ['0', Validators.required],
|
|
address: null,
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((data: { item: GuestBook }) => {
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
this.nameElement.nativeElement.focus();
|
|
}, 0);
|
|
}
|
|
|
|
showItem(item: GuestBook) {
|
|
this.item = item;
|
|
this.form.setValue({
|
|
name: item.name,
|
|
phone: item.phone,
|
|
pax: `${item.pax}`,
|
|
address: item.address,
|
|
});
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/guest-book');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error.error);
|
|
},
|
|
);
|
|
}
|
|
|
|
getItem(): GuestBook {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name;
|
|
this.item.phone = formModel.phone;
|
|
this.item.pax = parseInt(formModel.pax, 10);
|
|
this.item.address = formModel.address;
|
|
return this.item;
|
|
}
|
|
}
|