From dfc636441cc5731d2f327e2181ee954297ca8163 Mon Sep 17 00:00:00 2001 From: Silviu Popa Date: Wed, 12 May 2021 19:17:41 +0300 Subject: [PATCH] [ACA-4325] - disable upload button on upload new file version begin (#7017) * [ACA-4325] - disable upload button on upload version begin * Update upload-base.spec.ts * disable upload button only on version upload component * PR changes * change to started Co-authored-by: Eugenio Romano --- .../version-upload.component.spec.ts | 96 +++++++++++++++++++ .../version-upload.component.ts | 35 ++++++- 2 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 lib/content-services/src/lib/version-manager/version-upload.component.spec.ts diff --git a/lib/content-services/src/lib/version-manager/version-upload.component.spec.ts b/lib/content-services/src/lib/version-manager/version-upload.component.spec.ts new file mode 100644 index 0000000000..7d22f28be1 --- /dev/null +++ b/lib/content-services/src/lib/version-manager/version-upload.component.spec.ts @@ -0,0 +1,96 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; +import { TranslateModule } from '@ngx-translate/core'; +import { VersionUploadComponent } from 'content-services'; +import { ContentService, setupTestBed, UploadService } from '@alfresco/adf-core'; +import { ContentTestingModule } from '../testing/content.testing.module'; +import { Node } from '@alfresco/js-api'; + +describe('VersionUploadComponent', () => { + let component: VersionUploadComponent; + let fixture: ComponentFixture; + let uploadService: UploadService; + let contentService: ContentService; + + const node: Node = new Node({ + id: '1234', + name: 'TEST-NODE', + isFile: true, + nodeType: 'FAKE', + isFolder: false, + modifiedAt: new Date(), + modifiedByUser: null, + createdAt: new Date(), + createdByUser: null, + content: { + mimeType: 'text/html', + mimeTypeName: 'HTML', + sizeInBytes: 13 + } + }); + + setupTestBed({ + imports: [ + TranslateModule.forRoot(), + ContentTestingModule + ], + providers: [UploadService], + schemas: [CUSTOM_ELEMENTS_SCHEMA] + }); + + beforeEach(() => { + fixture = TestBed.createComponent(VersionUploadComponent); + component = fixture.componentInstance; + uploadService = TestBed.inject(UploadService); + contentService = TestBed.inject(ContentService); + spyOn(contentService, 'hasAllowableOperations').and.returnValue(true); + component.node = node; + }); + + it('should disabled upload button on upload starts', fakeAsync(() => { + component.uploadStarted.subscribe(() => { + expect(component.disabled).toEqual(true); + }); + uploadService.fileUploadStarting.next(); + tick(500); + fixture.detectChanges(); + })); + + it('should enable upload button on error', (done) => { + spyOn(component, 'canUpload').and.returnValue(true); + component.error.subscribe(() => { + expect(component.disabled).toEqual(false); + done(); + }); + component.onError(true); + fixture.detectChanges(); + }); + + it('should enable upload button on success', (done) => { + spyOn(component, 'canUpload').and.returnValue(true); + component.success.subscribe(() => { + expect(component.disabled).toEqual(false); + done(); + }); + component.onSuccess(true); + fixture.detectChanges(); + }); + +}); diff --git a/lib/content-services/src/lib/version-manager/version-upload.component.ts b/lib/content-services/src/lib/version-manager/version-upload.component.ts index 2d17cbafae..e587e2eef9 100644 --- a/lib/content-services/src/lib/version-manager/version-upload.component.ts +++ b/lib/content-services/src/lib/version-manager/version-upload.component.ts @@ -15,9 +15,11 @@ * limitations under the License. */ -import { Component, Input, ViewEncapsulation, Output, EventEmitter } from '@angular/core'; +import { Component, Input, ViewEncapsulation, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core'; import { Node } from '@alfresco/js-api'; -import { ContentService } from '@alfresco/adf-core'; +import { ContentService, FileUploadEvent, UploadService } from '@alfresco/adf-core'; +import { Subject } from 'rxjs'; +import { takeUntil } from 'rxjs/operators'; @Component({ selector: 'adf-version-upload', @@ -26,11 +28,13 @@ import { ContentService } from '@alfresco/adf-core'; encapsulation: ViewEncapsulation.None, host: { 'class': 'adf-version-upload' } }) -export class VersionUploadComponent { +export class VersionUploadComponent implements OnInit, OnDestroy { semanticVersion: string = 'minor'; comment: string; uploadVersion: boolean = false; + disabled: boolean = false; + onDestroy$ = new Subject(); /** The target node. */ @Input() @@ -68,11 +72,24 @@ export class VersionUploadComponent { @Output() commentChanged = new EventEmitter(); - constructor(private contentService: ContentService) { + /** Emitted when the upload starts */ + @Output() + uploadStarted = new EventEmitter(); + + constructor(private contentService: ContentService, private uploadService: UploadService) { + } + + ngOnInit() { + this.uploadService.fileUploadStarting + .pipe(takeUntil(this.onDestroy$)) + .subscribe((event: FileUploadEvent) => { + this.disabled = true; + this.uploadStarted.emit(event); + }); } canUpload(): boolean { - return this.contentService.hasAllowableOperations(this.node, 'update'); + return this.contentService.hasAllowableOperations(this.node, 'update') && !this.disabled; } isMajorVersion(): boolean { @@ -80,6 +97,7 @@ export class VersionUploadComponent { } cancelUpload() { + this.disabled = false; this.cancel.emit(); } @@ -92,11 +110,18 @@ export class VersionUploadComponent { } onSuccess(event: any) { + this.disabled = false; this.success.emit(event); } onError(event: any) { + this.disabled = false; this.error.emit(event); } + ngOnDestroy() { + this.onDestroy$.next(); + this.onDestroy$.complete(); + } + }