diff --git a/packages/editor/src/components/post-schedule/label.js b/packages/editor/src/components/post-schedule/label.js index dc4b0cd7233a69..dce2969b49c612 100644 --- a/packages/editor/src/components/post-schedule/label.js +++ b/packages/editor/src/components/post-schedule/label.js @@ -5,9 +5,9 @@ import { __ } from '@wordpress/i18n'; import { dateI18n, getSettings } from '@wordpress/date'; import { withSelect } from '@wordpress/data'; -function PostScheduleLabel( { date } ) { +export function PostScheduleLabel( { date, isFloating } ) { const settings = getSettings(); - return date ? + return date && ! isFloating ? dateI18n( settings.formats.datetime, date ) : __( 'Immediately' ); } @@ -15,5 +15,6 @@ function PostScheduleLabel( { date } ) { export default withSelect( ( select ) => { return { date: select( 'core/editor' ).getEditedPostAttribute( 'date' ), + isFloating: select( 'core/editor' ).isEditedPostDateFloating(), }; } )( PostScheduleLabel ); diff --git a/packages/editor/src/components/post-schedule/test/label.js b/packages/editor/src/components/post-schedule/test/label.js new file mode 100644 index 00000000000000..f1d063a0d0fcdf --- /dev/null +++ b/packages/editor/src/components/post-schedule/test/label.js @@ -0,0 +1,28 @@ +/** + * External dependencies + */ +import { shallow } from 'enzyme'; + +/** + * Internal dependencies + */ +import { PostScheduleLabel } from '../label'; + +describe( 'PostScheduleLabel', () => { + it( 'should show the post will be published immediately if no publish date is set', () => { + const wrapper = shallow( ); + expect( wrapper.text() ).toBe( 'Immediately' ); + } ); + + it( 'should show the post will be published immediately if it has a floating date', () => { + const date = '2018-09-17T01:23:45.678Z'; + const wrapper = shallow( ); + expect( wrapper.text() ).toBe( 'Immediately' ); + } ); + + it( 'should show the scheduled publish date if a date has been set', () => { + const date = '2018-09-17T01:23:45.678Z'; + const wrapper = shallow( ); + expect( wrapper.text() ).not.toBe( 'Immediately' ); + } ); +} ); diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index 81bc282d774e65..b350410925426a 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -427,6 +427,29 @@ export function isEditedPostBeingScheduled( state ) { return date.isAfter( now ); } +/** + * Returns whether the current post should be considered to have a "floating" + * date (i.e. that it would publish "Immediately" rather than at a set time). + * + * Unlike in the PHP backend, the REST API returns a full date string for posts + * where the 0000-00-00T00:00:00 placeholder is present in the database. To + * infer that a post is set to publish "Immediately" we check whether the date + * and modified date are the same. + * + * @param {Object} state Editor state. + * + * @return {boolean} Whether the edited post has a floating date value. + */ +export function isEditedPostDateFloating( state ) { + const date = getEditedPostAttribute( state, 'date' ); + const modified = getEditedPostAttribute( state, 'modified' ); + const status = getEditedPostAttribute( state, 'status' ); + if ( status === 'draft' || status === 'auto-draft' ) { + return date === modified; + } + return false; +} + /** * Returns a new reference when the inner blocks of a given block client ID * change. This is used exclusively as a memoized selector dependant, relying diff --git a/packages/editor/src/store/test/selectors.js b/packages/editor/src/store/test/selectors.js index 60bde150d492ab..2bc2a77e61d73b 100644 --- a/packages/editor/src/store/test/selectors.js +++ b/packages/editor/src/store/test/selectors.js @@ -38,6 +38,7 @@ const { hasAutosave, isEditedPostEmpty, isEditedPostBeingScheduled, + isEditedPostDateFloating, getBlockDependantsCacheBust, getBlockName, getBlock, @@ -1221,6 +1222,83 @@ describe( 'selectors', () => { } ); } ); + describe( 'isEditedPostDateFloating', () => { + let editor; + + beforeEach( () => { + editor = { + present: { + edits: {}, + }, + }; + } ); + + it( 'should return true for draft posts where the date matches the modified date', () => { + const state = { + currentPost: { + date: '2018-09-27T01:23:45.678Z', + modified: '2018-09-27T01:23:45.678Z', + status: 'draft', + }, + editor, + }; + + expect( isEditedPostDateFloating( state ) ).toBe( true ); + } ); + + it( 'should return true for auto-draft posts where the date matches the modified date', () => { + const state = { + currentPost: { + date: '2018-09-27T01:23:45.678Z', + modified: '2018-09-27T01:23:45.678Z', + status: 'auto-draft', + }, + editor, + }; + + expect( isEditedPostDateFloating( state ) ).toBe( true ); + } ); + + it( 'should return false for draft posts where the date does not match the modified date', () => { + const state = { + currentPost: { + date: '2018-09-27T01:23:45.678Z', + modified: '1970-01-01T00:00:00.000Z', + status: 'draft', + }, + editor, + }; + + expect( isEditedPostDateFloating( state ) ).toBe( false ); + } ); + + it( 'should return false for auto-draft posts where the date does not match the modified date', () => { + const state = { + currentPost: { + date: '2018-09-27T01:23:45.678Z', + modified: '1970-01-01T00:00:00.000Z', + status: 'auto-draft', + }, + editor, + }; + + expect( isEditedPostDateFloating( state ) ).toBe( false ); + } ); + + it( 'should return false for published posts', () => { + const state = { + currentPost: { + date: '2018-09-27T01:23:45.678Z', + modified: '2018-09-27T01:23:45.678Z', + status: 'publish', + }, + editor, + }; + + expect( isEditedPostDateFloating( state ) ).toBe( false ); + } ); + } ); + describe( 'getBlockDependantsCacheBust', () => { const rootBlock = { clientId: 123, name: 'core/paragraph', attributes: {} }; const rootOrder = [ 123 ];