Skip to content

Commit

Permalink
Feature: save lock control via actions (#10649)
Browse files Browse the repository at this point in the history
* Add new actions for lockPostSaving and unlockPostSaving

* isPostSavingLocked selector

* postSavingLock reducer

* check isPostSavingLocked in post publish button

* simplify reducer with object

* adjust isPostSavingLocked for new state shape

* build docs

* postSavingLock - initial state is an empty object

* ensure the isPostSavingLocked selector returns a boolean

* Add tests for isPostSavingLocked

* Add tests for the postSavingLock reducer

* postSavingLock: deepFreeze( state ) passed in tests

* simplify reducers, selectors & adjust tests

* move lockName into case handlers
  • Loading branch information
Adam Silverstein authored and gziolo committed Oct 26, 2018
1 parent a696e50 commit ac81cf7
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 2 deletions.
30 changes: 29 additions & 1 deletion docs/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,18 @@ Returns whether the post is locked.

Is locked.

### isPostSavingLocked

Returns whether post saving is locked.

*Parameters*

* state: Global application state.

*Returns*

Is locked.

### isPostLockTakeover

Returns whether the edition of the post has been taken over.
Expand Down Expand Up @@ -1752,4 +1764,20 @@ Returns an action object used in signalling that the user has enabled the publis

### disablePublishSidebar

Returns an action object used in signalling that the user has disabled the publish sidebar.
Returns an action object used in signalling that the user has disabled the publish sidebar.

### lockPostSaving

Returns an action object used to signal that post saving is locked.

*Parameters*

* lockName: The lock name.

### unlockPostSaving

Returns an action object used to signal that post saving is unlocked.

*Parameters*

* lockName: The lock name.
3 changes: 2 additions & 1 deletion packages/editor/src/components/post-publish-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ export default compose( [
getEditedPostVisibility,
isEditedPostSaveable,
isEditedPostPublishable,
isPostSavingLocked,
getCurrentPost,
getCurrentPostType,
} = select( 'core/editor' );
return {
isSaving: forceIsSaving || isSavingPost(),
isBeingScheduled: isEditedPostBeingScheduled(),
visibility: getEditedPostVisibility(),
isSaveable: isEditedPostSaveable(),
isSaveable: isEditedPostSaveable() && ! isPostSavingLocked(),
isPublishable: forceIsDirty || isEditedPostPublishable(),
hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ),
postType: getCurrentPostType(),
Expand Down
29 changes: 29 additions & 0 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,32 @@ export function disablePublishSidebar() {
type: 'DISABLE_PUBLISH_SIDEBAR',
};
}

/**
* Returns an action object used to signal that post saving is locked.
*
* @param {string} lockName The lock name.
*
* @return {Object} Action object
*/
export function lockPostSaving( lockName ) {
return {
type: 'LOCK_POST_SAVING',
lockName,
};
}

/**
* Returns an action object used to signal that post saving is unlocked.
*
* @param {string} lockName The lock name.
*
* @return {Object} Action object
*/
export function unlockPostSaving( lockName ) {
return {
type: 'UNLOCK_POST_SAVING',
lockName,
};
}

22 changes: 22 additions & 0 deletions packages/editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,27 @@ export function postLock( state = { isLocked: false }, action ) {
return state;
}

/**
* Post saving lock.
*
* When post saving is locked, the post cannot be published or updated.
*
* @param {PostSavingLockState} state Current state.
* @param {Object} action Dispatched action.
*
* @return {PostLockState} Updated state.
*/
export function postSavingLock( state = {}, action ) {
switch ( action.type ) {
case 'LOCK_POST_SAVING':
return { ...state, [ action.lockName ]: true };

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

export const reusableBlocks = combineReducers( {
data( state = {}, action ) {
switch ( action.type ) {
Expand Down Expand Up @@ -1133,4 +1154,5 @@ export default optimist( combineReducers( {
autosave,
settings,
tokens,
postSavingLock,
} ) );
11 changes: 11 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,17 @@ export function isPostLocked( state ) {
return state.postLock.isLocked;
}

/**
* Returns whether post saving is locked.
*
* @param {Object} state Global application state.
*
* @return {boolean} Is locked.
*/
export function isPostSavingLocked( state ) {
return state.postSavingLock.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 @@ -35,6 +35,7 @@ import {
template,
blockListSettings,
autosave,
postSavingLock,
} from '../reducer';

describe( 'state', () => {
Expand Down Expand Up @@ -2263,4 +2264,49 @@ describe( 'state', () => {
} );
} );
} );

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

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

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

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

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

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

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

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

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

expect( state ).toEqual( {} );
} );
} );
} );
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 @@ -111,6 +111,7 @@ const {
INSERTER_UTILITY_HIGH,
INSERTER_UTILITY_MEDIUM,
INSERTER_UTILITY_LOW,
isPostSavingLocked,
} = selectors;

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

describe( 'isPostSavingLocked', () => {
it( 'should return true if the post has postSavingLocks', () => {
const state = {
postSavingLock: [ { 1: true } ],
currentPost: {},
saving: {},
};

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

it( 'should return false if the post has no postSavingLocks', () => {
const state = {
postSavingLock: [],
currentPost: {},
saving: {},
};

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

describe( 'isEditedPostSaveable', () => {
it( 'should return false if the post has no title, excerpt, content', () => {
const state = {
Expand Down

0 comments on commit ac81cf7

Please sign in to comment.