import { Component , ViewEncapsulation , OnInit , OnDestroy } from '@angular/core' ; import { Store } from '@ngrx/store' ; import { AppHookService } from '@alfresco/aca-shared' ; import { AppStore , getAppSelection } from '@alfresco/aca-shared/store' ; import { SelectionState } from '@alfresco/adf-extensions' ; import { distinctUntilChanged , takeUntil } from 'rxjs/operators' ; import { Router } from '@angular/router' ; import { Subject } from 'rxjs' ; @ Component ( { selector : 'app-toggle-favorite-library' , template : `
    <button
      mat-menu-item
      (toggle)="onToggleEvent()"
      [adf-favorite-library]="library"
      [attr.title]="library.isFavorite ? ('APP.ACTIONS.REMOVE_FAVORITE' | translate) : ('APP.ACTIONS.FAVORITE' | translate)"
    >
      <mat-icon *ngIf="library.isFavorite">star</mat-icon>
      <mat-icon *ngIf="!library.isFavorite">star_border</mat-icon>
      <span>{{ (library.isFavorite ? 'APP.ACTIONS.REMOVE_FAVORITE' : 'APP.ACTIONS.FAVORITE') | translate }}</span>
    </button>
  ` , encapsulation : ViewEncapsulation . None , host : { class : 'app-toggle-favorite-library' } } ) export class ToggleFavoriteLibraryComponent implements OnInit , OnDestroy { library ; private onDestroy$ : Subject < boolean > = new Subject < boolean > ( ) ; constructor ( private store , private appHookService , private router ) { } ngOnInit ( ) { const isFavoriteLibraries = this . router . url . startsWith ( '/favorite/libraries' ) ; this . store . select ( getAppSelection ) . pipe ( distinctUntilChanged ( ) , takeUntil ( this . onDestroy$ ) ) . subscribe ( ( selection ) => { this . library = { ... selection . library } ; if ( selection . library && isFavoriteLibraries ) { this . library . isFavorite = true ; } } ) ; } ngOnDestroy ( ) { this . onDestroy$ . next ( true ) ; this . onDestroy$ . complete ( ) ; } onToggleEvent ( ) { this . appHookService . favoriteLibraryToggle . next ( ) ; } }