Skip to content

Commit

Permalink
Effects: Move trash post URL change to the BrowserUrl component
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 8, 2018
1 parent a9fab4d commit 3337f28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
26 changes: 24 additions & 2 deletions edit-post/components/browser-url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ export function getPostEditURL( postId ) {
return addQueryArgs( 'post.php', { post: postId, action: 'edit' } );
}

/**
* Returns the Post's Trashedd URL.
*
* @param {number} postId Post ID.
* @param {string} postType Post Type.
*
* @return {string} Post trashed URL.
*/
export function getPostTrashedURL( postId, postType ) {
return addQueryArgs( 'edit.php', {
trashed: 1,
post_type: postType,
ids: postId,
} );
}

export class BrowserURL extends Component {
constructor() {
super( ...arguments );
Expand All @@ -26,8 +42,13 @@ export class BrowserURL extends Component {
}

componentDidUpdate( prevProps ) {
const { postId, postStatus } = this.props;
const { postId, postStatus, postType } = this.props;
const { historyId } = this.state;

if ( postStatus === 'trashed' ) {
window.location.href = getPostTrashedURL( postId, postType );
}

if ( postId === prevProps.postId && postId === historyId ) {
return;
}
Expand Down Expand Up @@ -65,10 +86,11 @@ export class BrowserURL extends Component {

export default withSelect( ( select ) => {
const { getCurrentPost } = select( 'core/editor' );
const { id, status } = getCurrentPost();
const { id, status, type } = getCurrentPost();

return {
postId: id,
postStatus: status,
postType: type,
};
} )( BrowserURL );
12 changes: 2 additions & 10 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import apiRequest from '@wordpress/api-request';
/**
* Internal dependencies
*/
import { getWPAdminURL } from '../utils/url';
import {
setupEditorState,
resetAutosave,
Expand Down Expand Up @@ -254,6 +253,8 @@ export default {
dispatch( removeNotice( TRASH_POST_NOTICE_ID ) );
apiRequest( { path: `/wp/v2/${ basePath }/${ postId }`, method: 'DELETE' } ).then(
() => {
const post = getCurrentPost( getState() );
dispatch( resetPost( { ...post, status: 'trashed' } ) );
dispatch( {
...action,
type: 'TRASH_POST_SUCCESS',
Expand All @@ -271,15 +272,6 @@ export default {
}
);
},
TRASH_POST_SUCCESS( action ) {
const { postId, postType } = action;

window.location.href = getWPAdminURL( 'edit.php', {
trashed: 1,
post_type: postType,
ids: postId,
} );
},
TRASH_POST_FAILURE( action, store ) {
const message = action.error.message && action.error.code !== 'unknown_error' ? action.error.message : __( 'Trashing failed' );
store.dispatch( createErrorNotice( message, { id: TRASH_POST_NOTICE_ID } ) );
Expand Down

0 comments on commit 3337f28

Please sign in to comment.