Skip to content

Commit

Permalink
Add a post autosave lock (#16249)
Browse files Browse the repository at this point in the history
* add post autosave locking

* A post is not autosavable when there is a post autosave lock.

* A post is not autosavable when there is a post autosave lock.

* Update packages/editor/src/store/selectors.js

Co-Authored-By: Grzegorz (Greg) Ziółkowski <grzegorz@gziolo.pl>

* expect isEditedPostAutosaveable to return false when autosaveLock is enabled

* Update packages/editor/src/components/autosave-monitor/index.js

Co-Authored-By: Grzegorz (Greg) Ziółkowski <grzegorz@gziolo.pl>

* fix linting issue

* remove unused isPostAutosavingLocked reference

* add postAutosavingLock to test state

* remove removed test
  • Loading branch information
Adam Silverstein authored Sep 5, 2019
1 parent 80db093 commit 1eca362
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
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 @@ -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
Original file line number Diff line number Diff line change
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 ) {
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 );
} );
} );

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

0 comments on commit 1eca362

Please sign in to comment.