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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/designers-developers/developers/data/data-core-editor.md
Expand Up @@ -892,6 +892,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
22 changes: 22 additions & 0 deletions packages/editor/src/store/reducer.js
Expand Up @@ -238,6 +238,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 Expand Up @@ -393,4 +414,5 @@ export default optimist( combineReducers( {
postSavingLock,
isReady,
editorSettings,
postAutosavingLock,
} ) );
16 changes: 16 additions & 0 deletions packages/editor/src/store/selectors.js
Expand Up @@ -535,6 +535,11 @@ export const isEditedPostAutosaveable = createRegistrySelector( ( select ) => fu
return false;
}

// A post is not autosavable when there is a post autosave lock.
if ( isPostAutosavingLocked( state ) ) {
return false;
}

const postType = getCurrentPostType( state );
const postId = getCurrentPostId( state );
const hasFetchedAutosave = select( 'core' ).hasFetchedAutosaves( postType, postId );
Expand Down Expand Up @@ -1100,6 +1105,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
Expand Up @@ -15,6 +15,7 @@ import {
saving,
reusableBlocks,
postSavingLock,
postAutosavingLock,
} from '../reducer';

describe( 'state', () => {
Expand Down Expand Up @@ -489,4 +490,49 @@ describe( 'state', () => {
expect( state ).toEqual( {} );
} );
} );

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( {} );
} );
} );
} );
37 changes: 37 additions & 0 deletions packages/editor/src/store/test/selectors.js
Expand Up @@ -159,6 +159,7 @@ const {
getPermalink,
getPermalinkParts,
isPostSavingLocked,
isPostAutosavingLocked,
canUserUseUnfilteredHTML,
} = selectors;

Expand Down Expand Up @@ -1157,6 +1158,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 Expand Up @@ -1408,6 +1431,7 @@ describe( 'selectors', () => {
return true;
},
getAutosave() {},
postAutosavingLock: {},
};

expect( isEditedPostAutosaveable( state ) ).toBe( true );
Expand Down Expand Up @@ -1440,6 +1464,7 @@ describe( 'selectors', () => {
excerpt: 'foo',
};
},
postAutosavingLock: {},
};

expect( isEditedPostAutosaveable( state ) ).toBe( false );
Expand Down Expand Up @@ -1471,6 +1496,7 @@ describe( 'selectors', () => {
excerpt: 'foo',
};
},
postAutosavingLock: {},
};

expect( isEditedPostAutosaveable( state ) ).toBe( true );
Expand Down Expand Up @@ -1505,12 +1531,23 @@ describe( 'selectors', () => {
[ variantField ]: 'bar',
};
},
postAutosavingLock: {},
};

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

it( 'should return false if autosaving is locked', () => {
const state = {
currentPost: {},
saving: {},
postAutosavingLock: { example: true },
};

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

describe( 'isEditedPostEmpty', () => {
Expand Down