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

Update block scaffolding to use the withNotices HOC #14895

Merged
merged 2 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion extensions/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
"videopress",
"wordads"
],
"beta": [ "amazon", "seo" ]
"beta": [ "amazon", "seo", "cool-block" ]
scruffian marked this conversation as resolved.
Show resolved Hide resolved
}
19 changes: 9 additions & 10 deletions wp-cli-templates/block-edit-js.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { BlockIcon } from '@wordpress/block-editor';
import { Notice, Placeholder } from '@wordpress/components';
import { Placeholder, withNotices } from '@wordpress/components';
import { useState } from '@wordpress/element';

/**
Expand All @@ -12,7 +12,7 @@ import { useState } from '@wordpress/element';
import './editor.scss';
import icon from './icon';

export default function {{ className }}Edit( { attributes, className, setAttributes } ) {
function {{ className }}Edit( { attributes, className, noticeOperations, noticeUI, setAttributes } ) {
/**
* Write the block editor UI.
*
Expand All @@ -21,24 +21,23 @@ export default function {{ className }}Edit( { attributes, className, setAttribu
const [ notice, setNotice ] = useState();

/* Call this function when you want to show an error in the placeholder. */
const setErrorNotice = () => setNotice( __( 'Put error message here.', 'jetpack' ) );
const setErrorNotice = () => {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( __( 'Put error message here.', 'jetpack' ) );
};
Comment on lines +24 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of me wonders if we shouldn't extract this into a shared function available to all blocks? Ideally, that function could also handle all kinds of notices, not just erors. Something like this:

export function displayNotice( message, noticeOperations, type = 'error' ) {
	noticeOperations.removeAllNotices();
	noticeOperations.createNotice( { status: type, content: message } );
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be worth trying in some blocks, but I'd rather we only move stuff into the scaffolding when we've already had success with it in other blocks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I suggested we give that a try in #14884 (comment)

Until then, we should be able to merge your PR and revisit once we have some blocks using it.


return (
<div className={ className }>
<Placeholder
label={ __( '{{ title }}', 'jetpack' ) }
instructions={ __( 'Instructions go here.', 'jetpack' ) }
icon={ <BlockIcon icon={ icon } /> }
notices={
notice && (
<Notice status="error" isDismissible={ false }>
{ notice }
</Notice>
)
}
notices={ noticeUI }
>
{ __( 'User input goes here?', 'jetpack' ) }
</Placeholder>
</div>
);
}

export default {{ className }}Edit;