-
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.
Add: Bulk actions API and trash action.
- Loading branch information
1 parent
f987124
commit d0d1456
Showing
5 changed files
with
213 additions
and
0 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,66 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { trash } from '@wordpress/icons'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
export function useBulkTrashPostAction() { | ||
const { createSuccessNotice, createErrorNotice } = | ||
useDispatch( noticesStore ); | ||
const { deleteEntityRecord } = useDispatch( coreStore ); | ||
return useMemo( | ||
() => ( { | ||
id: 'move-to-trash', | ||
label: __( 'Move to Trash' ), | ||
isPrimary: true, | ||
icon: trash, | ||
isEligible( data, selection ) { | ||
console.log( { | ||
p: data.filter( ( post ) => selection.includes( post.id ) ), | ||
} ); | ||
return ! data | ||
.filter( ( post ) => selection.includes( post.id ) ) | ||
.some( ( post ) => post.status === 'trash' ); | ||
}, | ||
async callback( data, selection ) { | ||
const postsToDelete = data.filter( ( post ) => | ||
selection.includes( post.id ) | ||
); | ||
try { | ||
await Promise.all( | ||
postsToDelete.map( async ( post ) => { | ||
deleteEntityRecord( | ||
'postType', | ||
post.type, | ||
post.id, | ||
{}, | ||
{ throwOnError: true } | ||
); | ||
} ) | ||
); | ||
createSuccessNotice( | ||
__( 'The selected posts were moved to the trash.' ), | ||
{ | ||
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 posts to the trash.' | ||
); | ||
|
||
createErrorNotice( errorMessage, { type: 'snackbar' } ); | ||
} | ||
}, | ||
} ), | ||
[ createErrorNotice, createSuccessNotice, deleteEntityRecord ] | ||
); | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/edit-site/src/components/dataviews/bulk-actions.js
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,94 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
ToolbarButton, | ||
Toolbar, | ||
ToolbarGroup, | ||
ToolbarItem, | ||
Popover, | ||
} from '@wordpress/components'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __, _n, sprintf } from '@wordpress/i18n'; | ||
|
||
function PrimaryActionTrigger( { action, onClick } ) { | ||
return ( | ||
<ToolbarButton | ||
label={ action.label } | ||
icon={ action.icon } | ||
isDestructive={ action.isDestructive } | ||
size="compact" | ||
onClick={ onClick } | ||
/> | ||
); | ||
} | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
export default function BulkActions( { | ||
data, | ||
selection, | ||
bulkActions = EMPTY_ARRAY, | ||
setSelection, | ||
} ) { | ||
const primaryActions = useMemo( | ||
() => | ||
bulkActions.filter( ( action ) => { | ||
return action.isPrimary && action.isEligible( data, selection ); | ||
} ), | ||
[ bulkActions, data, selection ] | ||
); | ||
if ( | ||
( selection && selection.length === 0 ) || | ||
primaryActions.length === 0 | ||
) { | ||
return null; | ||
} | ||
return ( | ||
<Popover | ||
placement="top-middle" | ||
className="dataviews-bulk-actions-popover" | ||
> | ||
<Toolbar label="Bulk actions"> | ||
<div className="dataviews-bulk-actions-toolbar-wrapper"> | ||
<ToolbarGroup> | ||
<ToolbarButton onClick={ () => {} } disabled={ true }> | ||
{ | ||
// translators: %s: Total number of selected items. | ||
sprintf( | ||
// translators: %s: Total number of selected items. | ||
_n( | ||
'%s item selected', | ||
'%s items selected', | ||
selection.length | ||
), | ||
selection.length | ||
) | ||
} | ||
</ToolbarButton> | ||
<ToolbarButton | ||
onClick={ () => { | ||
setSelection( EMPTY_ARRAY ); | ||
} } | ||
> | ||
{ __( 'Deselect' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
<ToolbarGroup> | ||
{ primaryActions.map( ( action ) => { | ||
return ( | ||
<PrimaryActionTrigger | ||
key={ action.id } | ||
action={ action } | ||
onClick={ () => | ||
action.callback( data, selection ) | ||
} | ||
/> | ||
); | ||
} ) } | ||
</ToolbarGroup> | ||
</div> | ||
</Toolbar> | ||
</Popover> | ||
); | ||
} |
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