Skip to content

Commit

Permalink
Merge pull request DSpace#2252 from alexandrevryghem/issue-1671_fix-r…
Browse files Browse the repository at this point in the history
…elationships-not-updating-view-on-submission-form_contribute-main

Adding/removing relationships in the submission form does not update the view automatically
  • Loading branch information
tdonohue authored Jun 15, 2023
2 parents 12f9023 + c4b2565 commit 9102709
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/app/shared/form/form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@
(ngbEvent)="onCustomEvent($event)">
<ng-template modelType="ARRAY" let-group let-index="index" let-context="context">
<!--Array with repeatable items-->
<div *ngIf="(!context.notRepeatable) && !isVirtual(context, index)"
<div *ngIf="(!context.notRepeatable) && !isVirtual(context, index) && group.context.groups.length !== 1 && !isItemReadOnly(context, index)"
class="col-xs-2 d-flex flex-column justify-content-sm-start align-items-end">
<button type="button" class="btn btn-secondary" role="button"
title="{{'form.remove' | translate}}"
attr.aria-label="{{'form.remove' | translate}}"
(click)="removeItem($event, context, index)"
[disabled]="group.context.groups.length === 1 || isItemReadOnly(context, index)">
(click)="removeItem($event, context, index)">
<span><i class="fas fa-trash" aria-hidden="true"></i></span>
</button>
</div>
<div *ngIf="(!context.notRepeatable) && index === (group.context.groups.length - 1)" class="clearfix pl-4 w-100">
<div *ngIf="(!context.notRepeatable) && index === (group.context.groups.length - 1) && !isItemReadOnly(context, index)" class="clearfix pl-4 w-100">
<div class="btn-group" role="group">
<button type="button" role="button" class="ds-form-add-more btn btn-link"
title="{{'form.add' | translate}}"
attr.aria-label="{{'form.add' | translate}}"
[disabled]="isItemReadOnly(context, index)"
(click)="insertItem($event, group.context, group.context.groups.length)">
<span><i class="fas fa-plus"></i> {{'form.add' | translate}}</span>
</button>
Expand Down
40 changes: 38 additions & 2 deletions src/app/submission/sections/form/section-form.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ describe('SubmissionSectionFormComponent test suite', () => {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
compAsAny.formData = {};
compAsAny.sectionMetadata = ['dc.title'];
compAsAny.sectionData.data = {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
spyOn(compAsAny, 'inCurrentSubmissionScope').and.callThrough();

expect(comp.hasMetadataEnrichment(newSectionData)).toBeTruthy();
Expand All @@ -306,7 +308,9 @@ describe('SubmissionSectionFormComponent test suite', () => {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
compAsAny.formData = newSectionData;
compAsAny.sectionMetadata = ['dc.title'];
compAsAny.sectionData.data = {
'dc.title': [new FormFieldMetadataValueObject('test')]
};
spyOn(compAsAny, 'inCurrentSubmissionScope').and.callThrough();

expect(comp.hasMetadataEnrichment(newSectionData)).toBeFalsy();
Expand Down Expand Up @@ -343,6 +347,22 @@ describe('SubmissionSectionFormComponent test suite', () => {
} as FormFieldModel
]
},
{
fields: [
{
selectableMetadata: [{ metadata: 'scoped.workflow.relation' }],
scope: 'WORKFLOW',
} as FormFieldModel,
],
},
{
fields: [
{
selectableMetadata: [{ metadata: 'scoped.workspace.relation' }],
scope: 'WORKSPACE',
} as FormFieldModel,
],
},
{
fields: [
{
Expand Down Expand Up @@ -371,6 +391,14 @@ describe('SubmissionSectionFormComponent test suite', () => {
it('should return false for fields scoped to workflow', () => {
expect((comp as any).inCurrentSubmissionScope('scoped.workflow')).toBe(false);
});

it('should return true for relation fields scoped to workspace', () => {
expect((comp as any).inCurrentSubmissionScope('scoped.workspace.relation')).toBe(true);
});

it('should return false for relation fields scoped to workflow', () => {
expect((comp as any).inCurrentSubmissionScope('scoped.workflow.relation')).toBe(false);
});
});

describe('in workflow scope', () => {
Expand All @@ -390,6 +418,14 @@ describe('SubmissionSectionFormComponent test suite', () => {
it('should return false for fields scoped to workspace', () => {
expect((comp as any).inCurrentSubmissionScope('scoped.workspace')).toBe(false);
});

it('should return true for relation fields scoped to workflow', () => {
expect((comp as any).inCurrentSubmissionScope('scoped.workflow.relation')).toBe(true);
});

it('should return false for relation fields scoped to workspace', () => {
expect((comp as any).inCurrentSubmissionScope('scoped.workspace.relation')).toBe(false);
});
});
});

Expand Down
17 changes: 13 additions & 4 deletions src/app/submission/sections/form/section-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { WorkflowItem } from '../../../core/submission/models/workflowitem.model
import { SubmissionObject } from '../../../core/submission/models/submission-object.model';
import { SubmissionSectionObject } from '../../objects/submission-section-object.model';
import { SubmissionSectionError } from '../../objects/submission-section-error.model';
import { FormRowModel } from '../../../core/config/models/config-submission-form.model';

/**
* This component represents a section that contains a Form.
Expand Down Expand Up @@ -228,7 +229,9 @@ export class SubmissionSectionFormComponent extends SectionModelComponent {

const sectionDataToCheck = {};
Object.keys(sectionData).forEach((key) => {
if (this.sectionMetadata && this.sectionMetadata.includes(key) && this.inCurrentSubmissionScope(key)) {
// todo: removing Relationships works due to a bug -- dspace.entity.type is included in sectionData, which is what triggers the update;
// if we use this.sectionMetadata.includes(key), this field is filtered out and removed Relationships won't disappear from the form.
if (this.inCurrentSubmissionScope(key)) {
sectionDataToCheck[key] = sectionData[key];
}
});
Expand Down Expand Up @@ -256,9 +259,15 @@ export class SubmissionSectionFormComponent extends SectionModelComponent {
* @private
*/
private inCurrentSubmissionScope(field: string): boolean {
const scope = this.formConfig?.rows.find(row => {
return row.fields?.[0]?.selectableMetadata?.[0]?.metadata === field;
}).fields?.[0]?.scope;
const scope = this.formConfig?.rows.find((row: FormRowModel) => {
if (row.fields?.[0]?.selectableMetadata) {
return row.fields?.[0]?.selectableMetadata?.[0]?.metadata === field;
} else if (row.fields?.[0]?.selectableRelationship) {
return row.fields?.[0]?.selectableRelationship.relationshipType === field.replace(/^relationship\./g, '');
} else {
return false;
}
})?.fields?.[0]?.scope;

switch (scope) {
case SubmissionScopeType.WorkspaceItem: {
Expand Down

0 comments on commit 9102709

Please sign in to comment.