Skip to content

Commit

Permalink
reworks withSelect to output latestPosts array
Browse files Browse the repository at this point in the history
  • Loading branch information
jomurgel committed Sep 21, 2018
1 parent 688eac8 commit a725934
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/blocks/recent-posts/recent-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,26 @@ class RecentPostsBlock extends Component {
}
}

export default withSelect( ( props ) => {
export default withSelect( ( select, props ) => {
const { postsToShow, order, orderby, taxonomies } = props.attributes;
const { getEntityRecords } = select( 'core' );

const decodedTaxonomies = taxonomies ? JSON.parse( taxonomies ) : {};

// This can be made to be much more flexible and allow for custom taxonomies and the like. Phase 2!
const tags = decodedTaxonomies.post_tag && 0 < decodedTaxonomies.post_tag.length ? decodedTaxonomies.post_tag.map( tag => tag.id ) : undefined;
const categories = decodedTaxonomies.category && 0 < decodedTaxonomies.category.length ? decodedTaxonomies.category.map( category => category.id ) : undefined;
const latestPostsQuery = stringify( _pickBy( {

const latestPostsQuery = pickBy( {
_embed: 'embed',
orderby,
order,
tags,
categories,
per_page: postsToShow, // eslint-disable-line
}, value => ! _isUndefined( value ) ) );
}, ( value ) => ! isUndefined( value ) );

return {
latestPosts: `/wp/v2/posts?${ latestPostsQuery }`,
latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ),
};
} )( RecentPostsBlock );

0 comments on commit a725934

Please sign in to comment.