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

Top Posts & Pages Block: Prevent Disabling all Types #36305

Merged
merged 2 commits into from
Jun 13, 2024
Merged
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
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/update-top-posts-toggle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Top Posts & Pages Block: Require that one content type is always set to display.
24 changes: 20 additions & 4 deletions projects/plugins/jetpack/extensions/blocks/top-posts/controls.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
Notice,
PanelBody,
RangeControl,
SelectControl,
ToggleControl,
ToolbarGroup,
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';

export function TopPostsInspectorControls( {
Expand All @@ -17,17 +19,26 @@ export function TopPostsInspectorControls( {
const { displayAuthor, displayContext, displayDate, displayThumbnail, period, postsToShow } =
attributes;

const [ showErrorMessage, setShowErrorMessage ] = useState( false );

if ( ! postTypesData ) {
return;
}

const handleToggleChange = toggleId => isChecked => {
setToggleAttributes( prevAttributes => ( {
...prevAttributes,
const updatedAttributes = {
...toggleAttributes,
[ toggleId ]: isChecked,
} ) );
};

// Require at least one type to be selected.
if ( Object.values( updatedAttributes ).every( value => value === false ) ) {
return setShowErrorMessage( true );
}

setAttributes( { postTypes: { ...toggleAttributes, [ toggleId ]: isChecked } } );
setToggleAttributes( updatedAttributes );
setAttributes( { postTypes: updatedAttributes } );
setShowErrorMessage( false );
};

const periodOptions = [
Expand Down Expand Up @@ -70,6 +81,11 @@ export function TopPostsInspectorControls( {
onChange={ handleToggleChange( toggle.id ) }
/>
) ) }
{ showErrorMessage && (
<Notice className="jetpack-top-posts__error" status="error" isDismissible={ false }>
{ __( 'At least one item must be selected.', 'jetpack' ) }
</Notice>
) }
</PanelBody>
<PanelBody title={ __( 'Metadata settings', 'jetpack' ) }>
<ToggleControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
.wp-block-jetpack-top-posts .components-placeholder__fieldset {
display: block;
}

.jetpack-top-posts__error {
margin: 0;
}