import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MatDialog } from '@angular/material'; import { Subscription } from 'rxjs'; import { debounceTime, distinctUntilChanged } from 'rxjs/operators'; import { fuseAnimations } from '@fuse/animations'; import { FuseContactsContactFormDialogComponent } from './contact-form/contact-form.component'; import { ContactsService } from './contacts.service'; @Component({ selector : 'fuse-contacts', templateUrl : './contacts.component.html', styleUrls : ['./contacts.component.scss'], encapsulation: ViewEncapsulation.None, animations : fuseAnimations }) export class FuseContactsComponent implements OnInit, OnDestroy { hasSelectedContacts: boolean; searchInput: FormControl; dialogRef: any; onSelectedContactsChangedSubscription: Subscription; constructor( private contactsService: ContactsService, public dialog: MatDialog ) { this.searchInput = new FormControl(''); } newContact() { this.dialogRef = this.dialog.open(FuseContactsContactFormDialogComponent, { panelClass: 'contact-form-dialog', data : { action: 'new' } }); this.dialogRef.afterClosed() .subscribe((response: FormGroup) => { if ( !response ) { return; } this.contactsService.updateContact(response.getRawValue()); }); } ngOnInit() { this.onSelectedContactsChangedSubscription = this.contactsService.onSelectedContactsChanged .subscribe(selectedContacts => { this.hasSelectedContacts = selectedContacts.length > 0; }); this.searchInput.valueChanges .pipe( debounceTime(300), distinctUntilChanged() ) .subscribe(searchText => { this.contactsService.onSearchTextChanged.next(searchText); }); } ngOnDestroy() { this.onSelectedContactsChangedSubscription.unsubscribe(); } }