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

Fix the isBeingScheduled Selector. #11572

Merged
merged 4 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 16 additions & 1 deletion packages/date/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export function dateI18n( dateFormat, dateValue = new Date(), gmt = false ) {
/**
* Check whether a date is considered in the future according to the WordPress settings.
*
* @param {(Date|string)} dateValue Date object or string.
* @param {string} dateValue Date String or Date object in the Defined WP Timezone.
Copy link
Member

Choose a reason for hiding this comment

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

So is it a string or an object? The current documentation here sends mixed messaging.

*
* @return {boolean} Is in the future.
*/
Expand All @@ -406,4 +406,19 @@ export function isInTheFuture( dateValue ) {
return momentObject.isAfter( now );
}

/**
* Create and return a JavaScript Date Object from a date string in the WP timezone.
*
* @param {string?} dateString Date formatted in the WP timezone.
*
* @return {Date} Date
*/
export function getDate( dateString ) {
if ( ! dateString ) {
return momentLib.tz( 'WP' ).toDate();
}

return momentLib.tz( dateString, 'WP' ).toDate();
}

setupWPTimezone();
40 changes: 40 additions & 0 deletions packages/date/src/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Internal dependencies
*/
import { isInTheFuture, getDate, setSettings, __experimentalGetSettings } from '../';

describe( 'isInTheFuture', () => {
it( 'should return true if the date is in the future', () => {
// Create a Date object 1 minute in the future.
const date = new Date( Number( getDate() ) + ( 1000 * 60 ) );

expect( isInTheFuture( date ) ).toBe( true );
} );

it( 'should return true if the date is in the past', () => {
// Create a Date object 1 minute in the past.
const date = new Date( Number( getDate() ) - ( 1000 * 60 ) );

expect( isInTheFuture( date ) ).toBe( false );
} );

it( 'should ignore the timezone', () => {
const settings = __experimentalGetSettings();

// Set a timezone in the future
setSettings( {
...settings,
timezone: { offset: '4', string: '' },
} );
// Create a Date object 1 minute in the past.
let date = new Date( Number( getDate() ) - ( 1000 * 60 ) );
expect( isInTheFuture( date ) ).toBe( false );

// Create a Date object 1 minute in the future.
date = new Date( Number( getDate() ) + ( 1000 * 60 ) );
expect( isInTheFuture( date ) ).toBe( true );

// Restore default settings
setSettings( settings );
} );
} );
6 changes: 3 additions & 3 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
getFreeformContentHandlerName,
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';
import { isInTheFuture } from '@wordpress/date';
import { isInTheFuture, getDate } from '@wordpress/date';
import { removep } from '@wordpress/autop';
import { select } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
Expand Down Expand Up @@ -333,7 +333,7 @@ export function isCurrentPostPublished( state ) {
const post = getCurrentPost( state );

return [ 'publish', 'private' ].indexOf( post.status ) !== -1 ||
( post.status === 'future' && ! isInTheFuture( new Date( Number( new Date( post.date ) ) + ONE_MINUTE_IN_MS ) ) );
( post.status === 'future' && ! isInTheFuture( new Date( Number( getDate( post.date ) ) - ONE_MINUTE_IN_MS ) ) );
}

/**
Expand Down Expand Up @@ -493,7 +493,7 @@ export function hasAutosave( state ) {
export function isEditedPostBeingScheduled( state ) {
const date = getEditedPostAttribute( state, 'date' );
// Offset the date by one minute (network latency)
const checkedDate = new Date( Number( new Date( date ) ) + ONE_MINUTE_IN_MS );
const checkedDate = new Date( Number( getDate( date ) ) - ONE_MINUTE_IN_MS );

return isInTheFuture( checkedDate );
}
Expand Down