From 773e37fed956621f2c24e4fdaaa767ef61b49b30 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 20 Jun 2018 11:04:04 +0100 Subject: [PATCH] Effects: Move trash post URL change to the BrowserUrl component (#7228) * Effects: Move trash post URL change to the BrowserUrl component * Use the correct "trash" status * unit test the trashed post URL * Refactor componentDidUpdate to accomodate separate flows * Remove TRASH_POST_SUCCESS action and add a note about updatePost --- edit-post/components/browser-url/index.js | 37 +++++++++++++++++-- .../components/browser-url/test/index.js | 10 ++++- editor/store/effects.js | 19 +++------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/edit-post/components/browser-url/index.js b/edit-post/components/browser-url/index.js index b3973dd88070d6..7d50e897725407 100644 --- a/edit-post/components/browser-url/index.js +++ b/edit-post/components/browser-url/index.js @@ -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 ); @@ -26,17 +42,29 @@ export class BrowserURL extends Component { } componentDidUpdate( prevProps ) { - const { postId, postStatus } = this.props; + const { postId, postStatus, postType } = this.props; const { historyId } = this.state; - if ( postId === prevProps.postId && postId === historyId ) { + + if ( postStatus === 'trash' ) { + this.setTrashURL( postId, postType ); return; } - if ( postStatus !== 'auto-draft' ) { + if ( ( postId !== prevProps.postId || postId !== historyId ) && postStatus !== 'auto-draft' ) { this.setBrowserURL( postId ); } } + /** + * Navigates the browser to the post trashed URL to show a notice about the trashed post. + * + * @param {number} postId Post ID. + * @param {string} postType Post Type. + */ + setTrashURL( postId, postType ) { + window.location.href = getPostTrashedURL( postId, postType ); + } + /** * Replaces the browser URL with a post editor link for the given post ID. * @@ -65,10 +93,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 ); diff --git a/edit-post/components/browser-url/test/index.js b/edit-post/components/browser-url/test/index.js index 953c2dac92393d..a669896f5ca03e 100644 --- a/edit-post/components/browser-url/test/index.js +++ b/edit-post/components/browser-url/test/index.js @@ -6,7 +6,7 @@ import { shallow } from 'enzyme'; /** * Internal dependencies */ -import { getPostEditURL, BrowserURL } from '../'; +import { getPostEditURL, getPostTrashedURL, BrowserURL } from '../'; describe( 'getPostEditURL', () => { it( 'should generate relative path with post and action arguments', () => { @@ -16,6 +16,14 @@ describe( 'getPostEditURL', () => { } ); } ); +describe( 'getPostTrashedURL', () => { + it( 'should generate relative path with post and action arguments', () => { + const url = getPostTrashedURL( 1, 'page' ); + + expect( url ).toBe( 'edit.php?trashed=1&post_type=page&ids=1' ); + } ); +} ); + describe( 'BrowserURL', () => { let replaceStateSpy; diff --git a/editor/store/effects.js b/editor/store/effects.js index 3d18156758696d..5d98bb9821a9b1 100644 --- a/editor/store/effects.js +++ b/editor/store/effects.js @@ -25,7 +25,6 @@ import apiRequest from '@wordpress/api-request'; /** * Internal dependencies */ -import { getWPAdminURL } from '../utils/url'; import { setupEditorState, resetAutosave, @@ -261,10 +260,11 @@ export default { dispatch( removeNotice( TRASH_POST_NOTICE_ID ) ); apiRequest( { path: `/wp/v2/${ basePath }/${ postId }`, method: 'DELETE' } ).then( () => { - dispatch( { - ...action, - type: 'TRASH_POST_SUCCESS', - } ); + const post = getCurrentPost( getState() ); + + // TODO: This should be an updatePost action (updating subsets of post properties), + // But right now editPost is tied with change detection. + dispatch( resetPost( { ...post, status: 'trash' } ) ); }, ( err ) => { dispatch( { @@ -278,15 +278,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 } ) );