Add 'no posts' message to Latest Posts#3180
Add 'no posts' message to Latest Posts#3180gziolo merged 7 commits intoWordPress:masterfrom Soean:patch-5
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.
|
|
|
||
| this.state = { | ||
| latestPosts: [], | ||
| isLoading: true, |
There was a problem hiding this comment.
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.
Hey @jorgefilipecosta, thanks for your review. I added the changes to the PR.
| label={ __( 'Latest Posts' ) } | ||
| > | ||
| <Spinner /> | ||
| { placeholderContent } |
There was a problem hiding this comment.
This could be inlined here:
{ condition
? <Spinner />
: __( 'No posts found.' )
}There was a problem hiding this comment.
Thanks for your feedback, I inlined the condition.
|
|
||
| if ( ! latestPosts.length ) { | ||
| const hasPosts = Array.isArray( latestPosts ) && latestPosts.length; | ||
| if ( ! hasPosts ) { |
There was a problem hiding this comment.
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.
@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.
mcsf
left a comment
There was a problem hiding this comment.
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.
| 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.
Minor, but attributes could be extracted in the line above, so that this line becomes const { … } = attributes.
There was a problem hiding this comment.
We can also extract postsToShow here, since it's used several times.
| if ( ! latestPosts.length ) { | ||
| return ( | ||
| <Placeholder | ||
| const inspectorControls = ( |
There was a problem hiding this comment.
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.
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.
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.
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 👍 |
gziolo
left a comment
There was a problem hiding this comment.
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:
