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

Feature: save lock control via actions #10649

Merged
merged 20 commits into from
Oct 26, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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':
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
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,
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
} ) );
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