-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Components: Extract Reusable Post Sticky Component #3114
Merged
+177
−101
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import { flowRight } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withAPIData } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getCurrentPostType } from '../selectors'; | ||
|
||
export function PostStickyCheck( { postType, children, user } ) { | ||
if ( | ||
postType !== 'post' || | ||
! user.data || | ||
! user.data.capabilities.publish_posts || | ||
! user.data.capabilities.edit_others_posts | ||
) { | ||
return null; | ||
} | ||
|
||
return children; | ||
} | ||
|
||
const applyConnect = connect( | ||
( state ) => { | ||
return { | ||
postType: getCurrentPostType( state ), | ||
}; | ||
}, | ||
); | ||
|
||
const applyWithAPIData = withAPIData( () => { | ||
return { | ||
user: '/wp/v2/users/me?context=edit', | ||
}; | ||
} ); | ||
|
||
export default flowRight( [ | ||
applyConnect, | ||
applyWithAPIData, | ||
] )( PostStickyCheck ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import { flowRight } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { FormToggle, withInstanceId } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getEditedPostAttribute } from '../selectors'; | ||
import { editPost } from '../actions'; | ||
import PostStickyCheck from './check'; | ||
|
||
export function PostSticky( { onUpdateSticky, postSticky = false, instanceId } ) { | ||
const stickyToggleId = 'post-sticky-toggle-' + instanceId; | ||
|
||
return ( | ||
<PostStickyCheck> | ||
<label htmlFor={ stickyToggleId }>{ __( 'Stick to the Front Page' ) }</label> | ||
<FormToggle | ||
key="toggle" | ||
checked={ postSticky } | ||
onChange={ () => onUpdateSticky( ! postSticky ) } | ||
showHint={ false } | ||
id={ stickyToggleId } | ||
/> | ||
</PostStickyCheck> | ||
); | ||
} | ||
|
||
const applyConnect = connect( | ||
( state ) => { | ||
return { | ||
postSticky: getEditedPostAttribute( state, 'sticky' ), | ||
}; | ||
}, | ||
( dispatch ) => { | ||
return { | ||
onUpdateSticky( postSticky ) { | ||
dispatch( editPost( { sticky: postSticky } ) ); | ||
}, | ||
}; | ||
}, | ||
); | ||
|
||
export default flowRight( [ | ||
applyConnect, | ||
withInstanceId, | ||
] )( PostSticky ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PostStickyCheck } from '../check'; | ||
|
||
describe( 'PostSticky', () => { | ||
const user = { | ||
data: { | ||
capabilities: { | ||
edit_others_posts: true, | ||
publish_posts: true, | ||
}, | ||
}, | ||
}; | ||
|
||
it( 'should not render anything if the post type is not "post"', () => { | ||
const wrapper = shallow( | ||
<PostStickyCheck postType="page" user={ user }> | ||
Can Toggle Sticky | ||
</PostStickyCheck> | ||
); | ||
expect( wrapper.type() ).toBe( null ); | ||
} ); | ||
|
||
it( 'should not render anything if the user doesn\'t have the right capabilities', () => { | ||
let wrapper = shallow( | ||
<PostStickyCheck postType="post" user={ {} }> | ||
Can Toggle Sticky | ||
</PostStickyCheck> | ||
); | ||
expect( wrapper.type() ).toBe( null ); | ||
|
||
wrapper = shallow( | ||
<PostStickyCheck postType="post" user={ | ||
{ data: { capabilities: { edit_others_posts: false, publish_posts: true } } } | ||
}> | ||
Can Toggle Sticky | ||
</PostStickyCheck> | ||
); | ||
expect( wrapper.type() ).toBe( null ); | ||
|
||
wrapper = shallow( | ||
<PostStickyCheck postType="post" user={ | ||
{ data: { capabilities: { edit_others_posts: true, publish_posts: false } } } | ||
}> | ||
Can Toggle Sticky | ||
</PostStickyCheck> | ||
); | ||
expect( wrapper.type() ).toBe( null ); | ||
} ); | ||
|
||
it( 'should render if the user has the correct capability', () => { | ||
const wrapper = shallow( | ||
<PostStickyCheck postType="post" user={ user }> | ||
Can Toggle Sticky | ||
</PostStickyCheck> | ||
); | ||
expect( wrapper.type() ).not.toBe( null ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,22 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import { flowRight } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { PanelRow, FormToggle, withInstanceId, withAPIData } from '@wordpress/components'; | ||
import { PanelRow } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getEditedPostAttribute, getCurrentPostType } from '../../selectors'; | ||
import { editPost } from '../../actions'; | ||
|
||
export function PostSticky( { onUpdateSticky, postType, postSticky = false, instanceId, user } ) { | ||
if ( | ||
postType !== 'post' || | ||
! user.data || | ||
! user.data.capabilities.publish_posts || | ||
! user.data.capabilities.edit_others_posts | ||
) { | ||
return false; | ||
} | ||
|
||
const stickyToggleId = 'post-sticky-toggle-' + instanceId; | ||
import PostStickyCheck from '../../post-sticky/check'; | ||
import PostStickyForm from '../../post-sticky'; | ||
|
||
export function PostSticky() { | ||
return ( | ||
<PanelRow> | ||
<label htmlFor={ stickyToggleId }>{ __( 'Stick to the Front Page' ) }</label> | ||
<FormToggle | ||
checked={ postSticky } | ||
onChange={ () => onUpdateSticky( ! postSticky ) } | ||
showHint={ false } | ||
id={ stickyToggleId } | ||
/> | ||
</PanelRow> | ||
<PostStickyCheck> | ||
<PanelRow> | ||
<PostStickyForm /> | ||
</PanelRow> | ||
</PostStickyCheck> | ||
); | ||
} | ||
|
||
const applyConnect = connect( | ||
( state ) => { | ||
return { | ||
postType: getCurrentPostType( state ), | ||
postSticky: getEditedPostAttribute( state, 'sticky' ), | ||
}; | ||
}, | ||
( dispatch ) => { | ||
return { | ||
onUpdateSticky( postSticky ) { | ||
dispatch( editPost( { sticky: postSticky } ) ); | ||
}, | ||
}; | ||
}, | ||
); | ||
|
||
const applyWithAPIData = withAPIData( () => { | ||
return { | ||
user: '/wp/v2/users/me?context=edit', | ||
}; | ||
} ); | ||
|
||
export default flowRight( [ | ||
applyConnect, | ||
applyWithAPIData, | ||
withInstanceId, | ||
] )( PostSticky ); | ||
export default PostSticky; |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid duplicating the "support sticky" check, I extracted it to its own component. I could reuse this approach for all the other sidebar panels.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works well.
Otherwise, I could imagine the following:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could obfuscate component render logic, though. Right now most HoCs only deal with prop injection and side effects (like click-outside), not affecting when things should be rendered. Which is why the current way seems appropriate. 👌