Skip to content

Commit

Permalink
feat: send button support for pending state
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonrabb committed Apr 23, 2020
1 parent 6fcbefe commit db15bc6
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/components/send-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import { compose } from '@wordpress/compose';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* External dependencies
*/
import { get } from 'lodash';

export default compose( [
withDispatch( dispatch => {
const { editPost, savePost } = dispatch( 'core/editor' );
return { editPost, savePost };
} ),
withSelect( ( select, { forceIsSaving, forceIsDirty } ) => {
const {
getCurrentPost,
getEditedPostAttribute,
getEditedPostVisibility,
isEditedPostPublishable,
isEditedPostSaveable,
isSavingPost,
Expand All @@ -27,6 +34,8 @@ export default compose( [
validationErrors: meta.campaignValidationErrors,
status: getEditedPostAttribute( 'status' ),
isEditedPostBeingScheduled: isEditedPostBeingScheduled(),
hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ),
visibility: getEditedPostVisibility(),
};
} ),
] )(
Expand All @@ -39,6 +48,8 @@ export default compose( [
status,
validationErrors,
isEditedPostBeingScheduled,
hasPublishAction,
visibility,
} ) => {
const isButtonEnabled =
( isPublishable || isEditedPostBeingScheduled ) &&
Expand All @@ -59,8 +70,20 @@ export default compose( [
} else {
label = __( 'Send', 'newspack-newsletters' );
}

let publishStatus;
if ( ! hasPublishAction ) {
publishStatus = 'pending';
} else if ( visibility === 'private' ) {
publishStatus = 'private';
} else if ( isEditedPostBeingScheduled ) {
publishStatus = 'future';
} else {
publishStatus = 'publish';
}

const onClick = () => {
editPost( { status: isEditedPostBeingScheduled ? 'future' : 'publish' } );
editPost( { status: publishStatus } );
savePost();
};

Expand Down

0 comments on commit db15bc6

Please sign in to comment.