diff --git a/src/components/pagination-toggle.js b/src/components/pagination-toggle.js index d39c149..0c41066 100644 --- a/src/components/pagination-toggle.js +++ b/src/components/pagination-toggle.js @@ -1,31 +1,39 @@ /** * WordPress dependencies */ -import { ToggleControl } from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; +import { store as blockEditorStore } from '@wordpress/block-editor'; +import { useEffect } from '@wordpress/element'; -export const PaginationToggle = ( { attributes, setAttributes } ) => { - const { query: { disable_pagination: disablePagination } = {} } = - attributes; +/** + * Check for the core/query-pagination block. + * + * @param {Array} blocks The array of innerBlocks + * @return {string|boolean} Return either the clientId or false if not found. + */ +const getPaginationBlockClientId = ( blocks ) => { + return blocks.find( ( block ) => block.name === 'core/query-pagination' ) + ?.clientId; +}; - return ( - { - setAttributes( { - query: { - ...attributes.query, - disable_pagination: ! disablePagination, - }, - } ); - } } - __nextHasNoMarginBottom - /> +export const PaginationToggle = ( { attributes, setAttributes, clientId } ) => { + const innerBlocks = useSelect( + ( select ) => + select( blockEditorStore ).getBlocksByClientId( clientId )[ 0 ] + ?.innerBlocks ); + + useEffect( () => { + setAttributes( { + query: { + ...attributes.query, + disable_pagination: ! getPaginationBlockClientId( innerBlocks ) + ? true + : false, + }, + } ); + }, [ innerBlocks, setAttributes ] ); + + // There is no UI for component. + return null; }; diff --git a/src/variations/controls.js b/src/variations/controls.js index 5120681..7c91067 100644 --- a/src/variations/controls.js +++ b/src/variations/controls.js @@ -6,6 +6,7 @@ import { InspectorControls } from '@wordpress/block-editor'; import { PanelBody } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { createBlock } from '@wordpress/blocks'; + /** * Internal dependencies */