Skip to content

Commit

Permalink
fix bug where delete also reverts
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed May 9, 2024
1 parent 9fe5b87 commit 95ac1f9
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions packages/dataviews/src/bulk-actions-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,66 @@ const SNACKBAR_VARIANTS = {
},
};

function ActionTrigger( { action, onClick, items, isBusy } ) {
const isDisabled = useMemo( () => {
return isBusy || items.every( ( item ) => ! action.isEligible( item ) );
}, [ action, items, isBusy ] );
function ActionTrigger( { action, onClick, isBusy } ) {
return (
<ToolbarButton
disabled={ isDisabled }
disabled={ isBusy }
label={ action.label }
icon={ action.icon }
isDestructive={ action.isDestructive }
size="compact"
onClick={ onClick }
isBusy={ isBusy }
isDisabled={ isDisabled }
isDisabled={ isBusy }
/>
);
}

const EMPTY_ARRAY = [];

function ActionButton( {
action,
selectedItems,
actionInProgress,
setActionInProgress,
} ) {
const selectedEligibleItems = useMemo( () => {
return selectedItems.filter( ( item ) => {
return action.isEligible( item );
} );
}, [ action, selectedItems ] );
if ( !! action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ selectedEligibleItems }
ActionTrigger={ ActionTrigger }
onActionStart={ () => {
setActionInProgress( action.id );
} }
onActionPerformed={ () => {
setActionInProgress( null );
} }
/>
);
}
return (
<ActionTrigger
key={ action.id }
action={ action }
items={ selectedItems }
onClick={ () => {
setActionInProgress( action.id );
action.callback( selectedItems, () => {
setActionInProgress( action.id );
} );
} }
isBusy={ actionInProgress === action.id }
/>
);
}

function renderToolbarContent(
selection,
actionsToShow,
Expand Down Expand Up @@ -84,34 +124,13 @@ function renderToolbarContent(
</ToolbarGroup>
<ToolbarGroup>
{ actionsToShow.map( ( action ) => {
if ( !! action.RenderModal ) {
return (
<ActionWithModal
key={ action.id }
action={ action }
items={ selectedItems }
ActionTrigger={ ActionTrigger }
onActionStart={ () => {
setActionInProgress( action.id );
} }
onActionPerformed={ () => {
setActionInProgress( null );
} }
/>
);
}
return (
<ActionTrigger
<ActionButton
key={ action.id }
action={ action }
items={ selectedItems }
onClick={ () => {
setActionInProgress( action.id );
action.callback( selectedItems, () => {
setActionInProgress( action.id );
} );
} }
isBusy={ actionInProgress === action.id }
selectedItems={ selectedItems }
actionInProgress={ actionInProgress }
setActionInProgress={ setActionInProgress }
/>
);
} ) }
Expand Down

0 comments on commit 95ac1f9

Please sign in to comment.