Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, floating } ) {
const settings = getSettings();
return date ?
return date && ! floating ?
dateI18n( settings.formats.datetime, date ) :
__( 'Immediately' );
}

export default withSelect( ( select ) => {
return {
date: select( 'core/editor' ).getEditedPostAttribute( 'date' ),
kadamwhite marked this conversation as resolved.
Show resolved Hide resolved
floating: select( 'core/editor' ).isEditedPostDateFloating(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: maybe isFloating to clarify that it's a boolean?

};
} )( 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 } floating={ 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 } floating={ 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 @@ -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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this new selector to generate some changes in the docs if you run npm run docs:build. Can you commit these changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect this new selector to generate some changes in the docs if you run npm run docs:build. Can you commit these changes?

This was never done and there are local changes to commit on master after running the command.

Please review #10234

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 my bad, @aduth , hadn't had time to circle back to this and had missed that piece of feedback. I'll know for next time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries @kadamwhite

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 @@ -38,6 +38,7 @@ const {
hasAutosave,
isEditedPostEmpty,
isEditedPostBeingScheduled,
isEditedPostDateFloating,
getBlockDependantsCacheBust,
getBlockName,
getBlock,
Expand Down Expand Up @@ -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 ];
Expand Down