import { Component , OnInit , OnDestroy } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { first } from 'rxjs/operators' ; import { User } from '@app/_models' ; import { UserService , AuthenticationService } from '@app/_services' ; @ Component ( { templateUrl : 'home.component.html' } ) export class HomeComponent implements OnInit , OnDestroy { currentUser : User ; currentUserSubscription : Subscription ; users : User [ ] = [ ] ; constructor ( private authenticationService , private userService ) { this . currentUserSubscription = this . authenticationService . currentUser . subscribe ( user => { this . currentUser = user ; } ) ; } ngOnInit ( ) { this . loadAllUsers ( ) ; } ngOnDestroy ( ) { this . currentUserSubscription . unsubscribe ( ) ; } deleteUser ( id ) { this . userService . delete ( id ) . pipe ( first ( ) ) . subscribe ( ( ) => { this . loadAllUsers ( ) ; } ) ; } private loadAllUsers ( ) { this . userService . getAll ( ) . pipe ( first ( ) ) . subscribe ( users => { this . users = users ; } ) ; } }