/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { BlockControls, InspectorControls } from '@wordpress/editor'; import { PanelBody, RangeControl, ToggleControl, Toolbar, Path, SVG } from '@wordpress/components'; import { Component, Fragment } from '@wordpress/element'; import { get } from 'lodash'; import { withSelect } from '@wordpress/data'; import { compose, withInstanceId } from '@wordpress/compose'; export const MAX_POSTS_TO_SHOW = 6; function PlaceholderPostEdit( props ) { return (
{ __( "Preview unavailable: you haven't published enough posts with similar content.", 'jetpack' ) } { props.displayThumbnails && (
{ __( 'Grey square', 'jetpack' ) } { __( 'Icon for image', 'jetpack' ) }
) } { props.displayDate && (
{ __( 'August 3, 2018', 'jetpack' ) }
) } { props.displayContext && (
{ __( 'In “Uncategorized”', 'jetpack' ) }
) }
); } function RelatedPostsEditItem( props ) { return (
{ props.post.title } { props.displayThumbnails && props.post.img && props.post.img.src && ( { ) } { props.displayDate && (
{ props.post.date }
) } { props.displayContext && (
{ props.post.context }
) }
); } function RelatedPostsPreviewRows( props ) { const className = 'jp-related-posts-i2__row'; let topRowEnd = 0; const displayLowerRow = props.posts.length > 3; switch ( props.posts.length ) { case 2: case 4: case 5: topRowEnd = 2; break; default: topRowEnd = 3; break; } return (
{ props.posts.slice( 0, topRowEnd ) }
{ displayLowerRow && (
{ props.posts.slice( topRowEnd ) }
) }
); } class RelatedPostsEdit extends Component { render() { const { attributes, className, posts, setAttributes, instanceId } = this.props; const { displayContext, displayDate, displayThumbnails, postLayout, postsToShow } = attributes; const layoutControls = [ { icon: 'grid-view', title: __( 'Grid View', 'jetpack' ), onClick: () => setAttributes( { postLayout: 'grid' } ), isActive: postLayout === 'grid', }, { icon: 'list-view', title: __( 'List View', 'jetpack' ), onClick: () => setAttributes( { postLayout: 'list' } ), isActive: postLayout === 'list', }, ]; // To prevent the block from crashing, we need to limit ourselves to the // posts returned by the backend - so if we want 6 posts, but only 3 are // returned, we need to limit ourselves to those 3 and fill in the rest // with placeholders. // // Also, if the site does not have sufficient posts to display related ones // (minimum 10 posts), we also use this code block to fill in the // placeholders. const previewClassName = 'jp-relatedposts-i2'; const displayPosts = []; for ( let i = 0; i < postsToShow; i++ ) { if ( posts[ i ] ) { displayPosts.push( ); } else { displayPosts.push( ); } } return ( setAttributes( { displayThumbnails: value } ) } /> setAttributes( { displayDate: value } ) } /> setAttributes( { displayContext: value } ) } /> setAttributes( { postsToShow: Math.min( value, MAX_POSTS_TO_SHOW ) } ) } min={ 1 } max={ MAX_POSTS_TO_SHOW } />
); } } export default compose( withInstanceId, withSelect( select => { const { getCurrentPost } = select( 'core/editor' ); const posts = get( getCurrentPost(), 'jetpack-related-posts', [] ); return { posts, }; } ) )( RelatedPostsEdit );