Skip to content

Commit

Permalink
Merge pull request #521 from Automattic/fix-block-editor-status
Browse files Browse the repository at this point in the history
Fix setting initial status in Gutenberg
  • Loading branch information
mjangda authored Nov 1, 2019
2 parents 883f834 + edeb99e commit 35a4d2e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion blocks/dist/custom-status.build.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 45 additions & 11 deletions blocks/src/custom-status/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import './style.scss';
let { __ } = wp.i18n;
let { PluginPostStatusInfo } = wp.editPost;
let { registerPlugin } = wp.plugins;
let { withSelect, withDispatch } = wp.data;
let { subscribe, dispatch, select, withSelect, withDispatch } = wp.data;
let { compose } = wp.compose;
let { SelectControl } = wp.components;

Expand All @@ -20,10 +20,36 @@ let getStatusLabel = slug => statuses.find( s => s.value === slug ).label;
let sideEffectL10nManipulation = status => {
let node = document.querySelector('.editor-post-save-draft');
if ( node ) {
document.querySelector('.editor-post-save-draft').innerText = `${__( 'Save' ) } ${status}`
document.querySelector( '.editor-post-save-draft' ).innerText = `${ __( 'Save' ) }`
}
}

// Set the status to the default custom status.
subscribe( function () {
const postId = select( 'core/editor' ).getCurrentPostId();
// Post isn't ready yet so don't do anything.
if ( ! postId ) {
return;
}

// For new posts, we need to force the our default custom status.
// Otherwise WordPress will force it to "Draft".
const isCleanNewPost = select( 'core/editor' ).isCleanNewPost();
if ( isCleanNewPost ) {
dispatch( 'core/editor' ).editPost( {
status: ef_default_custom_status
} );

return;
}

// Update the "Save" button.
var status = select( 'core/editor' ).getEditedPostAttribute( 'status' );
if ( typeof status !== 'undefined' && status !== 'publish' ) {
sideEffectL10nManipulation( getStatusLabel( status ) );
}
} );

/**
* Custom status component
* @param object props
Expand All @@ -47,17 +73,25 @@ let EditFlowCustomPostStati = ( { onUpdate, status } ) => (
</PluginPostStatusInfo>
);

let plugin = compose(
withSelect((select) => ({
const mapSelectToProps = ( select ) => {
return {
status: select('core/editor').getEditedPostAttribute('status'),
})),
withDispatch((dispatch) => ({
onUpdate(status) {
dispatch('core/editor').editPost( { status } );
};
};

const mapDispatchToProps = ( dispatch ) => {
return {
onUpdate( status ) {
dispatch( 'core/editor' ).editPost( { status } );
sideEffectL10nManipulation( getStatusLabel( status ) );
}
}))
)(EditFlowCustomPostStati);
},
};
};

let plugin = compose(
withSelect( mapSelectToProps ),
withDispatch( mapDispatchToProps )
)( EditFlowCustomPostStati );

/**
* Kick it off
Expand Down

0 comments on commit 35a4d2e

Please # to comment.