Skip to content

Commit

Permalink
Fix articles block fail to get all inner blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Sep 10, 2024
1 parent ad718db commit e706ef9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/blocks/homepage-articles/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { addQueryArgs } from '@wordpress/url';
* Internal dependencies
*/
import metadata from './block.json';
import { getBlockQueries, sanitizePostList } from './utils';
import { getBlockQueries, sanitizePostList, recursivelyGetBlocks } from './utils';

const { name } = metadata;
export const STORE_NAMESPACE = `newspack-blocks/${ name }`;
Expand Down Expand Up @@ -138,15 +138,7 @@ const createFetchPostsSaga = blockNames => {

yield put( { type: 'DISABLE_UI' } );

// Ensure innerBlocks are populated for widget area blocks.
// See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216.
const blocks = getBlocks().map( block => {
const innerBlocks = select( 'core/block-editor' ).getBlocks( block.clientId );
return {
...block,
innerBlocks,
};
} );
const blocks = recursivelyGetBlocks( getBlocks );

const blockQueries = getBlockQueries( blocks, blockNames );

Expand Down
20 changes: 20 additions & 0 deletions src/blocks/homepage-articles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,23 @@ export const postsBlockDispatch = (
triggerReflow: isEditorBlock ? dispatch( STORE_NAMESPACE ).reflow : () => undefined,
};
};

// Ensure innerBlocks are populated for some blocks (e.g. `widget-area` and `post-content`).
// See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216.
// See https://github.com/Automattic/wp-calypso/issues/91839.
export const recursivelyGetBlocks = (
getBlocks: ( clientId?: string ) => Block[],
blocks: Block[] = getBlocks()
) => {
return blocks.map( ( block ) => {
let innerBlocks =
block.innerBlocks.length === 0
? getBlocks( block.clientId )
: block.innerBlocks;
innerBlocks = recursivelyGetBlocks( getBlocks, innerBlocks );
return {
...block,
innerBlocks,
};
});
};

0 comments on commit e706ef9

Please sign in to comment.