mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-07-31 17:38:28 +00:00
[ACA-1442] improved notifications and delete/restore (#393)
* remove notifications from files component * remove notifications from favorites page * remove irrelevant tests * snackbar effects * snackbar theme * improve permanent delete messaging * cleanup tests * strongly typed node delete directive, node actions * strongly-typed directives * test fixes * redux dev tools, migrate permanent delete directive * reload trashcan on service events * delete and restore nodes, snackbar effects with undo * wire viewer with store and effects * test fixes * migrate events * fix spelling * bug fixes * use notification effects on restore node * remove fdescribe * node-versions using snackbar actions * dispatch snackbars from node-move directive * store-enabled create folder * reduce dependency on ContentService for list reloads * favorites use unified preview api for files * simplify preview for shared files * remove test
This commit is contained in:
@@ -24,218 +24,35 @@
|
||||
*/
|
||||
|
||||
import { Directive, HostListener, Input } from '@angular/core';
|
||||
|
||||
import { TranslationService, NodesApiService, NotificationService } from '@alfresco/adf-core';
|
||||
import { MinimalNodeEntity } from 'alfresco-js-api';
|
||||
import { Observable } from 'rxjs/Rx';
|
||||
|
||||
import { ContentManagementService } from '../services/content-management.service';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { AppStore } from '../../store/states/app.state';
|
||||
import { DeleteNodesAction, NodeInfo } from '../../store/actions';
|
||||
|
||||
@Directive({
|
||||
selector: '[acaDeleteNode]'
|
||||
})
|
||||
export class NodeDeleteDirective {
|
||||
static RESTORE_MESSAGE_DURATION = 3000;
|
||||
static DELETE_MESSAGE_DURATION = 10000;
|
||||
|
||||
// tslint:disable-next-line:no-input-rename
|
||||
@Input('acaDeleteNode')
|
||||
selection: MinimalNodeEntity[];
|
||||
|
||||
constructor(private store: Store<AppStore>) {}
|
||||
|
||||
@HostListener('click')
|
||||
onClick() {
|
||||
this.deleteSelected();
|
||||
}
|
||||
if (this.selection && this.selection.length > 0) {
|
||||
const toDelete: NodeInfo[] = this.selection.map(node => {
|
||||
const { name } = node.entry;
|
||||
const id = node.entry.nodeId || node.entry.id;
|
||||
|
||||
constructor(
|
||||
private nodesApi: NodesApiService,
|
||||
private notification: NotificationService,
|
||||
private content: ContentManagementService,
|
||||
private translation: TranslationService
|
||||
) {}
|
||||
|
||||
private deleteSelected(): void {
|
||||
const batch = [];
|
||||
|
||||
this.selection.forEach((node) => {
|
||||
batch.push(this.performAction('delete', node.entry));
|
||||
});
|
||||
|
||||
Observable.forkJoin(...batch)
|
||||
.subscribe(
|
||||
(data) => {
|
||||
const processedData = this.processStatus(data);
|
||||
const message = this.getDeleteMessage(processedData);
|
||||
const withUndo = processedData.someSucceeded ? this.translation.instant('APP.ACTIONS.UNDO') : '';
|
||||
|
||||
this.notification.openSnackMessageAction(message, withUndo, NodeDeleteDirective.DELETE_MESSAGE_DURATION)
|
||||
.onAction()
|
||||
.subscribe(() => this.restore(processedData.success));
|
||||
|
||||
if (processedData.someSucceeded) {
|
||||
this.content.nodeDeleted.next(null);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private restore(items: any[]): void {
|
||||
const batch = [];
|
||||
|
||||
items.forEach((item) => {
|
||||
batch.push(this.performAction('restore', item));
|
||||
});
|
||||
|
||||
Observable.forkJoin(...batch)
|
||||
.subscribe(
|
||||
(data) => {
|
||||
const processedData = this.processStatus(data);
|
||||
|
||||
if (processedData.failed.length) {
|
||||
const message = this.getRestoreMessage(processedData);
|
||||
this.notification.openSnackMessageAction(
|
||||
message, '' , NodeDeleteDirective.RESTORE_MESSAGE_DURATION
|
||||
);
|
||||
}
|
||||
|
||||
if (processedData.someSucceeded) {
|
||||
this.content.nodeRestored.next(null);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private performAction(action: string, item: any): Observable<any> {
|
||||
const { name } = item;
|
||||
// Check if there's nodeId for Shared Files
|
||||
const id = item.nodeId || item.id;
|
||||
|
||||
let performedAction: any = null;
|
||||
|
||||
if (action === 'delete') {
|
||||
performedAction = this.nodesApi.deleteNode(id);
|
||||
} else {
|
||||
performedAction = this.nodesApi.restoreNode(id);
|
||||
}
|
||||
|
||||
return performedAction
|
||||
.map(() => {
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
status: 1
|
||||
name
|
||||
};
|
||||
})
|
||||
.catch((error: any) => {
|
||||
return Observable.of({
|
||||
id,
|
||||
name,
|
||||
status: 0
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private processStatus(data): any {
|
||||
const deleteStatus = {
|
||||
success: [],
|
||||
failed: [],
|
||||
get someFailed() {
|
||||
return !!(this.failed.length);
|
||||
},
|
||||
get someSucceeded() {
|
||||
return !!(this.success.length);
|
||||
},
|
||||
get oneFailed() {
|
||||
return this.failed.length === 1;
|
||||
},
|
||||
get oneSucceeded() {
|
||||
return this.success.length === 1;
|
||||
},
|
||||
get allSucceeded() {
|
||||
return this.someSucceeded && !this.someFailed;
|
||||
},
|
||||
get allFailed() {
|
||||
return this.someFailed && !this.someSucceeded;
|
||||
}
|
||||
};
|
||||
|
||||
return data.reduce(
|
||||
(acc, next) => {
|
||||
if (next.status === 1) {
|
||||
acc.success.push(next);
|
||||
} else {
|
||||
acc.failed.push(next);
|
||||
}
|
||||
|
||||
return acc;
|
||||
},
|
||||
deleteStatus
|
||||
);
|
||||
}
|
||||
|
||||
private getRestoreMessage(status): string {
|
||||
if (status.someFailed && !status.oneFailed) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.ERRORS.NODE_RESTORE_PLURAL',
|
||||
{ number: status.failed.length }
|
||||
);
|
||||
}
|
||||
|
||||
if (status.oneFailed) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.ERRORS.NODE_RESTORE',
|
||||
{ name: status.failed[0].name }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private getDeleteMessage(status): string {
|
||||
if (status.allFailed && !status.oneFailed) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.ERRORS.NODE_DELETION_PLURAL',
|
||||
{ number: status.failed.length }
|
||||
);
|
||||
}
|
||||
|
||||
if (status.allSucceeded && !status.oneSucceeded) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.INFO.NODE_DELETION.PLURAL',
|
||||
{ number: status.success.length }
|
||||
);
|
||||
}
|
||||
|
||||
if (status.someFailed && status.someSucceeded && !status.oneSucceeded) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.INFO.NODE_DELETION.PARTIAL_PLURAL',
|
||||
{
|
||||
success: status.success.length,
|
||||
failed: status.failed.length
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (status.someFailed && status.oneSucceeded) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.INFO.NODE_DELETION.PARTIAL_SINGULAR',
|
||||
{
|
||||
success: status.success.length,
|
||||
failed: status.failed.length
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (status.oneFailed && !status.someSucceeded) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.ERRORS.NODE_DELETION',
|
||||
{ name: status.failed[0].name }
|
||||
);
|
||||
}
|
||||
|
||||
if (status.oneSucceeded && !status.someFailed) {
|
||||
return this.translation.instant(
|
||||
'APP.MESSAGES.INFO.NODE_DELETION.SINGULAR',
|
||||
{ name: status.success[0].name }
|
||||
);
|
||||
this.store.dispatch(new DeleteNodesAction(toDelete));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user