Skip to content

Add 'no posts' message to Latest Posts#3180

Merged
gziolo merged 7 commits intoWordPress:masterfrom
Soean:patch-5
Nov 8, 2017
Merged

Add 'no posts' message to Latest Posts#3180
gziolo merged 7 commits intoWordPress:masterfrom
Soean:patch-5

Conversation

@Soean
Copy link
Copy Markdown
Member

@Soean Soean commented Oct 26, 2017

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:
bildschirmfoto 2017-10-26 um 16 18 19

No posts:
bildschirmfoto 2017-10-26 um 16 16 31

@Soean Soean changed the title Add loading state to Latest Posts Add 'no posts' message to Latest Posts Oct 26, 2017
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 26, 2017

Codecov Report

Merging #3180 into master will decrease coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
blocks/library/latest-posts/index.js 9.8% <0%> (-0.2%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ea79aa...3a7657e. Read the comment docs.

Comment thread blocks/library/latest-posts/index.js Outdated

this.state = {
latestPosts: [],
isLoading: true,
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

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.

Comment thread blocks/library/latest-posts/index.js Outdated
label={ __( 'Latest Posts' ) }
>
<Spinner />
{ placeholderContent }
Copy link
Copy Markdown
Member

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.' )
}

Copy link
Copy Markdown
Member Author

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.

@mtias mtias added [Feature] Blocks Overall functionality of blocks [Type] Enhancement A suggestion for improvement. labels Oct 27, 2017

if ( ! latestPosts.length ) {
const hasPosts = Array.isArray( latestPosts ) && latestPosts.length;
if ( ! hasPosts ) {
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

@Soean Soean Oct 28, 2017

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Soean this tests well for me! Nice work :) @mcsf, @gziolo could you have a second look
👀 before merging?

Copy link
Copy Markdown
Contributor

@mcsf mcsf left a 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.

Comment thread blocks/library/latest-posts/index.js Outdated
const { latestPosts } = this.state;
const { setAttributes } = this.props;
const { focus, setAttributes } = this.props;
const { displayPostDate, align, layout, columns } = this.props.attributes;
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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.

Comment thread blocks/library/latest-posts/index.js Outdated
if ( ! latestPosts.length ) {
return (
<Placeholder
const inspectorControls = (
Copy link
Copy Markdown
Contributor

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.' )
}
Copy link
Copy Markdown
Contributor

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 />

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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.

@Soean
Copy link
Copy Markdown
Member Author

Soean commented Nov 2, 2017

@mcsf Thanks for your review. The minor code improvements make sense. I rebased the PR and added the improvements into the last commit.

@codebykat
Copy link
Copy Markdown
Contributor

Tested this and it's working well 👍

Copy link
Copy Markdown
Member

@gziolo gziolo left a 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 👍

@gziolo gziolo merged commit eab4b64 into WordPress:master Nov 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Blocks Overall functionality of blocks [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Latest Posts block keeps spinning if no Posts

7 participants