Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determine if no_found_rows should be enabled based on existence of core/pagination block #98

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions src/components/pagination-toggle.js
Original file line number Diff line number Diff line change
@@ -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 (
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Disable pagination', 'advanced-query-loop' ) }
help={ __(
'Disabling pagination will not show any pagination controls on the front end. It can also provide a performance improvement for complicated queries.',
'advanced-query-loop'
) }
checked={ !! disablePagination }
onChange={ () => {
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;
};
1 change: 1 addition & 0 deletions src/variations/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading