-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataViews: Allow actions to be provided declaratively as a prop (#55192)
* DataViews: Allow actions to be provided declaratively as a prop * Restore PageActions component * Rename variable * Fix typo
- Loading branch information
1 parent
f29a96b
commit 8a9cb70
Showing
4 changed files
with
112 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
export default function useMoveToTrashAction() { | ||
const { createSuccessNotice, createErrorNotice } = | ||
useDispatch( noticesStore ); | ||
const { deleteEntityRecord } = useDispatch( coreStore ); | ||
|
||
return useMemo( | ||
() => ( { | ||
id: 'move-to-trash', | ||
label: __( 'Move to Trash' ), | ||
async perform( post ) { | ||
try { | ||
await deleteEntityRecord( | ||
'postType', | ||
post.type, | ||
post.id, | ||
{}, | ||
{ throwOnError: true } | ||
); | ||
createSuccessNotice( | ||
sprintf( | ||
/* translators: The page's title. */ | ||
__( '"%s" moved to the Trash.' ), | ||
decodeEntities( post.title.rendered ) | ||
), | ||
{ | ||
type: 'snackbar', | ||
id: 'edit-site-page-trashed', | ||
} | ||
); | ||
} catch ( error ) { | ||
const errorMessage = | ||
error.message && error.code !== 'unknown_error' | ||
? error.message | ||
: __( | ||
'An error occurred while moving the page to the trash.' | ||
); | ||
|
||
createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
} | ||
}, | ||
isDesctructive: true, | ||
} ), | ||
[ createSuccessNotice, createErrorNotice, deleteEntityRecord ] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters