Skip to content

Commit

Permalink
[AAE-15839] Fix file object input in viewers (#8804)
Browse files Browse the repository at this point in the history
* [AAE-15839] Fix file object input in viewers

* [AAE-15839] Attend review comments

* [AAE-15839] Remove duplication

* [AAE-15839] Fix package-lock.json

* [AAE-15839] Fix package.json

* [AAE-15839] Remove duplication

* [AAE-15839] Remove duplication
  • Loading branch information
pmartinezga committed Aug 9, 2023
1 parent 1f96c34 commit 04803c1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,27 @@ describe('BaseViewerWidgetComponent', () => {
});

it('should set the file id corretly when the field value is an array', (done) => {
const fakeField = new FormFieldModel(fakeForm, { id: 'fakeField', value: [fakePngAnswer] });
widget.field = fakeField;

fixture.detectChanges();
assertFileId([fakePngAnswer], '1933', fakeForm, widget, fixture, done);
});

fixture.whenStable().then(() => {
expect(widget.field.value).toBe('1933');
done();
});
it('should set the file id corretly when the field value is an object', (done) => {
assertFileId(fakePngAnswer, '1933', fakeForm, widget, fixture, done);
});

it('should set the file id corretly when the field value is a string', (done) => {
const fakeField = new FormFieldModel(fakeForm, { id: 'fakeField', value: 'fakeValue' });
widget.field = fakeField;
assertFileId('fakeValue', 'fakeValue', fakeForm, widget, fixture, done);
});
});

fixture.detectChanges();
function assertFileId(value: any, expectedFileId: string, fakeForm: FormModel, widget: BaseViewerWidgetComponent, fixture: ComponentFixture<BaseViewerWidgetComponent>, done: DoneFn) {
const fakeField = new FormFieldModel(fakeForm, { id: 'fakeField', value });
widget.field = fakeField;

fixture.whenStable().then(() => {
expect(widget.field.value).toBe('fakeValue');
done();
});
fixture.detectChanges();

fixture.whenStable().then(() => {
expect(widget.field.value).toBe(expectedFileId);
done();
});
});
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { FormService } from '../../../services/form.service';
import { WidgetComponent } from '../widget.component';

/* eslint-disable @angular-eslint/component-selector */
/* eslint-disable @angular-eslint/component-selector */

@Component({
selector: 'base-viewer-widget',
Expand All @@ -44,12 +44,13 @@ export class BaseViewerWidgetComponent extends WidgetComponent implements OnInit
}

ngOnInit(): void {
if (this.field &&
this.field.value &&
Array.isArray(this.field.value) &&
this.field.value.length) {
const file = this.field.value[0];
this.field.value = file.id;
if (this.field?.value) {
if (Array.isArray(this.field.value) && this.field.value.length > 0) {
const file = this.field.value[0];
this.field.value = file.id;
} else if (typeof this.field.value === 'object' && this.field.value.id) {
this.field.value = this.field.value.id;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* limitations under the License.
*/

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { WidgetComponent, FormService } from '@alfresco/adf-core';
import { Component, ViewEncapsulation } from '@angular/core';
import { FormService, BaseViewerWidgetComponent } from '@alfresco/adf-core';

/* eslint-disable @angular-eslint/component-selector */
/* eslint-disable @angular-eslint/component-selector */

@Component({
selector: 'file-viewer-widget',
Expand All @@ -37,18 +37,8 @@ import { WidgetComponent, FormService } from '@alfresco/adf-core';
},
encapsulation: ViewEncapsulation.None
})
export class FileViewerWidgetComponent extends WidgetComponent implements OnInit {
export class FileViewerWidgetComponent extends BaseViewerWidgetComponent {
constructor(formService: FormService) {
super(formService);
}

ngOnInit(): void {
if (this.field &&
this.field.value &&
Array.isArray(this.field.value) &&
this.field.value.length) {
const file = this.field.value[0];
this.field.value = file.id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*/

import { Component, EventEmitter, OnInit, Output, ViewEncapsulation } from '@angular/core';
import { FormService, WidgetComponent } from '@alfresco/adf-core';
import { Component, EventEmitter, Output, ViewEncapsulation } from '@angular/core';
import { BaseViewerWidgetComponent, FormService } from '@alfresco/adf-core';
import { Node } from '@alfresco/js-api';

/* eslint-disable @angular-eslint/component-selector */
Expand All @@ -38,7 +38,7 @@ import { Node } from '@alfresco/js-api';
},
encapsulation: ViewEncapsulation.None
})
export class PropertiesViewerWidgetComponent extends WidgetComponent implements OnInit {
export class PropertiesViewerWidgetComponent extends BaseViewerWidgetComponent {

@Output()
nodeContentLoaded: EventEmitter<Node> = new EventEmitter();
Expand All @@ -47,16 +47,6 @@ export class PropertiesViewerWidgetComponent extends WidgetComponent implements
super(formService);
}

ngOnInit(): void {
if (this.field &&
this.field.value &&
Array.isArray(this.field.value) &&
this.field.value.length) {
const file = this.field.value[0];
this.field.value = file.id;
}
}

onNodeContentLoaded(node: Node) {
this.nodeContentLoaded.emit(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* limitations under the License.
*/

import { FormService, WidgetComponent } from '@alfresco/adf-core';
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { BaseViewerWidgetComponent, FormService } from '@alfresco/adf-core';
import { Component, ViewEncapsulation } from '@angular/core';

/* eslint-disable @angular-eslint/component-selector */
/* eslint-disable @angular-eslint/component-selector */

@Component({
selector: 'file-viewer-widget',
Expand All @@ -37,18 +37,8 @@ import { Component, OnInit, ViewEncapsulation } from '@angular/core';
},
encapsulation: ViewEncapsulation.None
})
export class FileViewerWidgetComponent extends WidgetComponent implements OnInit {
export class FileViewerWidgetComponent extends BaseViewerWidgetComponent {
constructor(formService: FormService) {
super(formService);
}

ngOnInit(): void {
if (this.field &&
this.field.value &&
Array.isArray(this.field.value) &&
this.field.value.length) {
const file = this.field.value[0];
this.field.value = file.id;
}
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,3 @@
"module": "./index.js",
"typings": "./index.d.ts"
}

0 comments on commit 04803c1

Please sign in to comment.