import { Effect , Actions , ofType } from '@ngrx/effects' ; import { Injectable } from '@angular/core' ; import { map , switchMap , debounceTime , take , catchError } from 'rxjs/operators' ; import { Store } from '@ngrx/store' ; import { FileFromTemplate , FolderFromTemplate , CreateFromTemplate , CreateFromTemplateSuccess , TemplateActionTypes , getCurrentFolder , AppStore , SnackbarErrorAction } from '@alfresco/aca-shared/store' ; import { NodeTemplateService , TemplateDialogConfig } from '../../services/node-template.service' ; import { AlfrescoApiService } from '@alfresco/adf-core' ; import { AppHookService } from '@alfresco/aca-shared' ; import { from , Observable , of } from 'rxjs' ; import { NodeEntry , NodeBodyUpdate , Node , NodesApi } from '@alfresco/js-api' ; import { MatDialog } from '@angular/material/dialog' ; @ Injectable ( ) export class TemplateEffects { _nodesApi : NodesApi ; get nodesApi ( ) : NodesApi { this . _nodesApi = this . _nodesApi ?? new NodesApi ( this . apiService . getInstance ( ) ) ; return this . _nodesApi ; } constructor ( private matDialog , private appHookService , private store , private apiService , private actions$ , private nodeTemplateService ) { } @ Effect ( { dispatch : false } ) fileFromTemplate$ = this . actions$ . pipe ( ofType < FileFromTemplate > ( TemplateActionTypes . FileFromTemplate ) , map ( ( ) => { this . openDialog ( { primaryPathName : 'app:node_templates' , selectionType : 'file' } ) ; } ) ) ; @ Effect ( { dispatch : false } ) folderFromTemplate$ = this . actions$ . pipe ( ofType < FolderFromTemplate > ( TemplateActionTypes . FolderFromTemplate ) , map ( ( ) => this . openDialog ( { primaryPathName : 'app:space_templates' , selectionType : 'folder' } ) ) ) ; @ Effect ( { dispatch : false } ) createFromTemplate$ = this . actions$ . pipe ( ofType < CreateFromTemplate > ( TemplateActionTypes . CreateFromTemplate ) , map ( ( action ) => { this . store . select ( getCurrentFolder ) . pipe ( switchMap ( ( folder ) => { return this . copyNode ( action . payload , folder . id ) ; } ) , take ( 1 ) ) . subscribe ( ( node ) => { if ( node ) { this . store . dispatch ( new CreateFromTemplateSuccess ( node . entry ) ) ; } } ) ; } ) ) ; @ Effect ( { dispatch : false } ) createFromTemplateSuccess$ = this . actions$ . pipe ( ofType < CreateFromTemplateSuccess > ( TemplateActionTypes . CreateFromTemplateSuccess ) , map ( ( payload ) => { this . matDialog . closeAll ( ) ; this . appHookService . reload . next ( payload . node ) ; } ) ) ; private openDialog ( config ) { this . nodeTemplateService . selectTemplateDialog ( config ) . pipe ( debounceTime ( 300 ) ) . subscribe ( ( [ node ] ) => this . nodeTemplateService . createTemplateDialog ( node ) ) ; } private copyNode ( source , parentId ) { return from ( this . nodesApi . copyNode ( source . id , { targetParentId : parentId , name : source . name } ) ) . pipe ( switchMap ( ( node ) => this . updateNode ( node , { properties : { 'cm:title' : source . properties [ 'cm:title' ] , 'cm:description' : source . properties [ 'cm:description' ] } } ) ) , catchError ( ( error ) => { return this . handleError ( error ) ; } ) ) ; } private updateNode ( node , update ) { return from ( this . nodesApi . updateNode ( node . entry . id , update ) ) . pipe ( catchError ( ( ) => of ( node ) ) ) ; } private handleError ( error ) { let statusCode ; try { statusCode = JSON . parse ( error . message ) . error . statusCode ; } catch ( e ) { statusCode = null ; } if ( statusCode !== 409 ) { this . store . dispatch ( new SnackbarErrorAction ( 'APP.MESSAGES.ERRORS.GENERIC' ) ) ; } else { this . store . dispatch ( new SnackbarErrorAction ( 'APP.MESSAGES.ERRORS.CONFLICT' ) ) ; } return of ( null ) ; } }