Skip to content

Commit 0e94613

Browse files
committed
Derive whether to disable pagination based on the existence of the core/pagination block.
1 parent 6db0fd7 commit 0e94613

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/components/pagination-toggle.js

+32-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { ToggleControl } from '@wordpress/components';
5-
import { __ } from '@wordpress/i18n';
4+
import { useSelect } from '@wordpress/data';
5+
import { store as blockEditorStore } from '@wordpress/block-editor';
6+
import { useEffect } from '@wordpress/element';
67

7-
export const PaginationToggle = ( { attributes, setAttributes } ) => {
8-
const { query: { disable_pagination: disablePagination } = {} } =
9-
attributes;
8+
/**
9+
* Check for the core/query-pagination block.
10+
*
11+
* @param {Array} blocks The array of innerBlocks
12+
* @return {string|boolean} Return either the clientId or false if not found.
13+
*/
14+
const getPaginationBlockClientId = ( blocks ) => {
15+
return blocks.find( ( block ) => block.name === 'core/query-pagination' )
16+
?.clientId;
17+
};
1018

11-
return (
12-
<ToggleControl
13-
__nextHasNoMarginBottom
14-
label={ __( 'Disable pagination', 'advanced-query-loop' ) }
15-
help={ __(
16-
'Disabling pagination will not show any pagination controls on the front end. It can also provide a performance improvement for complicated queries.',
17-
'advanced-query-loop'
18-
) }
19-
checked={ !! disablePagination }
20-
onChange={ () => {
21-
setAttributes( {
22-
query: {
23-
...attributes.query,
24-
disable_pagination: ! disablePagination,
25-
},
26-
} );
27-
} }
28-
__nextHasNoMarginBottom
29-
/>
19+
export const PaginationToggle = ( { attributes, setAttributes, clientId } ) => {
20+
const innerBlocks = useSelect(
21+
( select ) =>
22+
select( blockEditorStore ).getBlocksByClientId( clientId )[ 0 ]
23+
?.innerBlocks
3024
);
25+
26+
useEffect( () => {
27+
setAttributes( {
28+
query: {
29+
...attributes.query,
30+
disable_pagination: ! getPaginationBlockClientId( innerBlocks )
31+
? true
32+
: false,
33+
},
34+
} );
35+
}, [ innerBlocks, setAttributes ] );
36+
37+
// There is no UI for component.
38+
return null;
3139
};

0 commit comments

Comments
 (0)