Skip to content

Commit

Permalink
Fix post_per_page and offset handling in preview
Browse files Browse the repository at this point in the history
  • Loading branch information
iansvo committed Nov 25, 2024
1 parent ec86fa4 commit 342e5ca
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/blocks/looper/components/LoopInnerBlocksRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,25 @@ export function LoopInnerBlocksRenderer( props ) {

const loopItemsContext = useMemo( () => {
if ( hasResolvedData && Array.isArray( data ) ) {
let perPage = query?.posts_per_page
? query?.posts_per_page
: 10;
let { posts_per_page: perPage = 10, offset = 0 } = query;

if ( '-1' === perPage?.toString() ) {
// Ensure the params are a valid integer for comparison.
perPage = parseInt( perPage, 10 );
offset = parseInt( offset, 10 );

console.log( { perPage, offset } );

if ( perPage < 0 ) {
perPage = data.length;
}

const items = data.slice( 0, perPage );
const items = data.slice(
offset > -1 ? offset : 0,
offset > -1 ? offset + perPage : perPage
);

return items.map( ( item, index ) => {
const { ID = null, id = null, type = 'post' } = item;
const { ID = null, id = null, post_type: postType = 'post' } = item;

// Remove any disallowed or hidden keys
for ( const itemKey in item ) {
Expand All @@ -256,7 +263,7 @@ export function LoopInnerBlocksRenderer( props ) {
}

return {
postType: type,
postType,
postId: id ? id : ID,
'generateblocks/loopItem': item,
'generateblocks/loopIndex': index + 1, // Preview doesn't support pagination so this index is correct.
Expand All @@ -273,7 +280,7 @@ export function LoopInnerBlocksRenderer( props ) {
},
'generateblocks/loopIndex': 1,
} ];
}, [ data, hasResolvedData, query?.per_page ] );
}, [ data, hasResolvedData, query?.posts_per_page, query?.offset ] );

if ( isResolvingData ) {
return ( <Spinner /> );
Expand Down

0 comments on commit 342e5ca

Please sign in to comment.