Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Nov 29, 2023
1 parent cf15ad2 commit 674230b
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 202 deletions.
65 changes: 45 additions & 20 deletions packages/edit-site/src/components/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { addQueryArgs } from '@wordpress/url';
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 { __, sprintf, _n } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo } from '@wordpress/element';
import { privateApis as routerPrivateApis } from '@wordpress/router';
Expand All @@ -28,23 +28,34 @@ export const trashPostAction = {
id: 'move-to-trash',
label: __( 'Move to Trash' ),
isPrimary: true,
isBulk: true,
icon: trash,
isEligible( { status } ) {
return status !== 'trash';
},
hideModalHeader: true,
RenderModal: ( { item: post, closeModal } ) => {
RenderModal: ( { items: posts, closeModal } ) => {
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const { deleteEntityRecord } = useDispatch( coreStore );
return (
<VStack spacing="5">
<Text>
{ sprintf(
// translators: %s: The page's title.
__( 'Are you sure you want to delete "%s"?' ),
decodeEntities( post.title.rendered )
) }
{ posts.length > 1
? sprintf(
// translators: %s: The number of posts (always plural).
__(
'Are you sure you want to delete %s posts?'
),
decodeEntities( posts.length )
)
: sprintf(
// translators: %s: The page's title.
__( 'Are you sure you want to delete "%s"?' ),
decodeEntities(
posts && posts[ 0 ]?.title?.rendered
)
) }
</Text>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
Expand All @@ -54,19 +65,31 @@ export const trashPostAction = {
variant="primary"
onClick={ async () => {
try {
await deleteEntityRecord(
'postType',
post.type,
post.id,
{},
{ throwOnError: true }
await Promise.all(
posts.map( async ( post ) => {
deleteEntityRecord(
'postType',
post.type,
post.id,
{},
{ throwOnError: true }
);
} )
);
createSuccessNotice(
sprintf(
/* translators: The page's title. */
__( '"%s" moved to the Trash.' ),
decodeEntities( post.title.rendered )
),
posts.length > 1
? __(
'The selected posts were moved to the trash.'
)
: sprintf(
/* translators: The page's title. */
__(
'"%s" moved to the Trash.'
),
decodeEntities(
posts[ 0 ].title.rendered
)
),
{
type: 'snackbar',
id: 'edit-site-page-trashed',
Expand All @@ -77,8 +100,10 @@ export const trashPostAction = {
error.message &&
error.code !== 'unknown_error'
? error.message
: __(
'An error occurred while moving the page to the trash.'
: _n(
'An error occurred while moving the page to the trash.',
'An error occurred while moving the pages to the trash.',
posts.length
);

createErrorNotice( errorMessage, {
Expand Down
66 changes: 0 additions & 66 deletions packages/edit-site/src/components/bulk-actions/index.js

This file was deleted.

39 changes: 31 additions & 8 deletions packages/edit-site/src/components/dataviews/bulk-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {
ToolbarButton,
Toolbar,
ToolbarGroup,
ToolbarItem,
Popover,
} from '@wordpress/components';
import { useMemo } from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { ActionWithModal } from './item-actions';

function PrimaryActionTrigger( { action, onClick } ) {
return (
Expand All @@ -28,22 +31,34 @@ const EMPTY_ARRAY = [];
export default function BulkActions( {
data,
selection,
bulkActions = EMPTY_ARRAY,
actions = EMPTY_ARRAY,
setSelection,
} ) {
const items = useMemo(
() =>
data?.filter( ( item ) => selection?.includes( item.id ) ) ??
EMPTY_ARRAY,
[ data, selection ]
);
const primaryActions = useMemo(
() =>
bulkActions.filter( ( action ) => {
return action.isPrimary && action.isEligible( data, selection );
actions.filter( ( action ) => {
return (
action.isBulk &&
action.isPrimary &&
items.every( ( item ) => action.isEligible( item ) )
);
} ),
[ bulkActions, data, selection ]
[ actions, items ]
);

if (
( selection && selection.length === 0 ) ||
primaryActions.length === 0
) {
return null;
}

return (
<Popover
placement="top-middle"
Expand Down Expand Up @@ -76,13 +91,21 @@ export default function BulkActions( {
</ToolbarGroup>
<ToolbarGroup>
{ primaryActions.map( ( action ) => {
if ( !! action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ items }
ActionTrigger={ PrimaryActionTrigger }
/>
);
}
return (
<PrimaryActionTrigger
key={ action.id }
action={ action }
onClick={ () =>
action.callback( data, selection )
}
onClick={ () => action.callback( items ) }
/>
);
} ) }
Expand Down
4 changes: 1 addition & 3 deletions packages/edit-site/src/components/dataviews/dataviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
__experimentalVStack as VStack,
__experimentalHStack as HStack,
Popover,
} from '@wordpress/components';
import { useMemo } from '@wordpress/element';

Expand Down Expand Up @@ -49,7 +48,6 @@ export default function DataViews( {
supportedLayouts,
selection,
setSelection,
bulkActions,
} ) {
const ViewComponent = viewTypeMap[ view.type ];
const _fields = useMemo( () => {
Expand Down Expand Up @@ -104,7 +102,7 @@ export default function DataViews( {
/>
<BulkActions
data={ data }
bulkActions={ bulkActions }
actions={ actions }
selection={ selection }
setSelection={ setSelection }
/>
Expand Down
Loading

0 comments on commit 674230b

Please sign in to comment.