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 1 commit
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
20 changes: 18 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,31 @@ import { __ } from '@wordpress/i18n';
import { dateI18n, getSettings } from '@wordpress/date';
import { withSelect } from '@wordpress/data';

function PostScheduleLabel( { date } ) {
// Unlike in the PHP backend, in the REST API response draft posts for which a
// date has not yet been set return a full gmt date string instead of the empty
// 0000-00-00 00:00:00 placeholder used in the database. To infer that a post
// is set to publish "immediately" we check whether the date and modified date
// are matching values.
function isFloatingDate( { date, modified, status } ) {
if ( status === 'draft' || status === 'auto-draft' ) {
return date === modified;
}
return false;
}

export function PostScheduleLabel( props ) {
const { date } = props;
const floating = isFloatingDate( props );
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
modified: select( 'core/editor' ).getEditedPostAttribute( 'modified' ),
status: select( 'core/editor' ).getEditedPostAttribute( 'status' ),
};
} )( PostScheduleLabel );
35 changes: 35 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,35 @@
/**
* 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 in draft status and the date and modified date match', () => {
const date = '2018-09-17T01:23:45.678Z';
const wrapper = shallow( <PostScheduleLabel date={ date } modified={ date } status={ 'draft' } /> );
expect( wrapper.text() ).toBe( 'Immediately' );
} );

it( 'should show the post will be published immediately if in auto-draft status and the date and modified date match', () => {
const date = '2018-09-17T01:23:45.678Z';
const wrapper = shallow( <PostScheduleLabel date={ date } modified={ date } status={ 'auto-draft' } /> );
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 modified = '2018-08-01T00:00:00.000Z';
const wrapper = shallow( <PostScheduleLabel date={ date } modified={ modified } status={ 'draft' } /> );
expect( wrapper.text() ).not.toBe( 'Immediately' );
} );
} );