-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Gutenberg Data Tutorial 5: Adding a Delete Button #40940
Conversation
…berg Data: "Adding a delete button"
…iting are still missing)
a223e36
to
200fb4e
Compare
This looks good. A couple of ideas:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work as always. Easy to read and to follow. Left some nits.
Co-authored-by: Andrei Draganescu <[email protected]>
Co-authored-by: Andrei Draganescu <[email protected]>
{ isDeleting ? ( | ||
<> | ||
<Spinner /> | ||
Deleting... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe for the sake of good practice we should use __( string )
for strings? Maybe it's too much for this tutorial tho.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scruffian have already mentioned that – I'd like to spin another PR to update all the parts of this tutorial at once.
To tell the user when any of these happens, we need to extract the error information using the `getLastEntityDeleteError` selector: | ||
|
||
```js | ||
// Replace 9 with an actual page ID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit confusing. Why not use a placeholder instead of this comment about the magic number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you introduce a placeholder that would clearly communicate that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left the placeholder for now, I'm happy to revisit later on, though!
Co-authored-by: Andrei Draganescu <[email protected]>
Co-authored-by: Andrei Draganescu <[email protected]>
Co-authored-by: Andrei Draganescu <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few nits but this is awesome otherwise!
Co-authored-by: Andrei Draganescu <[email protected]>
…on to avoid the feeling of abruptness.
Thanks for this great addition. One thing I noted is that the Alternatively we could use that to render a modal dialog, issue a toast when truthy, render an error message, or simply omit the rendering code for the example purposes. Other than that I think these tutorials are getting so much more valuable 👍 |
@dmsnell Hm I was sure I removed the const DeletePageButton = ({ pageId }) => {
// ...
const { error, /* ... */ } = useSelect(
select => ( {
error: select( coreDataStore ).getLastEntityDeleteError( 'postType', 'page', pageId ),
// ...
} ),
[pageId]
);
useEffect( () => {
if ( error ) {
// Display the error
}
}, [error] )
// ...
} If so – great spot, I'll rework it to be consistent with the error handling code later in the same document. |
if you removed it I might have missed it because I followed the link in the description, which referenced a specific commit, so maybe it referenced an earlier version of the PR. the main thing was in that |
It's already taken care of @dmsnell: import { store as noticesStore } from '@wordpress/notices';
import { useEffect } from '@wordpress/element';
function DeletePageButton( { pageId } ) {
const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore );
// useSelect returns a list of selectors if you pass the store handle
// instead of a callback:
const { getLastEntityDeleteError } = useSelect( coreDataStore )
const handleDelete = async () => {
const success = await deleteEntityRecord( 'postType', 'page', pageId);
if ( success ) {
// Tell the user the operation succeeded:
createSuccessNotice( "The page was deleted!", {
type: 'snackbar',
} );
} else {
// We use the selector directly to get the fresh error *after* the deleteEntityRecord
// have failed.
const lastError = getLastEntityDeleteError( 'postType', 'page', pageId );
const message = ( lastError?.message || 'There was an error.' ) + ' Please refresh the page and try again.'
// Tell the user how exactly the operation has failed:
createErrorNotice( message, {
type: 'snackbar',
} );
}
}
// ...
} |
Description
This PR introduces the 5th part of Create your First App with Gutenberg Data tutorial – the focus is on deleting entity records.
Follows up on the fourth part: #39261 (sadly, the images aren't rendered properly there)
See the rich-text preview