Refactored UploadSectionFileViewComponent using MetadataMap

This commit is contained in:
Giuseppe Digilio
2019-02-22 12:23:41 +01:00
parent 6911d6f6ad
commit e708e3bb97
3 changed files with 33 additions and 24 deletions

View File

@@ -1,22 +1,26 @@
<div>
<ng-container *ngIf="metadata.length > 0">
<ng-container *ngFor="let entry of metadata">
<ng-container *ngIf="metadata">
<ng-container *ngFor="let entry of getAllMetadataValue(fileTitleKey)">
<ng-container *ngIf="entry.value !== ''">
<h5 *ngIf="metadata.indexOf(entry) === 0">
<h5>
{{entry.value}}
</h5>
<ng-container *ngIf="metadata.indexOf(entry) !== 0">
{{entry.value | dsTruncate:[150]}}
</ng-container>
</ng-container>
<ng-container *ngIf="entry.value === ''">
<h5 *ngIf="metadata.indexOf(entry) === 0">
<span class="text-muted">{{'submission.sections.upload.no-entry' | translate}} {{entry.key}}</span>
<h5 *ngIf="metadata[fileTitleKey].indexOf(entry) === 0">
<span class="text-muted">{{'submission.sections.upload.no-entry' | translate}} {{fileTitleKey}}</span>
</h5>
<ng-container *ngIf="metadata.indexOf(entry) !== 0">
<span class="text-muted">{{'submission.sections.upload.no-entry' | translate}} {{entry.key}}</span>
</ng-container>
</ng-container>
<span class="clearfix"></span>
</ng-container>
<ng-container *ngFor="let entry of getAllMetadataValue(fileDescrKey)">
<ng-container *ngIf="entry.value !== ''">
{{entry.value | dsTruncate:[150]}}
</ng-container>
<ng-container *ngIf="entry.value === ''">
<span *ngIf="metadata[fileDescrKey].indexOf(entry) === 0" class="text-muted">{{'submission.sections.upload.no-entry' | translate}} {{fileDescrKey}}</span>
</ng-container>
<span class="clearfix"></span>
</ng-container>
</ng-container>

View File

@@ -8,6 +8,7 @@ import { mockUploadFiles } from '../../../../../shared/mocks/mock-submission';
import { FormComponent } from '../../../../../shared/form/form.component';
import { UploadSectionFileViewComponent } from './file-view.component';
import { TruncatePipe } from '../../../../../shared/utils/truncate.pipe';
import { Metadata } from '../../../../../core/shared/metadata.model';
describe('UploadSectionFileViewComponent test suite', () => {
@@ -74,10 +75,14 @@ describe('UploadSectionFileViewComponent test suite', () => {
it('should init metadata array properly', () => {
comp.fileData = fileData;
const expectMetadataMap = {
[comp.fileTitleKey]: Metadata.all(fileData.metadata, 'dc.title'),
[comp.fileDescrKey]: [],
};
fixture.detectChanges();
expect(comp.metadata.length).toBe(2);
expect(comp.metadata).toEqual(expectMetadataMap);
});

View File

@@ -1,7 +1,9 @@
import { Component, Input, OnInit } from '@angular/core';
import { WorkspaceitemSectionUploadFileObject } from '../../../../../core/submission/models/workspaceitem-section-upload-file.model';
import { Metadatum } from '../../../../../core/shared/metadatum.model';
import { isNotEmpty } from '../../../../../shared/empty.util';
import { MetadataMap, MetadataValue } from '../../../../../core/shared/metadata.interfaces';
import { Metadata } from '../../../../../core/shared/metadata.model';
@Component({
selector: 'ds-submission-upload-section-file-view',
@@ -10,20 +12,18 @@ import { isNotEmpty } from '../../../../../shared/empty.util';
export class UploadSectionFileViewComponent implements OnInit {
@Input() fileData: WorkspaceitemSectionUploadFileObject;
public metadata: Metadatum[] = [];
public metadata: MetadataMap = Object.create({});
public fileTitleKey = 'Title';
public fileDescrKey = 'Description';
ngOnInit() {
if (isNotEmpty(this.fileData.metadata)) {
this.metadata.push({
key: 'Title',
language: (this.fileData.metadata.hasOwnProperty('dc.title') ? this.fileData.metadata['dc.title'][0].language : ''),
value: (this.fileData.metadata.hasOwnProperty('dc.title') ? this.fileData.metadata['dc.title'][0].value : '')
});
this.metadata.push({
key: 'Description',
language: (this.fileData.metadata.hasOwnProperty('dc.description') ? this.fileData.metadata['dc.description'][0].language : ''),
value: (this.fileData.metadata.hasOwnProperty('dc.description') ? this.fileData.metadata['dc.description'][0].value : '')
});
this.metadata[this.fileTitleKey] = Metadata.all(this.fileData.metadata, 'dc.title');
this.metadata[this.fileDescrKey] = Metadata.all(this.fileData.metadata, 'dc.description');
}
}
getAllMetadataValue(metadataKey): MetadataValue[] {
return Metadata.all(this.metadata, metadataKey);
}
}