-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add 'no posts' message to Latest Posts #3180
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3180 +/- ##
==========================================
- Coverage 31.17% 31.17% -0.01%
==========================================
Files 235 235
Lines 6534 6535 +1
Branches 1163 1165 +2
==========================================
Hits 2037 2037
+ Misses 3773 3772 -1
- Partials 724 726 +2
Continue to review full report at Codecov.
|
blocks/library/latest-posts/index.js
Outdated
@@ -54,12 +54,13 @@ registerBlockType( 'core/latest-posts', { | |||
|
|||
this.state = { | |||
latestPosts: [], | |||
isLoading: true, |
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.
Hi, @Soean thank you for your contribution!
Would it be possible to avoid the need for an isLoading flag by setting the initial state of latestPosts to null?
This way we would be able to differentiate between the state lastest posts are not yet available and latestPosts are empty. By avoiding loading flags and just checking data availability, if later we decide to implement an offline mechanism it may make things simpler.
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.
Hey @jorgefilipecosta, thanks for your review. I added the changes to the PR.
blocks/library/latest-posts/index.js
Outdated
return ( | ||
<Placeholder | ||
icon="admin-post" | ||
label={ __( 'Latest Posts' ) } | ||
> | ||
<Spinner /> | ||
{ placeholderContent } |
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 be inlined here:
{ condition
? <Spinner />
: __( 'No posts found.' )
}
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.
Thanks for your feedback, I inlined the condition.
blocks/library/latest-posts/index.js
Outdated
@@ -99,13 +99,17 @@ registerBlockType( 'core/latest-posts', { | |||
const { latestPosts } = this.state; | |||
const { setAttributes } = this.props; | |||
|
|||
if ( ! latestPosts.length ) { | |||
const hasPosts = Array.isArray( latestPosts ) && latestPosts.length; | |||
if ( ! hasPosts ) { |
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.
Hi @Soean, if there are no posts we just render the placeholder. So if the user has no posts the user will not be able to change to block settings e.g: number of posts to display. I think if the user does not have posts he should be able to use the inspector to configure the block like if posts existed.
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.
@jorgefilipecosta You are right. Now the inspector controls are also shown on the placeholder view. The BlockControls with align and layoutControls are only visible if posts existed.
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.
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 looks good, thanks for the contribution. My recommendations are to address the feedback left here, and then that you rebase your branch on top of the latest origin/master
and squash it into one nice commit, so that we can merge it cleanly.
blocks/library/latest-posts/index.js
Outdated
@@ -97,17 +97,56 @@ registerBlockType( 'core/latest-posts', { | |||
|
|||
render() { | |||
const { latestPosts } = this.state; | |||
const { setAttributes } = this.props; | |||
const { focus, setAttributes } = this.props; | |||
const { displayPostDate, align, layout, columns } = this.props.attributes; |
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.
Minor, but attributes
could be extracted in the line above, so that this line becomes const { … } = attributes
.
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.
We can also extract postsToShow
here, since it's used several times.
blocks/library/latest-posts/index.js
Outdated
if ( ! latestPosts.length ) { | ||
return ( | ||
<Placeholder | ||
const inspectorControls = ( |
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.
Also minor: if this becomes const inspectorControls = focus && (
then we can reduce one whole indentation level for that assignment.
{ ! Array.isArray( latestPosts ) ? | ||
<Spinner /> : | ||
__( 'No posts found.' ) | ||
} |
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.
When using ternaries (or if/else structs, for that matter) we tend to prefer conditions that aren't negated. Also, specifically for ternaries (but not e.g. &&
) we prefer to place the operators at the beginning of the breaking lines. So in this case:
Array.isArray( latestPosts )
? __( 'No posts found.' )
: <Spinner />
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.
I also favor these, but I wonder if a recent ESlint update forced the style applied here, in which case we should update the ESlint config.
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.
Oh, you're right, this has changed. @Soean, you can dismiss my comment on prepending the lines with the operators.
Once we update our ESLint config we can circle back, no worries.
@mcsf Thanks for your review. The minor code improvements make sense. I rebased the PR and added the improvements into the last commit. |
Tested this and it's working well 👍 |
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.
All comments were addressed. I think it looks good to go. Thanks for your contribution :) Taking into account that it was tested by @codebykat and me. I'm giving 👍
Description
Now the placeholder shows a message, if no posts were found. This PR adds a loading state to the block.
resolve #3135
Screenshots:
Loading:

No posts:
