import { AppStore , DownloadNodesAction , EditOfflineAction , SnackbarErrorAction , getAppSelection } from '@alfresco/aca-shared/store' ; import { MinimalNodeEntity , NodeEntry , SharedLinkEntry , Node , NodesApi } from '@alfresco/js-api' ; import { Component , OnInit , ViewEncapsulation } from '@angular/core' ; import { Store } from '@ngrx/store' ; import { isLocked } from '@alfresco/aca-shared' ; import { AlfrescoApiService } from '@alfresco/adf-core' ; @ Component ( { selector : 'app-toggle-edit-offline' , template : `
    <button
      mat-menu-item
      [attr.title]="(isNodeLocked ? 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' : 'APP.ACTIONS.EDIT_OFFLINE') | translate"
      (click)="onClick()"
    >
      <ng-container *ngIf="isNodeLocked">
        <mat-icon>cancel</mat-icon>
        <span>{{ 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' | translate }}</span>
      </ng-container>

      <ng-container *ngIf="!isNodeLocked">
        <mat-icon>edit</mat-icon>
        <span>{{ 'APP.ACTIONS.EDIT_OFFLINE' | translate }}</span>
      </ng-container>
    </button>
  ` , encapsulation : ViewEncapsulation . None , host : { class : 'app-toggle-edit-offline' } } ) export class ToggleEditOfflineComponent implements OnInit { private nodesApi : NodesApi ; selection : MinimalNodeEntity ; constructor ( private store , private alfrescoApiService ) { this . nodesApi = new NodesApi ( this . alfrescoApiService . getInstance ( ) ) ; } ngOnInit ( ) { this . store . select ( getAppSelection ) . subscribe ( ( { file } ) => { this . selection = file ; } ) ; } get isNodeLocked ( ) : boolean { return ! ! ( this . selection && isLocked ( this . selection ) ) ; } async onClick ( ) { await this . toggleLock ( this . selection ) ; } private async toggleLock ( node ) { const id = ( node as SharedLinkEntry ) . entry . nodeId || node . entry . id ; if ( isLocked ( this . selection ) ) { try { const response = await this . unlockNode ( id ) ; this . update ( response ?. entry ) ; this . store . dispatch ( new EditOfflineAction ( this . selection ) ) ; } catch { this . onUnlockError ( ) ; } } else { try { const response = await this . lockNode ( id ) ; this . update ( response ?. entry ) ; this . store . dispatch ( new DownloadNodesAction ( [ this . selection ] ) ) ; this . store . dispatch ( new EditOfflineAction ( this . selection ) ) ; } catch { this . onLockError ( ) ; } } } onLockError ( ) { this . store . dispatch ( new SnackbarErrorAction ( 'APP.MESSAGES.ERRORS.LOCK_NODE' , { fileName : this . selection . entry . name } ) ) ; } onUnlockError ( ) { this . store . dispatch ( new SnackbarErrorAction ( 'APP.MESSAGES.ERRORS.UNLOCK_NODE' , { fileName : this . selection . entry . name } ) ) ; } lockNode ( nodeId ) { return this . nodesApi . lockNode ( nodeId , { type : 'ALLOW_OWNER_CHANGES' , lifetime : 'PERSISTENT' } ) ; } unlockNode ( nodeId ) { return this . nodesApi . unlockNode ( nodeId ) ; } private update ( data ) { if ( data && data . properties ) { const properties = this . selection . entry . properties || { } ; properties [ 'cm:lockLifetime' ] = data . properties [ 'cm:lockLifetime' ] ; properties [ 'cm:lockOwner' ] = data . properties [ 'cm:lockOwner' ] ; properties [ 'cm:lockType' ] = data . properties [ 'cm:lockType' ] ; this . selection . entry . properties = properties ; } } }