Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Add 'isDeletingPost' selector #44012

Merged
merged 3 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,18 @@ _Returns_

- `boolean`: Whether current post is scheduled to be posted.

### isDeletingPost

Returns true if the post is currently being deleted, or false otherwise.

_Parameters_

- _state_ `Object`: Editor state.

_Returns_

- `boolean`: Whether post is being deleted.

### isEditedPostAutosaveable

Returns true if the post can be autosaved, or false otherwise.
Expand Down
7 changes: 5 additions & 2 deletions packages/editor/src/components/post-trash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { store as editorStore } from '../../store';

export default function PostTrash() {
const { isNew, postId } = useSelect( ( select ) => {
const { isNew, isDeleting, postId } = useSelect( ( select ) => {
const store = select( editorStore );
return {
isNew: store.isEditedPostNew(),
isDeleting: store.isDeletingPost(),
postId: store.getCurrentPostId(),
};
}, [] );
Expand All @@ -29,7 +30,9 @@ export default function PostTrash() {
className="editor-post-trash"
isDestructive
variant="secondary"
onClick={ () => trashPost() }
isBusy={ isDeleting }
aria-disabled={ isDeleting }
onClick={ isDeleting ? undefined : () => trashPost() }
>
{ __( 'Move to trash' ) }
</Button>
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export const trashPost =
registry.dispatch( noticesStore ).removeNotice( TRASH_POST_NOTICE_ID );
const { rest_base: restBase, rest_namespace: restNamespace = 'wp/v2' } =
postType;
dispatch( { type: 'REQUEST_POST_DELETE_START' } );
try {
const post = select.getCurrentPost();
await apiFetch( {
Expand All @@ -262,6 +263,7 @@ export const trashPost =
...getNotificationArgumentsForTrashFail( { error } )
);
}
dispatch( { type: 'REQUEST_POST_DELETE_FINISH' } );
};

/**
Expand Down
21 changes: 21 additions & 0 deletions packages/editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ export function saving( state = {}, action ) {
return state;
}

/**
* Reducer returning deleting post request state.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function deleting( state = {}, action ) {
switch ( action.type ) {
case 'REQUEST_POST_DELETE_START':
case 'REQUEST_POST_DELETE_FINISH':
return {
pending: action.type === 'REQUEST_POST_DELETE_START',
};
}

return state;
}

/**
* Post Lock State.
*
Expand Down Expand Up @@ -263,6 +283,7 @@ export default combineReducers( {
postId,
postType,
saving,
deleting,
postLock,
template,
postSavingLock,
Expand Down
11 changes: 11 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,17 @@ export function isEditedPostDateFloating( state ) {
return false;
}

/**
* Returns true if the post is currently being deleted, or false otherwise.
*
* @param {Object} state Editor state.
*
* @return {boolean} Whether post is being deleted.
*/
export function isDeletingPost( state ) {
return !! state.deleting.pending;
}

/**
* Returns true if the post is currently being saved, or false otherwise.
*
Expand Down
42 changes: 42 additions & 0 deletions packages/editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,48 @@ describe( 'Post actions', () => {
const { status } = registry.select( editorStore ).getCurrentPost();
expect( status ).toBe( 'trash' );
} );

it( 'sets deleting state', async () => {
const post = {
id: postId,
type: 'post',
content: 'foo',
status: 'publish',
};

const dispatch = Object.assign( jest.fn(), {
savePost: jest.fn(),
} );
const select = {
getCurrentPostType: () => 'post',
getCurrentPost: () => post,
};
const registry = {
dispatch: () => ( {
removeNotice: jest.fn(),
createErrorNotice: jest.fn(),
} ),
resolveSelect: () => ( {
getPostType: () => ( {
rest_namespace: 'wp/v2',
rest_base: 'posts',
} ),
} ),
};

apiFetch.setFetchHandler( async () => {
return { ...post, status: 'trash' };
} );

await actions.trashPost()( { select, dispatch, registry } );

expect( dispatch ).toHaveBeenCalledWith( {
type: 'REQUEST_POST_DELETE_START',
} );
expect( dispatch ).toHaveBeenCalledWith( {
type: 'REQUEST_POST_DELETE_FINISH',
} );
} );
} );
} );

Expand Down