112 lines
3.2 KiB
TypeScript
112 lines
3.2 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
|
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { Observable, of as observableOf } from 'rxjs';
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
|
|
|
import { ToasterService } from '../../core/toaster.service';
|
|
import { Customer } from '../customer';
|
|
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('phone', { static: true }) phoneElement?: ElementRef;
|
|
form: FormGroup;
|
|
item: GuestBook = new GuestBook();
|
|
customers: Observable<Customer[]>;
|
|
|
|
constructor(
|
|
private fb: FormBuilder,
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private toaster: ToasterService,
|
|
private ser: GuestBookService,
|
|
) {
|
|
// Create form
|
|
this.form = this.fb.group({
|
|
name: [null, Validators.required],
|
|
phone: [null, Validators.required],
|
|
pax: ['0', Validators.required],
|
|
address: null,
|
|
});
|
|
// Setup Account Autocomplete
|
|
this.customers = (this.form.get('phone') as FormControl).valueChanges.pipe(
|
|
startWith(null),
|
|
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
|
debounceTime(150),
|
|
distinctUntilChanged(),
|
|
switchMap((x) => (x === null ? observableOf([]) : this.ser.autocomplete(x))),
|
|
);
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { item: GuestBook };
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.phoneElement !== undefined) {
|
|
this.phoneElement.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('Error', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
displayFn(customer?: Customer): string {
|
|
return customer ? customer.phone : '';
|
|
}
|
|
|
|
selected(event: MatAutocompleteSelectedEvent): void {
|
|
const customer = event.option.value;
|
|
this.form.patchValue({
|
|
name: customer.name,
|
|
// phone: customer.phone,
|
|
address: customer.address,
|
|
});
|
|
}
|
|
|
|
getItem(): GuestBook {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name;
|
|
if (typeof formModel.phone === 'string') {
|
|
this.item.phone = formModel.phone;
|
|
} else {
|
|
this.item.phone = (formModel.phone as Customer).phone;
|
|
}
|
|
this.item.pax = parseInt(formModel.pax, 10);
|
|
this.item.address = formModel.address;
|
|
return this.item;
|
|
}
|
|
}
|