barker/bookie/src/app/guest-book/guest-book-detail/guest-book-detail.component.ts

130 lines
4.2 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { 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 { Customer } from '../../core/customer';
import { ToasterService } from '../../core/toaster.service';
import { CustomerService } from '../../customers/customer.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('phone', { static: true }) phoneElement?: ElementRef;
form: FormGroup<{
name: FormControl<string>;
phone: FormControl<string | Customer>;
pax: FormControl<number>;
address: FormControl<string | null>;
hours: FormControl<number>;
minutes: FormControl<number>;
}>;
item: GuestBook = new GuestBook();
customers: Observable<Customer[]>;
constructor(
private route: ActivatedRoute,
private router: Router,
private toaster: ToasterService,
private ser: GuestBookService,
private customerService: CustomerService,
) {
// Create form
this.form = new FormGroup({
name: new FormControl('', { validators: Validators.required, nonNullable: true }),
phone: new FormControl<string | Customer>('', {
validators: Validators.required,
nonNullable: true,
}),
pax: new FormControl(0, { validators: Validators.required, nonNullable: true }),
address: new FormControl<string | null>(null),
hours: new FormControl(0, { validators: [Validators.min(0), Validators.max(23)], nonNullable: true }),
minutes: new FormControl(0, { validators: [Validators.min(0), Validators.max(59)], nonNullable: true }),
});
// Setup Account Autocomplete
this.customers = this.form.controls.phone.valueChanges.pipe(
startWith(null),
map((x) => (x !== null && (x as string).length >= 1 ? x : null)),
debounceTime(150),
distinctUntilChanged(),
switchMap((x) => (x === null ? observableOf([]) : this.customerService.autocomplete(x as string))),
);
}
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,
hours: +item.date.substring(12, 14),
minutes: +item.date.substring(15, 17),
});
}
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 = formModel.pax ?? 0;
this.item.address = formModel.address ?? null;
const hours = ('' + (formModel.hours as number)).padStart(2, '0');
const minutes = ('' + (formModel.minutes as number)).padStart(2, '0');
this.item.date = this.item.date.substring(0, 12) + hours + ':' + minutes;
return this.item;
}
}