import { ChangeDetectionStrategy, Component, Input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; import { MatDrawer } from '@angular/material/sidenav'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { Contact } from 'app/modules/admin/apps/chat/chat.types'; import { ChatService } from 'app/modules/admin/apps/chat/chat.service'; @Component({ selector : 'chat-new-chat', templateUrl : './new-chat.component.html', encapsulation : ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush }) export class NewChatComponent implements OnInit, OnDestroy { @Input() drawer: MatDrawer; contacts: Contact[] = []; private _unsubscribeAll: Subject = new Subject(); /** * Constructor */ constructor(private _chatService: ChatService) { } // ----------------------------------------------------------------------------------------------------- // @ Lifecycle hooks // ----------------------------------------------------------------------------------------------------- /** * On init */ ngOnInit(): void { // Contacts this._chatService.contacts$ .pipe(takeUntil(this._unsubscribeAll)) .subscribe((contacts: Contact[]) => { this.contacts = contacts; }); } /** * On destroy */ ngOnDestroy(): void { // Unsubscribe from all subscriptions this._unsubscribeAll.next(); this._unsubscribeAll.complete(); } // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- /** * Track by function for ngFor loops * * @param index * @param item */ trackByFn(index: number, item: any): any { return item.id || index; } }