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

Add a post autosave lock #16249

Merged
merged 19 commits into from
Sep 5, 2019
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
12 changes: 12 additions & 0 deletions docs/designers-developers/developers/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,18 @@ _Returns_

- `boolean`: Whether or not the permalink is editable.

<a name="isPostAutosavingLocked" href="#isPostAutosavingLocked">#</a> **isPostAutosavingLocked**

Returns whether post autosaving is locked.

_Parameters_

- _state_ `Object`: Global application state.

_Returns_

- `boolean`: Is locked.

<a name="isPostLocked" href="#isPostLocked">#</a> **isPostLocked**

Returns whether the post is locked.
Expand Down
21 changes: 21 additions & 0 deletions packages/editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,27 @@ export function postSavingLock( state = {}, action ) {
return state;
}

/**
* Post autosaving lock.
*
* When post autosaving is locked, the post will not autosave.
*
* @param {PostAutosavingLockState} state Current state.
* @param {Object} action Dispatched action.
*
* @return {PostLockState} Updated state.
*/
export function postAutosavingLock( state = {}, action ) {
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
switch ( action.type ) {
case 'LOCK_POST_AUTOSAVING':
return { ...state, [ action.lockName ]: true };

case 'UNLOCK_POST_AUTOSAVING':
return omit( state, action.lockName );
}
return state;
}

export const reusableBlocks = combineReducers( {
data( state = {}, action ) {
switch ( action.type ) {
Expand Down
16 changes: 16 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ export const isEditedPostAutosaveable = createRegistrySelector( ( select ) => fu
return false;
}

// A post is not autosavable when there is a post autosave lock.
if ( isPostAutosavingLocked() ) {
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

const postType = getCurrentPostType( state );
const postId = getCurrentPostId( state );
const hasFetchedAutosave = select( 'core' ).hasFetchedAutosaves( postType, postId );
Expand Down Expand Up @@ -1064,6 +1069,17 @@ export function isPostSavingLocked( state ) {
return Object.keys( state.postSavingLock ).length > 0;
}

/**
* Returns whether post autosaving is locked.
*
* @param {Object} state Global application state.
*
* @return {boolean} Is locked.
*/
export function isPostAutosavingLocked( state ) {
return Object.keys( state.postAutosavingLock ).length > 0;
}

/**
* Returns whether the edition of the post has been taken over.
*
Expand Down
46 changes: 46 additions & 0 deletions packages/editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
saving,
reusableBlocks,
postSavingLock,
postAutosavingLock,
previewLink,
} from '../reducer';
import { INITIAL_EDITS_DEFAULTS } from '../defaults';
Expand Down Expand Up @@ -847,6 +848,51 @@ describe( 'state', () => {
} );
} );

describe( 'postAutosavingLock', () => {
it( 'returns empty object by default', () => {
const state = postAutosavingLock( undefined, {} );

expect( state ).toEqual( {} );
} );

it( 'returns correct post locks when locks added and removed', () => {
let state = postAutosavingLock( undefined, {
type: 'LOCK_POST_AUTOSAVING',
lockName: 'test-lock',
} );

expect( state ).toEqual( {
'test-lock': true,
} );

state = postAutosavingLock( deepFreeze( state ), {
type: 'LOCK_POST_AUTOSAVING',
lockName: 'test-lock-2',
} );

expect( state ).toEqual( {
'test-lock': true,
'test-lock-2': true,
} );

state = postAutosavingLock( deepFreeze( state ), {
type: 'UNLOCK_POST_AUTOSAVING',
lockName: 'test-lock',
} );

expect( state ).toEqual( {
'test-lock-2': true,
} );

state = postAutosavingLock( deepFreeze( state ), {
type: 'UNLOCK_POST_AUTOSAVING',
lockName: 'test-lock-2',
} );

expect( state ).toEqual( {} );
} );
} );

describe( 'previewLink', () => {
it( 'returns null by default', () => {
const state = previewLink( undefined, {} );
Expand Down
23 changes: 23 additions & 0 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const {
getPermalink,
getPermalinkParts,
isPostSavingLocked,
isPostAutosavingLocked,
canUserUseUnfilteredHTML,
} = selectors;

Expand Down Expand Up @@ -1097,6 +1098,28 @@ describe( 'selectors', () => {
} );
} );

describe( 'isPostAutosavingLocked', () => {
it( 'should return true if the post has postAutosavingLocks', () => {
const state = {
postAutosavingLock: { example: true },
currentPost: {},
saving: {},
};

expect( isPostAutosavingLocked( state ) ).toBe( true );
} );

it( 'should return false if the post has no postAutosavingLocks', () => {
const state = {
postAutosavingLock: {},
currentPost: {},
saving: {},
};

expect( isPostAutosavingLocked( state ) ).toBe( false );
} );
} );

adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
describe( 'isEditedPostSaveable', () => {
it( 'should return false if the post has no title, excerpt, content', () => {
const state = {
Expand Down