Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove the offset argument and leave the computation to the caller
  • Loading branch information
youknowriad committed Nov 5, 2018
1 parent b20ff16 commit 03102d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/date/CHANGELOG.md
@@ -1,4 +1,4 @@
## 2.1.1 (Unreleased)
## 2.2.0 (Unreleased)

### Deprecations

Expand Down
13 changes: 10 additions & 3 deletions packages/date/src/index.js
Expand Up @@ -392,9 +392,16 @@ export function dateI18n( dateFormat, dateValue = new Date(), gmt = false ) {
return format( dateFormat, dateMoment );
}

export function isInTheFuture( dateString, minutesOffset = 0 ) {
const now = momentLib.tz( 'WP' ).add( minutesOffset, 'minute' );
const momentObject = momentLib.tz( dateString, 'WP' );
/**
* Check whether a date is considered in the future according to the WordPress settings.
*
* @param {(Date|string)} dateValue Date object or string.
*
* @return {boolean} Is in the future.
*/
export function isInTheFuture( dateValue ) {
const now = momentLib.tz( 'WP' );
const momentObject = momentLib.tz( dateValue, 'WP' );

return momentObject.isAfter( now );
}
Expand Down
9 changes: 7 additions & 2 deletions packages/editor/src/store/selectors.js
Expand Up @@ -54,6 +54,7 @@ export const INSERTER_UTILITY_NONE = 0;
const MILLISECONDS_PER_HOUR = 3600 * 1000;
const MILLISECONDS_PER_DAY = 24 * 3600 * 1000;
const MILLISECONDS_PER_WEEK = 7 * 24 * 3600 * 1000;
const ONE_MINUTE_IN_MS = 60 * 1000;

/**
* Shared reference to an empty array for cases where it is important to avoid
Expand Down Expand Up @@ -323,7 +324,7 @@ export function isCurrentPostPublished( state ) {
const post = getCurrentPost( state );

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

/**
Expand Down Expand Up @@ -481,7 +482,11 @@ export function hasAutosave( state ) {
* @return {boolean} Whether the post has been published.
*/
export function isEditedPostBeingScheduled( state ) {
return isInTheFuture( getEditedPostAttribute( state, 'date' ), 1 );
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 );

return isInTheFuture( checkedDate );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/test/selectors.js
Expand Up @@ -1512,7 +1512,7 @@ describe( 'selectors', () => {

describe( 'isEditedPostBeingScheduled', () => {
it( 'should return true for posts with a future date', () => {
const time = Date.now() + ( 1000 * 3600 * 24 * 7 ); // sdays in the future
const time = Date.now() + ( 1000 * 3600 * 24 * 7 ); // 7 days in the future
const date = new Date( time );
const state = {
editor: {
Expand Down

0 comments on commit 03102d4

Please sign in to comment.