Skip to content

Commit

Permalink
Improve the warning for Comments Form placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
michalczaplinski committed Apr 22, 2022
1 parent 10fd07b commit 12053bb
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions packages/block-library/src/post-comments-form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
BlockControls,
Warning,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useEntityProp } from '@wordpress/core-data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';

import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
Expand All @@ -39,7 +40,49 @@ export default function PostCommentsFormEdit( {
} ),
} );

const isInSiteEditor = postType === undefined || postId === undefined;
const isSiteEditor = postType === undefined || postId === undefined;

const { defaultCommentStatus } = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()
.__experimentalDiscussionSettings
);

const postTypeSupportsComments = useSelect( ( select ) =>
postType
? !! select( coreStore ).getPostType( postType )?.supports.comments
: false
);

let warning = false;
let showPlacholder = true;

if ( ! isSiteEditor && 'open' !== commentStatus ) {
if ( 'closed' === commentStatus ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: Comments on this %s are not allowed.'
),
postType
);
showPlacholder = false;
} else if ( ! postTypeSupportsComments ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: Comments for this post type (%s) are not enabled.'
),
postType
);
showPlacholder = false;
} else if ( 'open' !== defaultCommentStatus ) {
warning = __(
'Post Comments Form block: Comments are not enabled.'
);
showPlacholder = false;
}
}

return (
<>
Expand All @@ -52,29 +95,9 @@ export default function PostCommentsFormEdit( {
/>
</BlockControls>
<div { ...blockProps }>
{ ! commentStatus && ! isInSiteEditor && (
<Warning>
{ __(
'Post Comments Form block: comments are not enabled for this post type.'
) }
</Warning>
) }

{ 'open' !== commentStatus && ! isInSiteEditor && (
<Warning>
{ sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: comments to this %s are not allowed.'
),
postType
) }
</Warning>
) }
{ warning && <Warning>{ warning }</Warning> }

{ ( 'open' === commentStatus || isInSiteEditor ) && (
<CommentsForm />
) }
{ showPlacholder ? <CommentsForm /> : null }
</div>
</>
);
Expand Down

0 comments on commit 12053bb

Please sign in to comment.