deprecate lock-node directive (#1774)

* deprecate lock-node directive

* delete old file after rebase

* fix code and tests
This commit is contained in:
Denys Vuika 2020-12-10 19:17:28 +00:00 committed by GitHub
parent 94eb4c90ff
commit 882cc1c68b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 289 deletions

View File

@ -24,9 +24,8 @@
*/
import { ToggleEditOfflineComponent } from './toggle-edit-offline.component';
import { LockNodeDirective } from '../../../directives/lock-node.directive';
import { setupTestBed, CoreModule } from '@alfresco/adf-core';
import { TestBed } from '@angular/core/testing';
import { CoreModule } from '@alfresco/adf-core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { Store } from '@ngrx/store';
import { NodeEntry } from '@alfresco/js-api';
@ -34,16 +33,17 @@ import { DownloadNodesAction, EditOfflineAction, SnackbarErrorAction } from '@al
import { TranslateModule } from '@ngx-translate/core';
describe('ToggleEditOfflineComponent', () => {
let fixture;
let component;
let store;
let dispatchSpy;
let selectSpy;
const selection = { file: { entry: { name: 'test', properties: {} } } };
let fixture: ComponentFixture<ToggleEditOfflineComponent>;
let component: ToggleEditOfflineComponent;
let store: Store;
let dispatchSpy: jasmine.Spy;
let selectSpy: jasmine.Spy;
let selection: any;
setupTestBed({
beforeEach(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), CoreModule.forRoot()],
declarations: [ToggleEditOfflineComponent, LockNodeDirective],
declarations: [ToggleEditOfflineComponent],
providers: [
{
provide: Store,
@ -55,17 +55,16 @@ describe('ToggleEditOfflineComponent', () => {
]
});
beforeEach(() => {
fixture = TestBed.createComponent(ToggleEditOfflineComponent);
component = fixture.componentInstance;
spyOn(component, 'unlockNode').and.returnValue(Promise.resolve(null));
spyOn(component, 'lockNode').and.returnValue(Promise.resolve(null));
store = TestBed.inject(Store);
dispatchSpy = spyOn(store, 'dispatch');
selectSpy = spyOn(store, 'select');
});
afterEach(() => {
dispatchSpy.calls.reset();
selection = { file: { entry: { name: 'test', properties: {}, isLocked: false } } };
});
it('should initialized with data from store', () => {
@ -73,15 +72,15 @@ describe('ToggleEditOfflineComponent', () => {
fixture.detectChanges();
expect(component.selection).toEqual(selection.file);
expect(component.selection).toEqual(selection.file as any);
});
it('should download content if node is locked', () => {
it('should download content when node is locked', async () => {
selectSpy.and.returnValue(of(selection));
fixture.detectChanges();
const isLocked = true;
component.onToggleEvent(isLocked);
selection.file.entry.isLocked = false;
await component.onClick();
fixture.detectChanges();
@ -90,23 +89,21 @@ describe('ToggleEditOfflineComponent', () => {
it('should not download content if node is not locked', () => {
selectSpy.and.returnValue(of(selection));
fixture.detectChanges();
const isLocked = false;
component.onToggleEvent(isLocked);
component.onClick();
fixture.detectChanges();
expect(dispatchSpy.calls.argsFor(0)).not.toEqual([new DownloadNodesAction([selection.file as NodeEntry])]);
});
it('should dispatch EditOfflineAction action', () => {
it('should dispatch EditOfflineAction action', async () => {
selectSpy.and.returnValue(of(selection));
selection.file.entry.isLocked = true;
fixture.detectChanges();
const isLocked = false;
component.onToggleEvent(isLocked);
await component.onClick();
fixture.detectChanges();
expect(dispatchSpy.calls.argsFor(0)).toEqual([new EditOfflineAction(selection.file as NodeEntry)]);
@ -114,8 +111,8 @@ describe('ToggleEditOfflineComponent', () => {
it('should raise notification on lock error', () => {
selectSpy.and.returnValue(of(selection));
fixture.detectChanges();
fixture.detectChanges();
component.onLockError();
fixture.detectChanges();
@ -128,9 +125,9 @@ describe('ToggleEditOfflineComponent', () => {
it('should raise notification on unlock error', () => {
selectSpy.and.returnValue(of(selection));
fixture.detectChanges();
component.onUnlockLockError();
fixture.detectChanges();
component.onUnlockError();
fixture.detectChanges();
expect(dispatchSpy.calls.argsFor(0)).toEqual([

View File

@ -24,28 +24,26 @@
*/
import { AppStore, DownloadNodesAction, EditOfflineAction, SnackbarErrorAction, getAppSelection } from '@alfresco/aca-shared/store';
import { MinimalNodeEntity } from '@alfresco/js-api';
import { MinimalNodeEntity, NodeEntry, SharedLinkEntry, Node } 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
#lock="lockNode"
mat-menu-item
(toggle)="onToggleEvent($event)"
(lockError)="onLockError()"
(unlockError)="onUnlockLockError()"
[acaLockNode]="selection"
[attr.title]="lock.isNodeLocked() ? ('APP.ACTIONS.EDIT_OFFLINE_CANCEL' | translate) : ('APP.ACTIONS.EDIT_OFFLINE' | translate)"
[attr.title]="(isNodeLocked ? 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' : 'APP.ACTIONS.EDIT_OFFLINE') | translate"
(click)="onClick()"
>
<ng-container *ngIf="lock.isNodeLocked()">
<ng-container *ngIf="isNodeLocked">
<mat-icon>cancel</mat-icon>
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE_CANCEL' | translate }}</span>
</ng-container>
<ng-container *ngIf="!lock.isNodeLocked()">
<ng-container *ngIf="!isNodeLocked">
<mat-icon>edit</mat-icon>
<span>{{ 'APP.ACTIONS.EDIT_OFFLINE' | translate }}</span>
</ng-container>
@ -57,7 +55,7 @@ import { Store } from '@ngrx/store';
export class ToggleEditOfflineComponent implements OnInit {
selection: MinimalNodeEntity;
constructor(private store: Store<AppStore>) {}
constructor(private store: Store<AppStore>, private alfrescoApiService: AlfrescoApiService) {}
ngOnInit() {
this.store.select(getAppSelection).subscribe(({ file }) => {
@ -65,11 +63,37 @@ export class ToggleEditOfflineComponent implements OnInit {
});
}
onToggleEvent(isNodeLocked: boolean) {
if (isNodeLocked) {
this.store.dispatch(new DownloadNodesAction([this.selection]));
get isNodeLocked(): boolean {
return !!(this.selection && isLocked(this.selection));
}
async onClick() {
await this.toggleLock(this.selection);
}
private async toggleLock(node: NodeEntry | SharedLinkEntry) {
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() {
@ -80,11 +104,34 @@ export class ToggleEditOfflineComponent implements OnInit {
);
}
onUnlockLockError() {
onUnlockError() {
this.store.dispatch(
new SnackbarErrorAction('APP.MESSAGES.ERRORS.UNLOCK_NODE', {
fileName: this.selection.entry.name
})
);
}
lockNode(nodeId: string) {
return this.alfrescoApiService.nodesApi.lockNode(nodeId, {
type: 'ALLOW_OWNER_CHANGES',
lifetime: 'PERSISTENT'
});
}
unlockNode(nodeId: string) {
return this.alfrescoApiService.nodesApi.unlockNode(nodeId);
}
private update(data: Node) {
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;
}
}
}

View File

@ -27,11 +27,10 @@ import { NgModule } from '@angular/core';
import { DocumentListDirective } from './document-list.directive';
import { LibraryMembershipDirective } from './library-membership.directive';
import { LibraryFavoriteDirective } from './library-favorite.directive';
import { LockNodeDirective } from './lock-node.directive';
import { SharedDirectivesModule } from '@alfresco/aca-shared';
export function directives() {
return [DocumentListDirective, LibraryMembershipDirective, LibraryFavoriteDirective, LockNodeDirective];
return [DocumentListDirective, LibraryMembershipDirective, LibraryFavoriteDirective];
}
@NgModule({

View File

@ -1,134 +0,0 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 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 <http://www.gnu.org/licenses/>.
*/
import { Component, ViewChild } from '@angular/core';
import { LockNodeDirective } from './lock-node.directive';
import { AlfrescoApiService, setupTestBed, CoreModule } from '@alfresco/adf-core';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { AppTestingModule } from '../testing/app-testing.module';
@Component({
selector: 'app-test-component',
template: ` <button #lock="lockNode" [acaLockNode]="selection">Lock</button> `
})
class TestComponent {
@ViewChild('lock')
directive: LockNodeDirective;
selection = null;
}
describe('LockNodeDirective', () => {
let fixture;
let api;
let component;
setupTestBed({
imports: [TranslateModule.forRoot(), CoreModule.forRoot(), AppTestingModule],
declarations: [TestComponent, LockNodeDirective]
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.selection = null;
api = TestBed.inject(AlfrescoApiService);
});
it('should return false if selection is null', () => {
component.selection = null;
fixture.detectChanges();
expect(component.directive.isNodeLocked()).toBe(false);
});
it('should return false if selection is not locked', () => {
component.selection = { entry: { name: 'test-name', properties: {} } };
fixture.detectChanges();
expect(component.directive.isNodeLocked()).toBe(false);
});
it('should return true if selection is locked', () => {
component.selection = {
entry: {
name: 'test-name',
properties: { 'cm:lockType': 'WRITE_LOCK' }
}
};
fixture.detectChanges();
expect(component.directive.isNodeLocked()).toBe(true);
});
it('should lock selection', fakeAsync(() => {
component.selection = {
entry: {
id: 'id',
name: 'test-name',
properties: {}
}
};
spyOn(api.nodesApi, 'lockNode').and.returnValue(
Promise.resolve({
entry: { properties: { 'cm:lockType': 'WRITE_LOCK' } }
})
);
fixture.detectChanges();
component.directive.onClick();
tick();
fixture.detectChanges();
expect(component.selection.entry.properties['cm:lockType']).toBe('WRITE_LOCK');
}));
it('should unlock selection', fakeAsync(() => {
component.selection = {
entry: {
id: 'id',
name: 'test-name',
properties: {
'cm:lockType': 'WRITE_LOCK'
}
}
};
spyOn(api.nodesApi, 'unlockNode').and.returnValue(
Promise.resolve({
entry: { properties: {} }
})
);
fixture.detectChanges();
component.directive.onClick();
tick();
fixture.detectChanges();
expect(component.selection.entry.properties['cm:lockType']).toBe(undefined);
}));
});

View File

@ -1,96 +0,0 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2020 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 <http://www.gnu.org/licenses/>.
*/
import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core';
import { NodeEntry, SharedLinkEntry } from '@alfresco/js-api';
import { AlfrescoApiService } from '@alfresco/adf-core';
import { isLocked } from '@alfresco/aca-shared';
@Directive({
selector: '[acaLockNode]',
exportAs: 'lockNode'
})
export class LockNodeDirective {
@Input('acaLockNode')
node: NodeEntry = null;
@Output() toggle: EventEmitter<any> = new EventEmitter();
@Output() lockError: EventEmitter<any> = new EventEmitter();
@Output() unlockError: EventEmitter<any> = new EventEmitter();
@HostListener('click')
onClick() {
this.toggleLock(this.node);
}
constructor(private alfrescoApiService: AlfrescoApiService) {}
isNodeLocked(): boolean {
return !!(this.node && isLocked(this.node));
}
private async toggleLock(node: NodeEntry | SharedLinkEntry) {
const id = (node as SharedLinkEntry).entry.nodeId || node.entry.id;
if (isLocked(this.node)) {
try {
const response = await this.unlockNode(id);
this.update(response.entry);
this.toggle.emit(false);
} catch (error) {
this.unlockError.emit(error);
}
} else {
try {
const response = await this.lockNode(id);
this.update(response.entry);
this.toggle.emit(true);
} catch (error) {
this.lockError.emit(error);
}
}
}
private lockNode(nodeId: string) {
return this.alfrescoApiService.nodesApi.lockNode(nodeId, {
type: 'ALLOW_OWNER_CHANGES',
lifetime: 'PERSISTENT'
});
}
private unlockNode(nodeId: string) {
return this.alfrescoApiService.nodesApi.unlockNode(nodeId);
}
private update(data) {
const properties = this.node.entry.properties || {};
properties['cm:lockLifetime'] = data.properties['cm:lockLifetime'];
properties['cm:lockOwner'] = data.properties['cm:lockOwner'];
properties['cm:lockType'] = data.properties['cm:lockType'];
}
}