Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACA-4325] - disable upload button on upload new file version begin #7017

Merged
merged 5 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<VersionUploadComponent>;
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();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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()
Expand Down Expand Up @@ -68,18 +72,32 @@ export class VersionUploadComponent {
@Output()
commentChanged = new EventEmitter<string>();

constructor(private contentService: ContentService) {
/** Emitted when the upload starts */
@Output()
uploadStarted = new EventEmitter<FileUploadEvent>();

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 {
return this.semanticVersion !== 'minor';
}

cancelUpload() {
this.disabled = false;
this.cancel.emit();
}

Expand All @@ -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();
}

}