Skip to content

Commit

Permalink
Editor: Port deprecated notices action to module usage
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Oct 12, 2018
1 parent c0d5fe2 commit 839c4ba
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 108 deletions.
4 changes: 2 additions & 2 deletions packages/editor/src/components/editor-notices/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function EditorNotices( props ) {

export default compose( [
withSelect( ( select ) => ( {
notices: select( 'core/editor' ).getNotices(),
notices: select( 'core/notices' ).getNotices(),
} ) ),
withDispatch( ( dispatch ) => ( {
onRemove: dispatch( 'core/editor' ).removeNotice,
onRemove: dispatch( 'core/notices' ).removeNotice,
} ) ),
] )( EditorNotices );
29 changes: 25 additions & 4 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { compose } from '@wordpress/compose';
import { createElement, Component } from '@wordpress/element';
import { DropZoneProvider, SlotFillProvider } from '@wordpress/components';
import { withDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -21,10 +22,27 @@ class EditorProvider extends Component {
super( ...arguments );

// Assume that we don't need to initialize in the case of an error recovery.
if ( ! props.recovery ) {
this.props.updateEditorSettings( props.settings );
this.props.updatePostLock( props.settings.postLock );
this.props.setupEditor( props.post, props.settings.autosave );
if ( props.recovery ) {
return;
}

props.updateEditorSettings( props.settings );
props.updatePostLock( props.settings.postLock );
props.setupEditor( props.post );

if ( props.settings.autosave ) {
props.createWarningNotice(
__( 'There is an autosave of this post that is more recent than the version below.' ),
{
id: 'autosave-exists',
actions: [
{
label: __( 'View the autosave' ),
url: props.settings.autosave.editLink,
},
],
}
);
}
}

Expand Down Expand Up @@ -93,9 +111,12 @@ export default withDispatch( ( dispatch ) => {
updateEditorSettings,
updatePostLock,
} = dispatch( 'core/editor' );
const { createWarningNotice } = dispatch( 'core/notices' );

return {
setupEditor,
updateEditorSettings,
updatePostLock,
createWarningNotice,
};
} )( EditorProvider );
23 changes: 1 addition & 22 deletions packages/editor/src/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import {
doBlocksMatchTemplate,
synchronizeBlocksWithTemplate,
} from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import {
setupEditorState,
replaceBlocks,
createWarningNotice,
selectBlock,
resetBlocks,
setTemplateValidity,
Expand Down Expand Up @@ -53,7 +51,6 @@ import {
trashPost,
trashPostFailure,
refreshPost,
AUTOSAVE_POST_NOTICE_ID,
} from './effects/posts';

/**
Expand Down Expand Up @@ -200,7 +197,7 @@ export default {
) );
},
SETUP_EDITOR( action, store ) {
const { post, autosave } = action;
const { post } = action;
const state = store.getState();

// Parse content as blocks
Expand All @@ -219,28 +216,10 @@ export default {
edits.title = post.title.raw;
}

// Check the auto-save status
let autosaveAction;
if ( autosave ) {
const noticeMessage = __( 'There is an autosave of this post that is more recent than the version below.' );
autosaveAction = createWarningNotice(
<p>
{ noticeMessage }
{ ' ' }
<a href={ autosave.editLink }>{ __( 'View the autosave' ) }</a>
</p>,
{
id: AUTOSAVE_POST_NOTICE_ID,
spokenMessage: noticeMessage,
}
);
}

const setupAction = setupEditorState( post, blocks, edits );

return compact( [
setupAction,
autosaveAction,

// TODO: This is temporary, necessary only so long as editor setup
// is a separate action from block resetting.
Expand Down
90 changes: 49 additions & 41 deletions packages/editor/src/store/effects/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { pick, includes } from 'lodash';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
// TODO: Ideally this would be the only dispatch in scope. This requires either
// refactoring editor actions to yielded controls, or replacing direct dispatch
// on the editor store with action creators (e.g. `REQUEST_POST_UPDATE_START`).
import { dispatch as dataDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -18,9 +22,6 @@ import {
resetAutosave,
resetPost,
updatePost,
removeNotice,
createSuccessNotice,
createErrorNotice,
} from '../actions';
import {
getCurrentPost,
Expand All @@ -38,8 +39,7 @@ import { resolveSelector } from './utils';
/**
* Module Constants
*/
const SAVE_POST_NOTICE_ID = 'SAVE_POST_NOTICE_ID';
export const AUTOSAVE_POST_NOTICE_ID = 'AUTOSAVE_POST_NOTICE_ID';
export const SAVE_POST_NOTICE_ID = 'SAVE_POST_NOTICE_ID';
const TRASH_POST_NOTICE_ID = 'TRASH_POST_NOTICE_ID';

/**
Expand Down Expand Up @@ -120,8 +120,8 @@ export const requestPostUpdate = async ( action, store ) => {
data: toSend,
} );
} else {
dispatch( removeNotice( SAVE_POST_NOTICE_ID ) );
dispatch( removeNotice( AUTOSAVE_POST_NOTICE_ID ) );
dataDispatch( 'core/notices' ).removeNotice( SAVE_POST_NOTICE_ID );
dataDispatch( 'core/notices' ).removeNotice( 'autosave-exists' );

request = apiFetch( {
path: `/wp/v2/${ postType.rest_base }/${ post.id }`,
Expand Down Expand Up @@ -217,24 +217,30 @@ export const requestPostUpdateSuccess = ( action, store ) => {
}

if ( noticeMessage ) {
dispatch( createSuccessNotice(
<p>
{ noticeMessage }
{ ' ' }
{ shouldShowLink && <a href={ post.link }>{ __( 'View post' ) }</a> }
</p>,
{ id: SAVE_POST_NOTICE_ID, spokenMessage: noticeMessage }
) );
const actions = [];
if ( shouldShowLink ) {
actions.push( {
label: __( 'View post' ),
url: post.link,
} );
}

dataDispatch( 'core/notices' ).createSuccessNotice(
noticeMessage,
{
id: SAVE_POST_NOTICE_ID,
actions,
}
);
}
};

/**
* Request Post Update Failure Effect handler
*
* @param {Object} action action object.
* @param {Object} store Redux Store.
*/
export const requestPostUpdateFailure = ( action, store ) => {
export const requestPostUpdateFailure = ( action ) => {
const { post, edits, error } = action;

if ( error && 'rest_autosave_no_changes' === error.code ) {
Expand All @@ -243,8 +249,6 @@ export const requestPostUpdateFailure = ( action, store ) => {
return;
}

const { dispatch } = store;

const publishStatus = [ 'publish', 'private', 'future' ];
const isPublished = publishStatus.indexOf( post.status ) !== -1;
// If the post was being published, we show the corresponding publish error message
Expand All @@ -258,26 +262,28 @@ export const requestPostUpdateFailure = ( action, store ) => {
messages[ edits.status ] :
__( 'Updating failed' );

const cloudflareDetailsLink = addQueryArgs(
'post.php',
{
post: post.id,
action: 'edit',
'classic-editor': '',
'cloudflare-error': '',
} );
dataDispatch( 'core/notices' ).createErrorNotice( noticeMessage, {
id: SAVE_POST_NOTICE_ID,
} );

const cloudflaredMessage = error && 'cloudflare_error' === error.code ?
<p>
{ noticeMessage }
<br />
{ __( 'Cloudflare is blocking REST API requests.' ) }
{ ' ' }
<a href={ cloudflareDetailsLink }>{ __( 'Learn More' ) } </a>
</p> :
noticeMessage;

dispatch( createErrorNotice( cloudflaredMessage, { id: SAVE_POST_NOTICE_ID } ) );
if ( error && 'cloudflare_error' === error.code ) {
dataDispatch( 'core/notices' ).createErrorNotice(
__( 'Cloudflare is blocking REST API requests.' ),
{
actions: [
{
label: __( 'Learn More' ),
url: addQueryArgs( 'post.php', {
post: post.id,
action: 'edit',
'classic-editor': '',
'cloudflare-error': '',
} ),
},
],
},
);
}
};

/**
Expand All @@ -292,7 +298,7 @@ export const trashPost = async ( action, store ) => {
const postTypeSlug = getCurrentPostType( getState() );
const postType = await resolveSelector( 'core', 'getPostType', postTypeSlug );

dispatch( removeNotice( TRASH_POST_NOTICE_ID ) );
dataDispatch( 'core/notices' ).removeNotice( TRASH_POST_NOTICE_ID );
try {
await apiFetch( { path: `/wp/v2/${ postType.rest_base }/${ postId }`, method: 'DELETE' } );
const post = getCurrentPost( getState() );
Expand All @@ -315,9 +321,11 @@ export const trashPost = async ( action, store ) => {
* @param {Object} action action object.
* @param {Object} store Redux Store.
*/
export const trashPostFailure = ( action, store ) => {
export const trashPostFailure = ( action ) => {
const message = action.error.message && action.error.code !== 'unknown_error' ? action.error.message : __( 'Trashing failed' );
store.dispatch( createErrorNotice( message, { id: TRASH_POST_NOTICE_ID } ) );
dataDispatch( 'core/notices' ).createErrorNotice( message, {
id: TRASH_POST_NOTICE_ID,
} );
};

/**
Expand Down
24 changes: 14 additions & 10 deletions packages/editor/src/store/effects/reusable-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ import {
cloneBlock,
} from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
// TODO: Ideally this would be the only dispatch in scope. This requires either
// refactoring editor actions to yielded controls, or replacing direct dispatch
// on the editor store with action creators (e.g. `REMOVE_REUSABLE_BLOCK`).
import { dispatch as dataDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import { resolveSelector } from './utils';
import {
receiveReusableBlocks as receiveReusableBlocksAction,
createSuccessNotice,
createErrorNotice,
removeBlocks,
replaceBlocks,
receiveBlocks,
Expand Down Expand Up @@ -131,13 +133,14 @@ export const saveReusableBlocks = async ( action, store ) => {
id,
} );
const message = isTemporary ? __( 'Block created.' ) : __( 'Block updated.' );
dispatch( createSuccessNotice( message, { id: REUSABLE_BLOCK_NOTICE_ID } ) );
dataDispatch( 'core/notices' ).createSuccessNotice( message, {
id: REUSABLE_BLOCK_NOTICE_ID,
} );
} catch ( error ) {
dispatch( { type: 'SAVE_REUSABLE_BLOCK_FAILURE', id } );
dispatch( createErrorNotice( error.message, {
dataDispatch( 'core/notices' ).createErrorNotice( error.message, {
id: REUSABLE_BLOCK_NOTICE_ID,
spokenMessage: error.message,
} ) );
} );
}
};

Expand Down Expand Up @@ -191,17 +194,18 @@ export const deleteReusableBlocks = async ( action, store ) => {
optimist: { type: COMMIT, id: transactionId },
} );
const message = __( 'Block deleted.' );
dispatch( createSuccessNotice( message, { id: REUSABLE_BLOCK_NOTICE_ID } ) );
dataDispatch( 'core/notices' ).createSuccessNotice( message, {
id: REUSABLE_BLOCK_NOTICE_ID,
} );
} catch ( error ) {
dispatch( {
type: 'DELETE_REUSABLE_BLOCK_FAILURE',
id,
optimist: { type: REVERT, id: transactionId },
} );
dispatch( createErrorNotice( error.message, {
dataDispatch( 'core/notices' ).createErrorNotice( error.message, {
id: REUSABLE_BLOCK_NOTICE_ID,
spokenMessage: error.message,
} ) );
} );
}
};

Expand Down
3 changes: 0 additions & 3 deletions packages/editor/src/store/effects/test/reusable-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
unregisterBlockType,
createBlock,
} from '@wordpress/blocks';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
Expand Down Expand Up @@ -40,7 +39,6 @@ import reducer from '../../reducer';
import '../../..';

jest.mock( '@wordpress/api-fetch', () => jest.fn() );
jest.mock( '@wordpress/deprecated', () => jest.fn() );

describe( 'reusable blocks effects', () => {
beforeAll( () => {
Expand Down Expand Up @@ -223,7 +221,6 @@ describe( 'reusable blocks effects', () => {
id: 123,
updatedId: 456,
} );
expect( deprecated ).toHaveBeenCalled();
} );

it( 'should handle an API error', async () => {
Expand Down
Loading

0 comments on commit 839c4ba

Please sign in to comment.