146 lines
4.4 KiB
TypeScript
146 lines
4.4 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { Observable } from 'rxjs';
|
|
import { distinctUntilChanged, startWith, switchMap } from 'rxjs/operators';
|
|
|
|
import { Contact } from '../../core/contact';
|
|
import { Department } from '../../core/department';
|
|
import { Office } from '../../core/office';
|
|
import { ToasterService } from '../../core/toaster.service';
|
|
import { OfficeService } from '../../offices/office.service';
|
|
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
|
import { ContactService } from '../contact.service';
|
|
|
|
@Component({
|
|
selector: 'app-contact-detail',
|
|
templateUrl: './contact-detail.component.html',
|
|
styleUrls: ['./contact-detail.component.css'],
|
|
})
|
|
export class ContactDetailComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
|
form: FormGroup;
|
|
departments: Department[] = [];
|
|
offices: Observable<Office[]> = new Observable<Office[]>();
|
|
item: Contact = new Contact();
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private dialog: MatDialog,
|
|
private fb: FormBuilder,
|
|
private toaster: ToasterService,
|
|
private ser: ContactService,
|
|
private officeService: OfficeService,
|
|
) {
|
|
// Create form
|
|
this.form = this.fb.group({
|
|
name: '',
|
|
mobile: '',
|
|
landline: '',
|
|
email: '',
|
|
address: '',
|
|
department: '',
|
|
office: '',
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { item: Contact; departments: Department[] };
|
|
this.departments = data.departments;
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
showItem(item: Contact) {
|
|
this.item = item;
|
|
this.form.setValue({
|
|
name: this.item.name,
|
|
mobile: this.item.mobile,
|
|
landline: this.item.landline,
|
|
email: this.item.email,
|
|
address: this.item.address,
|
|
department: this.item.department ? this.item.department.id : '',
|
|
office: this.item.office ? this.item.office.id : '',
|
|
});
|
|
this.offices = (this.form.get('department') as FormControl).valueChanges.pipe(
|
|
startWith(this.item.department ? this.item.department.id : ''),
|
|
distinctUntilChanged(),
|
|
switchMap((x) => this.officeService.filteredList(x)),
|
|
);
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.nameElement !== undefined) {
|
|
this.nameElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/contacts');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.item.id as string).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/contacts');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: { title: 'Delete Contact?', content: 'Are you sure? This cannot be undone.' },
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
getItem(): Contact {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name;
|
|
this.item.mobile = formModel.mobile;
|
|
this.item.landline = formModel.landline;
|
|
this.item.email = formModel.email;
|
|
this.item.address = formModel.address;
|
|
if (formModel.department === undefined || formModel.department === '') {
|
|
this.item.department = undefined;
|
|
} else {
|
|
if (this.item.department === null || this.item.department === undefined) {
|
|
this.item.department = new Department();
|
|
}
|
|
this.item.department.id = formModel.department;
|
|
}
|
|
if (formModel.office === undefined || formModel.office === '') {
|
|
this.item.office = undefined;
|
|
} else {
|
|
if (this.item.office === null || this.item.office === undefined) {
|
|
this.item.office = new Office();
|
|
}
|
|
this.item.office.id = formModel.office;
|
|
}
|
|
return this.item;
|
|
}
|
|
}
|