Skip to content

Commit 597dd72

Browse files
committed
refactor: enhance lookup functionality by adding parentGroupUri support and cleaning up related code
1 parent 91d8232 commit 597dd72

7 files changed

Lines changed: 42 additions & 20 deletions

File tree

src/common/constants/bibframeMapping.constants.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,14 @@ export const SIMPLE_LOOKUP_MAPPING = {
482482

483483
export const BFLITE_TYPES_MAP = {
484484
_notes: {
485-
field: { uri: 'http://id.loc.gov/ontologies/bibframe/noteType' },
486-
data: SIMPLE_LOOKUP_MAPPING._notes,
485+
field: '',
486+
data: {},
487+
fields: {
488+
type: {
489+
field: { uri: 'type' },
490+
data: SIMPLE_LOOKUP_MAPPING._notes,
491+
},
492+
},
487493
},
488494
_creatorReference: {
489495
field: '',

src/common/helpers/lookupOptions.helper.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ export const formatLookupOptions = (
4040
};
4141
});
4242

43-
export const getBFGroup = (typeMap: FieldTypeMap, propertyURI: string) =>
44-
Object.values(typeMap).find(({ field }) => (field as { uri: string }).uri === propertyURI);
43+
export const getBFGroup = (typeMap: FieldTypeMap, propertyURI: string, parentGroupUri?: string) => {
44+
return (
45+
Object.values(typeMap).find(({ field }) => (field as { uri: string }).uri === propertyURI) ??
46+
(parentGroupUri ? typeMap[parentGroupUri]?.fields?.[propertyURI] : undefined)
47+
);
48+
};
4549

46-
export const filterLookupOptionsByMappedValue = (lookupData: MultiselectOption[], propertyURI?: string) => {
50+
export const filterLookupOptionsByMappedValue = (
51+
lookupData: MultiselectOption[],
52+
propertyURI?: string,
53+
parentGroupUri?: string,
54+
) => {
4755
if (!propertyURI) return lookupData;
4856

4957
let filteredLookupData = lookupData;
5058
const typesMap = BFLITE_TYPES_MAP;
51-
const bfGroup = getBFGroup(typesMap as FieldTypeMap, propertyURI);
59+
const bfGroup = getBFGroup(typesMap as FieldTypeMap, propertyURI, parentGroupUri);
5260

5361
if (bfGroup) {
5462
const bf20Uris = Object.values(bfGroup.data).map(({ uri }) => uri);
@@ -63,14 +71,15 @@ export const filterLookupOptionsByParentBlock = (
6371
lookupData?: MultiselectOption[] | Nullish,
6472
propertyURI?: string,
6573
parentBlockUri?: string,
74+
parentGroupUri?: string,
6675
) => {
6776
if (!lookupData) return;
6877

6978
if (!parentBlockUri || !propertyURI) return lookupData;
7079

7180
let filteredLookupData = lookupData;
7281
const typesMap = BFLITE_TYPES_MAP;
73-
const bfGroup = getBFGroup(typesMap as FieldTypeMap, propertyURI);
82+
const bfGroup = getBFGroup(typesMap as FieldTypeMap, propertyURI, parentGroupUri);
7483

7584
if (bfGroup) {
7685
const bf20MappedData = Object.values(bfGroup.data);

src/common/hooks/useSimpleLookupData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ export const useSimpleLookupData = () => {
88

99
const getLookupData = lookupCacheService.getAll;
1010

11-
const loadLookupData = async (uri: string, propertyURI?: string) => {
11+
const loadLookupData = async (uri: string, propertyURI?: string, parentGroupUri?: string) => {
1212
try {
1313
const response = await loadSimpleLookup(uri);
1414

1515
if (!response) return null;
1616

1717
const formattedLookupData = formatLookupOptions(response, uri);
18-
const filteredLookupData = filterLookupOptionsByMappedValue(formattedLookupData, propertyURI);
18+
const filteredLookupData = filterLookupOptionsByMappedValue(formattedLookupData, propertyURI, parentGroupUri);
1919
const sortedLookupData = filteredLookupData?.toSorted(alphabeticSortLabel);
2020

2121
lookupCacheService.save?.(uri, sortedLookupData);

src/common/services/recordToSchemaMapping/recordToSchemaMapping.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ export class RecordToSchemaMappingService implements IRecordToSchemaMapping {
407407
schemaUiElem: SchemaEntry;
408408
id?: string;
409409
}) {
410-
const { type, constraints, uri } = schemaUiElem;
410+
const { type, constraints, uriBFLite } = schemaUiElem;
411411
const partialTemplateMatch = this.templateMetadata?.filter(({ path }) => path.at(-1) === fieldUri);
412412
let updatedData = data;
413413

@@ -427,7 +427,7 @@ export class RecordToSchemaMappingService implements IRecordToSchemaMapping {
427427
data: updatedData,
428428
uri: constraints?.useValuesFrom?.[0],
429429
uriSelector: BFLITE_URIS.LINK,
430-
propertyUri: uri,
430+
propertyUri: uriBFLite,
431431
blockUri: this.currentBlockUri,
432432
groupUri: this.currentRecordGroupKey,
433433
fieldUri,

src/common/services/userValues/userValueTypes/simpleLookup.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { BFLITE_TYPES_MAP, DEFAULT_GROUP_VALUES, BFLITE_URIS } from '@common/con
66

77
export class SimpleLookupUserValueService extends UserValueType implements IUserValueType {
88
private uri?: string;
9+
private groupUri?: string;
910
private propertyUri?: string;
1011
private loadedData?: MultiselectOption[];
1112
private contents?: UserValueContents[];
@@ -19,6 +20,7 @@ export class SimpleLookupUserValueService extends UserValueType implements IUser
1920

2021
async generate({ data, uri, uuid, uriSelector, type, propertyUri, groupUri, fieldUri }: UserValueDTO) {
2122
this.uri = uri;
23+
this.groupUri = groupUri;
2224
this.propertyUri = propertyUri;
2325
const cachedData = this.getCachedData();
2426

@@ -110,11 +112,6 @@ export class SimpleLookupUserValueService extends UserValueType implements IUser
110112
type?: AdvancedFieldType;
111113
fieldUri?: string;
112114
}) {
113-
console.log('====================================');
114-
console.log('BFLITE_TYPES_MAP', BFLITE_TYPES_MAP);
115-
console.log('groupUri', groupUri);
116-
console.log('====================================');
117-
118115
const typesMap = (BFLITE_TYPES_MAP as FieldTypeMap)[groupUri as string];
119116
const mappedUri =
120117
typesMap && itemUri
@@ -162,7 +159,7 @@ export class SimpleLookupUserValueService extends UserValueType implements IUser
162159
if (!response) return;
163160

164161
const formattedLookupData = formatLookupOptions(response, this.uri);
165-
const filteredLookupData = filterLookupOptionsByMappedValue(formattedLookupData, this.propertyUri);
162+
const filteredLookupData = filterLookupOptionsByMappedValue(formattedLookupData, this.propertyUri, this.groupUri);
166163

167164
this.loadedData = filteredLookupData?.toSorted(alphabeticSortLabel);
168165

src/components/EditSection/DrawComponent.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ export const DrawComponent: FC<IDrawComponent & EditSectionDataProps> = ({
120120
value: AdvancedFieldType.block,
121121
});
122122

123+
const groupEntry = findParentEntryByProperty({
124+
schema,
125+
path: entry.path,
126+
key: 'type',
127+
value: AdvancedFieldType.group,
128+
});
129+
123130
return (
124131
<FieldWithMetadataAndControls entry={entry} level={level} isCompact={isCompact}>
125132
<SimpleLookupField
@@ -130,8 +137,9 @@ export const DrawComponent: FC<IDrawComponent & EditSectionDataProps> = ({
130137
parentUri={constraints?.valueDataType?.dataTypeURI}
131138
value={selectedUserValue?.contents}
132139
isDisabled={isDisabled}
133-
propertyUri={entry.uri}
140+
propertyUri={entry.uriBFLite}
134141
parentBlockUri={blockEntry?.uriBFLite}
142+
parentGroupUri={groupEntry?.uriBFLite}
135143
/>
136144
</FieldWithMetadataAndControls>
137145
);

src/components/SimpleLookupField/SimpleLookupField.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface Props {
2626
onChange: (uuid: string, contents: Array<UserValueContents>) => void;
2727
propertyUri?: string;
2828
parentBlockUri?: string;
29+
parentGroupUri?: string;
2930
}
3031

3132
const LoadingMessage: FC = () => <FormattedMessage id="ld.loading" />;
@@ -38,13 +39,14 @@ export const SimpleLookupField: FC<Props> = ({
3839
htmlId,
3940
onChange,
4041
parentUri,
42+
parentGroupUri,
4143
isDisabled = false,
4244
propertyUri,
4345
parentBlockUri,
4446
}) => {
4547
const { getLookupData, loadLookupData } = useSimpleLookupData();
4648
const loadedOptions = getLookupData()?.[uri] || [];
47-
const options = filterLookupOptionsByParentBlock(loadedOptions, propertyUri, parentBlockUri);
49+
const options = filterLookupOptionsByParentBlock(loadedOptions, propertyUri, parentBlockUri, parentGroupUri);
4850
const { addStatusMessagesItem } = useStatusState();
4951
const { simpleLookupRef, forceDisplayOptionsAtTheTop } = useSimpleLookupObserver();
5052

@@ -63,7 +65,7 @@ export const SimpleLookupField: FC<Props> = ({
6365
setIsLoading(true);
6466

6567
try {
66-
await loadLookupData(uri, propertyUri);
68+
await loadLookupData(uri, propertyUri, parentGroupUri);
6769
} catch (error) {
6870
console.error('Cannot load data for the Lookup:', error);
6971

0 commit comments

Comments
 (0)