Skip to content

Commit

Permalink
Show "Publish: Immediately" for new drafts by inferring floating date (
Browse files Browse the repository at this point in the history
…#9967)

* Show "Publish: Immediately" for new drafts by inferring floating date

* Add isEditedPostDateFloating selector

Move logic to determine whether a post should be considered to have a
floating date from the PostScheduleLabel component into a new selector
isEditedPostDateFloating.

* Rename "floating" component property to "isFloating" for type clarity
  • Loading branch information
kadamwhite authored and youknowriad committed Oct 2, 2018
1 parent 78d1254 commit af53caa
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/editor/src/components/post-schedule/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ 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' );
}

export default withSelect( ( select ) => {
return {
date: select( 'core/editor' ).getEditedPostAttribute( 'date' ),
isFloating: select( 'core/editor' ).isEditedPostDateFloating(),
};
} )( PostScheduleLabel );
28 changes: 28 additions & 0 deletions packages/editor/src/components/post-schedule/test/label.js
Original file line number Diff line number Diff line change
@@ -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( <PostScheduleLabel date={ undefined } /> );
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( <PostScheduleLabel date={ date } isFloating={ true } /> );
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( <PostScheduleLabel date={ date } isFloating={ false } /> );
expect( wrapper.text() ).not.toBe( 'Immediately' );
} );
} );
23 changes: 23 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,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
Expand Down
78 changes: 78 additions & 0 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {
hasAutosave,
isEditedPostEmpty,
isEditedPostBeingScheduled,
isEditedPostDateFloating,
getBlockDependantsCacheBust,
getBlockName,
getBlock,
Expand Down Expand Up @@ -1234,6 +1235,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 ];
Expand Down

0 comments on commit af53caa

Please sign in to comment.