Skip to content

Commit

Permalink
Remove null values to prevent search errors (#277)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
brookewp authored Jan 7, 2025
1 parent 3d6c73d commit a571052
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/blocks/remote-data-container/components/item-list/ItemList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ export function ItemList( props: ItemListProps ) {

const instanceId = useInstanceId( ItemList, blockName );

// ensure each result has an 'id' key
const data = useMemo( () => {
return ( results ?? [] ).map( ( item: Record< string, unknown > ) =>
item.id
? item
: {
...item,
id: Object.keys( item ).find( key => /(^|_)(id)$/i.test( key ) ) // Regex to match 'id' or part of '_id'
? item[ Object.keys( item ).find( key => /(^|_)(id)$/i.test( key ) ) as string ]
: instanceId,
}
) as RemoteDataResult[];
// remove null values from the data to prevent errors in filterSortAndPaginate
const removeNullValues = ( obj: Record< string, unknown > ): Record< string, unknown > => {
return Object.fromEntries(
Object.entries( obj ).filter( ( [ _, value ] ) => value !== null )
);
};

return ( results ?? [] ).map( ( item: Record< string, unknown > ) => {
const parsedItem = removeNullValues( item );

if ( parsedItem.id ) {
return parsedItem;
}

// ensure each result has an 'id' key
const idKey = Object.keys( parsedItem ).find( key => /(^|_)(id)$/i.test( key ) );
return {
...parsedItem,
id: idKey ? parsedItem[ idKey ] : instanceId,
};
} ) as RemoteDataResult[];
}, [ results ] );

// get fields from results data to use as columns
Expand Down

0 comments on commit a571052

Please sign in to comment.