Skip to content

Commit

Permalink
Add: Permanently delete and restore actions to dataviews. (#55781)
Browse files Browse the repository at this point in the history
* Add: Permanently delete and restore actions.

* small updates

---------

Co-authored-by: ntsekouras <ntsekouras@outlook.com>
  • Loading branch information
jorgefilipecosta and ntsekouras authored Nov 3, 2023
1 parent a229cc2 commit 6f46fe8
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
110 changes: 109 additions & 1 deletion packages/edit-site/src/components/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { external, trash } from '@wordpress/icons';
import { external, trash, backup } from '@wordpress/icons';
import { addQueryArgs } from '@wordpress/url';
import { useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
Expand Down Expand Up @@ -67,6 +67,114 @@ export function useTrashPostAction() {
);
}

export function usePermanentlyDeletePostAction() {
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const { deleteEntityRecord } = useDispatch( coreStore );

return useMemo(
() => ( {
id: 'permanently-delete',
label: __( 'Permanently delete' ),
isPrimary: true,
icon: trash,
isEligible( { status } ) {
return status === 'trash';
},
async perform( post ) {
try {
await deleteEntityRecord(
'postType',
post.type,
post.id,
{ force: true },
{ throwOnError: true }
);
createSuccessNotice(
sprintf(
/* translators: The posts's title. */
__( '"%s" permanently deleted.' ),
decodeEntities( post.title.rendered )
),
{
type: 'snackbar',
id: 'edit-site-post-permanently-deleted',
}
);
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __(
'An error occurred while permanently deleting the post.'
);

createErrorNotice( errorMessage, { type: 'snackbar' } );
}
},
} ),
[ createSuccessNotice, createErrorNotice, deleteEntityRecord ]
);
}

export function useRestorePostAction() {
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
const { editEntityRecord, saveEditedEntityRecord } =
useDispatch( coreStore );

return useMemo(
() => ( {
id: 'restore',
label: __( 'Restore' ),
isPrimary: true,
icon: backup,
isEligible( { status } ) {
return status === 'trash';
},
async perform( post ) {
await editEntityRecord( 'postType', post.type, post.id, {
status: 'draft',
} );
try {
await saveEditedEntityRecord(
'postType',
post.type,
post.id,
{ throwOnError: true }
);
createSuccessNotice(
sprintf(
/* translators: The posts's title. */
__( '"%s" has been restored.' ),
decodeEntities( post.title.rendered )
),
{
type: 'snackbar',
id: 'edit-site-post-restored',
}
);
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __(
'An error occurred while restoring the post.'
);

createErrorNotice( errorMessage, { type: 'snackbar' } );
}
},
} ),
[
createSuccessNotice,
createErrorNotice,
editEntityRecord,
saveEditedEntityRecord,
]
);
}

export const viewPostAction = {
id: 'view-post',
label: __( 'View' ),
Expand Down
13 changes: 12 additions & 1 deletion packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { DataViews } from '../dataviews';
import { DEFAULT_STATUSES, default as DEFAULT_VIEWS } from './default-views';
import {
useTrashPostAction,
usePermanentlyDeletePostAction,
useRestorePostAction,
postRevisionsAction,
viewPostAction,
useEditPostAction,
Expand Down Expand Up @@ -225,15 +227,24 @@ export default function PagePages() {
);

const trashPostAction = useTrashPostAction();
const permanentlyDeletePostAction = usePermanentlyDeletePostAction();
const restorePostAction = useRestorePostAction();
const editPostAction = useEditPostAction();
const actions = useMemo(
() => [
viewPostAction,
trashPostAction,
restorePostAction,
permanentlyDeletePostAction,
editPostAction,
postRevisionsAction,
],
[ trashPostAction, editPostAction ]
[
trashPostAction,
permanentlyDeletePostAction,
restorePostAction,
editPostAction,
]
);
const onChangeView = useCallback(
( viewUpdater ) => {
Expand Down

1 comment on commit 6f46fe8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 6f46fe8.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6742370099
📝 Reported issues:

Please sign in to comment.