diff --git a/src/app/store/app-store.module.ts b/src/app/store/app-store.module.ts index 4ea9aae32..bd71d2f60 100644 --- a/src/app/store/app-store.module.ts +++ b/src/app/store/app-store.module.ts @@ -39,7 +39,8 @@ import { SearchEffects, LibraryEffects, UploadEffects, - FavoriteEffects + FavoriteEffects, + TemplateEffects } from './effects'; import { INITIAL_STATE } from './initial-state'; @@ -56,7 +57,8 @@ import { INITIAL_STATE } from './initial-state'; SearchEffects, LibraryEffects, UploadEffects, - FavoriteEffects + FavoriteEffects, + TemplateEffects ]), !environment.production ? StoreDevtoolsModule.instrument({ maxAge: 25 }) diff --git a/src/app/store/effects.ts b/src/app/store/effects.ts index 29bee5773..4a7b28ca2 100644 --- a/src/app/store/effects.ts +++ b/src/app/store/effects.ts @@ -32,3 +32,4 @@ export * from './effects/search.effects'; export * from './effects/library.effects'; export * from './effects/upload.effects'; export * from './effects/upload.effects'; +export * from './effects/template.effects'; diff --git a/src/app/store/effects/template.effects.ts b/src/app/store/effects/template.effects.ts new file mode 100644 index 000000000..9ae79a2e1 --- /dev/null +++ b/src/app/store/effects/template.effects.ts @@ -0,0 +1,87 @@ +/*! + * @license + * Alfresco Example Content Application + * + * Copyright (C) 2005 - 2019 Alfresco Software Limited + * + * This file is part of the Alfresco Example Content Application. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * The Alfresco Example Content Application is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Alfresco Example Content Application is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ + +import { Effect, Actions, ofType } from '@ngrx/effects'; +import { Injectable } from '@angular/core'; +import { map, withLatestFrom, switchMap, catchError } from 'rxjs/operators'; +import { Store } from '@ngrx/store'; +import { + CreateFileFromTemplate, + TemplateActionTypes, + getCurrentFolder, + AppStore, + SnackbarErrorAction +} from '@alfresco/aca-shared/store'; +import { CreateFileFromTemplateService } from '../../services/create-file-from-template.service'; +import { AlfrescoApiService } from '@alfresco/adf-core'; +import { ContentManagementService } from '../../services/content-management.service'; +import { from, of } from 'rxjs'; +import { NodeEntry } from '@alfresco/js-api'; + +@Injectable() +export class TemplateEffects { + constructor( + private content: ContentManagementService, + private store: Store, + private apiService: AlfrescoApiService, + private actions$: Actions, + private createFileFromTemplateService: CreateFileFromTemplateService + ) {} + + @Effect({ dispatch: false }) + fileFromTemplate$ = this.actions$.pipe( + ofType(TemplateActionTypes.CreateFileFromTemplate), + map(() => { + this.createFileFromTemplateService + .openTemplatesDialog() + .pipe( + withLatestFrom(this.store.select(getCurrentFolder)), + switchMap(([[template], parentNode]) => { + return from( + this.apiService + .getInstance() + .nodes.copyNode(template.id, { targetParentId: parentNode.id }) + ); + }), + catchError(error => { + const { statusCode } = JSON.parse(error.message).error; + + if (statusCode !== 409) { + this.store.dispatch( + new SnackbarErrorAction('APP.MESSAGES.ERRORS.GENERIC') + ); + } + + return of(null); + }) + ) + .subscribe((node: NodeEntry | null) => { + if (node) { + this.content.reload.next(); + } + }); + }) + ); +}