Skip to content

Commit a571052

Browse files
authored
Remove null values to prevent search errors (#277)
* Remove null values from data to prevent search errors Signed-off-by: brookewp <[email protected]> * Remove duplicate comment Signed-off-by: brookewp <[email protected]> --------- Signed-off-by: brookewp <[email protected]>
1 parent 3d6c73d commit a571052

File tree

1 file changed

+21
-11
lines changed
  • src/blocks/remote-data-container/components/item-list

1 file changed

+21
-11
lines changed

src/blocks/remote-data-container/components/item-list/ItemList.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@ export function ItemList( props: ItemListProps ) {
2020

2121
const instanceId = useInstanceId( ItemList, blockName );
2222

23-
// ensure each result has an 'id' key
2423
const data = useMemo( () => {
25-
return ( results ?? [] ).map( ( item: Record< string, unknown > ) =>
26-
item.id
27-
? item
28-
: {
29-
...item,
30-
id: Object.keys( item ).find( key => /(^|_)(id)$/i.test( key ) ) // Regex to match 'id' or part of '_id'
31-
? item[ Object.keys( item ).find( key => /(^|_)(id)$/i.test( key ) ) as string ]
32-
: instanceId,
33-
}
34-
) as RemoteDataResult[];
24+
// remove null values from the data to prevent errors in filterSortAndPaginate
25+
const removeNullValues = ( obj: Record< string, unknown > ): Record< string, unknown > => {
26+
return Object.fromEntries(
27+
Object.entries( obj ).filter( ( [ _, value ] ) => value !== null )
28+
);
29+
};
30+
31+
return ( results ?? [] ).map( ( item: Record< string, unknown > ) => {
32+
const parsedItem = removeNullValues( item );
33+
34+
if ( parsedItem.id ) {
35+
return parsedItem;
36+
}
37+
38+
// ensure each result has an 'id' key
39+
const idKey = Object.keys( parsedItem ).find( key => /(^|_)(id)$/i.test( key ) );
40+
return {
41+
...parsedItem,
42+
id: idKey ? parsedItem[ idKey ] : instanceId,
43+
};
44+
} ) as RemoteDataResult[];
3545
}, [ results ] );
3646

3747
// get fields from results data to use as columns

0 commit comments

Comments
 (0)