Skip to content

Commit

Permalink
Show confirmation dialog when moving a post to the trash
Browse files Browse the repository at this point in the history
  • Loading branch information
mtias committed May 1, 2023
1 parent 76cf678 commit 5f99550
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions packages/editor/src/components/post-trash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import {
Button,
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -20,21 +24,40 @@ export default function PostTrash() {
};
}, [] );
const { trashPost } = useDispatch( editorStore );
const [ showConfirmDialog, setShowConfirmDialog ] = useState( false );

if ( isNew || ! postId ) {
return null;
}

const handleConfirm = () => {
setShowConfirmDialog( false );
trashPost();
};

return (
<Button
className="editor-post-trash"
isDestructive
variant="secondary"
isBusy={ isDeleting }
aria-disabled={ isDeleting }
onClick={ isDeleting ? undefined : () => trashPost() }
>
{ __( 'Move to trash' ) }
</Button>
<>
<Button
className="editor-post-trash"
isDestructive
variant="secondary"
isBusy={ isDeleting }
aria-disabled={ isDeleting }
onClick={
isDeleting ? undefined : () => setShowConfirmDialog( true )
}
>
{ __( 'Move to trash' ) }
</Button>
<ConfirmDialog
isOpen={ showConfirmDialog }
onConfirm={ handleConfirm }
onCancel={ () => setShowConfirmDialog( false ) }
>
{ __(
'Are you sure you want to move this post to the trash?'
) }
</ConfirmDialog>
</>
);
}

0 comments on commit 5f99550

Please sign in to comment.