diff --git a/src/editor/post-status-slot/index.js b/src/editor/post-status-slot/index.js index 29ed05e8d..f9649b3c3 100644 --- a/src/editor/post-status-slot/index.js +++ b/src/editor/post-status-slot/index.js @@ -7,30 +7,48 @@ import { WebPreview } from 'newspack-components'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { withSelect } from '@wordpress/data'; +import { withSelect, withDispatch } from '@wordpress/data'; +import { compose } from '@wordpress/compose'; import { PluginPostStatusInfo } from '@wordpress/edit-post'; import { registerPlugin } from '@wordpress/plugins'; import { Button } from '@wordpress/components'; +import { useState } from '@wordpress/element'; -const StatusInfoPreviewButton = ( { url } ) => - url ? ( +const StatusInfoPreviewButton = ( { url, savePost } ) => { + const [ inFlight, setInFlight ] = useState( false ); + const handleClick = showPreview => async () => { + setInFlight( true ); + await savePost(); + showPreview(); + setInFlight( false ); + }; + return ( ( - ) } /> - ) : null; + ); +}; -const StatusInfoPreviewButtonWithSelect = withSelect( select => { - const { getEditedPostAttribute } = select( 'core/editor' ); - const meta = getEditedPostAttribute( 'meta' ); - return { url: meta.campaign && meta.campaign.long_archive_url }; -} )( StatusInfoPreviewButton ); +const StatusInfoPreviewButtonWithSelect = compose( [ + withSelect( select => { + const { getEditedPostAttribute } = select( 'core/editor' ); + const meta = getEditedPostAttribute( 'meta' ); + return { url: meta.campaign && meta.campaign.long_archive_url }; + } ), + withDispatch( dispatch => { + const { savePost } = dispatch( 'core/editor' ); + return { + savePost, + }; + } ), +] )( StatusInfoPreviewButton ); export default () => { registerPlugin( 'newspack-newsletters-post-status-info', { diff --git a/src/editor/testing/index.js b/src/editor/testing/index.js index d4cb870a4..92e7f7dc5 100644 --- a/src/editor/testing/index.js +++ b/src/editor/testing/index.js @@ -3,19 +3,29 @@ */ import { __ } from '@wordpress/i18n'; import apiFetch from '@wordpress/api-fetch'; -import { withSelect } from '@wordpress/data'; +import { withSelect, withDispatch } from '@wordpress/data'; +import { compose } from '@wordpress/compose'; import { Fragment, useState } from '@wordpress/element'; import { Button, TextControl } from '@wordpress/components'; import { hasValidEmail } from '../utils'; -export default withSelect( select => { - const { getCurrentPostId } = select( 'core/editor' ); - return { postId: getCurrentPostId() }; -} )( ( { postId } ) => { +export default compose( [ + withSelect( select => { + const { getCurrentPostId } = select( 'core/editor' ); + return { postId: getCurrentPostId() }; + } ), + withDispatch( dispatch => { + const { savePost } = dispatch( 'core/editor' ); + return { + savePost, + }; + } ), +] )( ( { postId, savePost } ) => { const [ inFlight, setInFlight ] = useState( false ); const [ testEmail, setTestEmail ] = useState( '' ); - const sendMailchimpTest = () => { + const sendMailchimpTest = async () => { setInFlight( true ); + await savePost(); const params = { path: `/newspack-newsletters/v1/mailchimp/${ postId }/test`, data: {