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

UILD-452: fix for "Creator of Work"/"Other contributors" fields are not shown for the resource #67

Merged
merged 2 commits into from
Jan 7, 2025
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
1 change: 1 addition & 0 deletions src/common/constants/bibframe.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ export const BF_URI_DELIMITER = '/';
export const ENTRY_DELIMITER = '__';
export const ENTRY_COUNT_DELIMITER = '::';
export const ENTRY_CONTROL_DELIMITER = '--';
export const TWIN_CHILDREN_KEY_DELIMITER = '$$';
9 changes: 9 additions & 0 deletions src/common/helpers/schema.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ENTRY_COUNT_DELIMITER,
ENTRY_DELIMITER,
PREV_ENTRY_PATH_INDEX,
TWIN_CHILDREN_KEY_DELIMITER,
} from '@common/constants/bibframe.constants';

export const getLookupLabelKey = (uriBFLite?: string) => {
Expand Down Expand Up @@ -205,3 +206,11 @@ export const getHtmlIdForEntry = ({ path = [] }: Partial<SchemaEntry>, schema: S

export const getHtmlIdForSchemaControl = (controlType: SchemaControlType, htmlId = '') =>
`${htmlId}${ENTRY_CONTROL_DELIMITER}${controlType}`;

export const generateTwinChildrenKey = (entry: SchemaEntry) => {
const { uri, constraints } = entry;
const { valueDataType } = constraints ?? {};
const suffix = valueDataType?.dataTypeURI ? `${TWIN_CHILDREN_KEY_DELIMITER}${valueDataType?.dataTypeURI}` : '';

return `${uri}${suffix}`;
};
37 changes: 22 additions & 15 deletions src/common/services/schema/schemaWithDuplicates.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { cloneDeep } from 'lodash';
import { v4 as uuidv4 } from 'uuid';
import { ISelectedEntries } from '../selectedEntries/selectedEntries.interface';
import { getParentEntryUuid, getUdpatedAssociatedEntries } from '@common/helpers/schema.helper';
import {
generateTwinChildrenKey,
getParentEntryUuid,
getUdpatedAssociatedEntries,
} from '@common/helpers/schema.helper';
import { generateEmptyValueUuid } from '@common/helpers/complexLookup.helper';
import { IEntryPropertiesGeneratorService } from './entryPropertiesGenerator.interface';
import { MIN_AMT_OF_SIBLING_ENTRIES_TO_BE_DELETABLE } from '@common/constants/bibframe.constants';
Expand All @@ -24,7 +28,7 @@ export class SchemaWithDuplicatesService implements ISchemaWithDuplicatesService
}

duplicateEntry(entry: SchemaEntry) {
const { uuid, path, children, constraints, uri = '' } = entry;
const { path, children, constraints } = entry;

if (!constraints?.repeatable) return;

Expand All @@ -36,29 +40,30 @@ export class SchemaWithDuplicatesService implements ISchemaWithDuplicatesService
const parentEntry = this.schema.get(parentEntryUuid);
const updatedParentEntry = this.getUpdatedParentEntry({
parentEntry,
originalEntryUuid: uuid,
originalEntry: entry,
updatedEntryUuid: updatedEntryUuid,
childEntryId: uri,
});

if (updatedParentEntry) {
this.schema.set(parentEntryUuid, updatedParentEntry);
this.schema.set(updatedEntryUuid, updatedEntry);
const twinChildrenKey = generateTwinChildrenKey(entry);

this.updateDeletabilityAndPositioning(updatedParentEntry?.twinChildren?.[uri]);
this.updateDeletabilityAndPositioning(updatedParentEntry?.twinChildren?.[twinChildrenKey]);
this.entryPropertiesGeneratorService?.applyHtmlIdToEntries(this.schema);
}

return updatedEntryUuid;
}

deleteEntry(entry: SchemaEntry) {
const { deletable, uuid, path, uri = '' } = entry;
const { deletable, uuid, path } = entry;

if (!deletable) return;

const parent = this.schema.get(getParentEntryUuid(path));
const twinSiblings = parent?.twinChildren?.[uri];
const twinChildrenKey = generateTwinChildrenKey(entry);
const twinSiblings = parent?.twinChildren?.[twinChildrenKey];

if (twinSiblings) {
const updatedTwinSiblings = twinSiblings?.filter(twinUuid => twinUuid !== uuid);
Expand All @@ -67,7 +72,7 @@ export class SchemaWithDuplicatesService implements ISchemaWithDuplicatesService
...parent,
twinChildren: {
...parent.twinChildren,
[uri]: updatedTwinSiblings,
[twinChildrenKey]: updatedTwinSiblings,
},
children: parent.children?.filter(child => child !== uuid),
});
Expand Down Expand Up @@ -168,28 +173,30 @@ export class SchemaWithDuplicatesService implements ISchemaWithDuplicatesService

private getUpdatedParentEntry({
parentEntry,
originalEntryUuid,
originalEntry,
updatedEntryUuid,
childEntryId,
}: {
parentEntry?: SchemaEntry;
originalEntryUuid: string;
originalEntry: SchemaEntry;
updatedEntryUuid: string;
childEntryId?: string;
}) {
if (!parentEntry) return;

const updatedParentEntry = cloneDeep(parentEntry);
const { children } = updatedParentEntry;
const originalEntryIndex = children?.indexOf(originalEntryUuid);
const originalEntryIndex = children?.indexOf(originalEntry.uuid);
const { uri } = originalEntry;
const childEntryId = uri ?? '';

if (childEntryId) {
const twinChildrenKey = generateTwinChildrenKey(originalEntry);

if (!updatedParentEntry.twinChildren) {
updatedParentEntry.twinChildren = {};
}

updatedParentEntry.twinChildren[childEntryId] = [
...new Set([...(updatedParentEntry.twinChildren[childEntryId] ?? []), originalEntryUuid, updatedEntryUuid]),
updatedParentEntry.twinChildren[twinChildrenKey] = [
...new Set([...(updatedParentEntry.twinChildren[twinChildrenKey] ?? []), originalEntry.uuid, updatedEntryUuid]),
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/components/Fields/Fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FC, memo, ReactElement, ReactNode } from 'react';
import { AdvancedFieldType } from '@common/constants/uiControls.constants';
import { IDrawComponent } from '@components/EditSection';
import { ENTITY_LEVEL } from '@common/constants/bibframe.constants';
import { generateTwinChildrenKey } from '@common/helpers/schema.helper';
import { DuplicateGroupContainer } from '@components/DuplicateGroupContainer';
import { ConditionalWrapper } from '@components/ConditionalWrapper';
import { DuplicateSubcomponentContainer } from '@components/DuplicateSubcomponentContainer';
Expand Down Expand Up @@ -81,8 +82,8 @@ export const Fields: FC<IFields> = memo(

if (!entry) return;

const { uri = '' } = entry;
const twinChildrenOfSameType = twinChildren?.[uri];
const twinChildrenKey = generateTwinChildrenKey(entry);
const twinChildrenOfSameType = twinChildren?.[twinChildrenKey];
const isFirstTwinChild = twinChildrenOfSameType?.[0] === uuid;

// render cloned / grouped items starting from the main item (prototype) separately
Expand Down
52 changes: 52 additions & 0 deletions src/test/__tests__/common/helpers/schema.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as SchemaHelper from '@common/helpers/schema.helper';
import * as BibframeMappingConstants from '@common/constants/bibframeMapping.constants';
import { AdvancedFieldType } from '@common/constants/uiControls.constants';
import { getMockedImportedConstant } from '@src/test/__mocks__/common/constants/constants.mock';
import { generateTwinChildrenKey } from '@common/helpers/schema.helper';

describe('schema.helper', () => {
const {
Expand Down Expand Up @@ -304,4 +305,55 @@ describe('schema.helper', () => {
expect(htmlId).toEqual('mockBfid::0__mockUri::0__mockUriBFLite::0');
});
});

describe('generateTwinChildrenKey', () => {
test('returns just URI when no valueDataType exists', () => {
const entry = {
uri: 'test:uri',
constraints: {},
} as SchemaEntry;

const result = generateTwinChildrenKey(entry);

expect(result).toBe('test:uri');
});

test('returns concatenated string with valueDataType when present', () => {
const entry = {
uri: 'test:uri',
constraints: {
valueDataType: {
dataTypeURI: 'test:dataType',
},
},
} as SchemaEntry;

const result = generateTwinChildrenKey(entry);

expect(result).toBe('test:uri$$test:dataType');
});

test('returns URI when constraints is undefined', () => {
const entry = {
uri: 'test:uri',
} as SchemaEntry;

const result = generateTwinChildrenKey(entry);

expect(result).toBe('test:uri');
});

test('returns URI when valueDataType is empty object', () => {
const entry = {
uri: 'test:uri',
constraints: {
valueDataType: {},
},
} as SchemaEntry;

const result = generateTwinChildrenKey(entry);

expect(result).toBe('test:uri');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,37 @@ describe('SchemaWithDuplicatesService', () => {

expect(schemaWithDuplicatesService.get()).toEqual(schema);
});

test('updates parent twinChildren when deleting entry', () => {
const parentEntry = {
uuid: 'testKey-1',
path: ['testKey-1'],
children: ['testKey-2', 'testKey-3'],
twinChildren: {
mockUri: ['testKey-2', 'testKey-3'],
},
};

const childEntry = {
uri: 'mockUri',
uuid: 'testKey-2',
path: ['testKey-1', 'testKey-2'],
deletable: true,
};

const testSchema = new Map([
['testKey-1', parentEntry],
['testKey-2', { ...childEntry, children: [], twinChildren: { mockUri: [] } }],
['testKey-3', { ...childEntry, uuid: 'testKey-3', children: [], twinChildren: { mockUri: [] } }],
]);

initServices(testSchema);

schemaWithDuplicatesService.deleteEntry(childEntry);

const updatedParent = schemaWithDuplicatesService.get().get('testKey-1');
expect(updatedParent?.twinChildren?.['mockUri']).toEqual(['testKey-3']);
expect(updatedParent?.children).toEqual(['testKey-3']);
});
});
});
Loading