/** * External dependencies */ import classnames from 'classnames'; import isUndefined from 'lodash/isUndefined'; import pickBy from 'lodash/pickBy'; import side from './icons'; /** * WordPress dependencies */ const { Component, Fragment } = wp.element; const { PanelBody, Placeholder, QueryControls, RangeControl, TextControl, Spinner, ToggleControl, Toolbar, withAPIData, } = wp.components; const { __ } = wp.i18n; const { decodeEntities } = wp.htmlEntities; const { withSelect } = wp.data; const { InspectorControls, BlockAlignmentToolbar, BlockControls, } = wp.editor; const MAX_POSTS_COLUMNS = 4; class PostsGridEdit extends Component { constructor() { super( ...arguments ); this.handleReadMoreText = this.handleReadMoreText.bind( this ); this.toggleState = this.toggleState.bind( this ); } toggleState( key ) { const { setAttributes } = this.props; const value = this.props.attributes[ key ]; setAttributes( { [ key ]: ! value, } ); } handleReadMoreText( value ) { const { setAttributes } = this.props; setAttributes( { readMoreText: value } ); } render() { const { attributes, categoriesList, setAttributes, latestPosts } = this.props; const { displayPostDate, displayPostExcerpt, displayReadMore, displayFeaturedImage, displayPostAuthor, readMoreText, align, postLayout, columns, order, orderBy, categories, postsToShow, } = attributes; const hasPosts = Array.isArray( latestPosts ) && latestPosts.length; const inspectorControls = ( setAttributes( { order: value } ) } onOrderByChange={ ( value ) => setAttributes( { orderBy: value } ) } onCategoryChange={ ( value ) => setAttributes( { categories: '' !== value ? value : undefined } ) } onNumberOfItemsChange={ ( value ) => setAttributes( { postsToShow: value } ) } /> this.toggleState( 'displayPostDate' ) } /> this.toggleState( 'displayPostExcerpt' ) } /> this.toggleState( 'displayPostAuthor' ) } /> this.toggleState( 'displayFeaturedImage' ) } /> this.toggleState( 'displayReadMore' ) } /> { displayReadMore && this.handleReadMoreText( nextValue ) } /> } { postLayout === 'grid' && setAttributes( { columns: value } ) } min={ 2 } max={ ! hasPosts ? MAX_POSTS_COLUMNS : Math.min( MAX_POSTS_COLUMNS, latestPosts.length ) } /> } ); if ( ! hasPosts ) { return ( { inspectorControls } { ! Array.isArray( latestPosts ) ? : __( 'No posts found.' ) } ); } // Removing posts from display should be instant. const displayPosts = latestPosts.length > postsToShow ? latestPosts.slice( 0, postsToShow ) : latestPosts; const layoutControls = [ { icon: 'list-view', title: __( 'List View' ), onClick: () => setAttributes( { postLayout: 'list' } ), isActive: postLayout === 'list', }, { icon: 'grid-view', title: __( 'Grid View' ), onClick: () => setAttributes( { postLayout: 'grid' } ), isActive: postLayout === 'grid', }, { icon: side, title: __( 'Side View' ), onClick: () => setAttributes( { postLayout: 'side' } ), isActive: postLayout === 'side', }, ]; return ( { inspectorControls } { setAttributes( { align: nextAlign } ); } } controls={ [ 'center', 'wide', 'full' ] } /> ); } } export default withSelect( ( select, props ) => { const { postsToShow, order, orderBy, categories } = props.attributes; const { getEntityRecords } = select( 'core' ); const latestPostsQuery = pickBy( { categories, order, orderby: orderBy, per_page: postsToShow, _fields: [ 'id', 'date_gmt', 'link', 'title', 'excerpt', 'featured_media', 'author', 'advanced/featured_image_src', 'advanced/author_data', ], }, ( value ) => ! isUndefined( value ) ); const categoriesListQuery = { per_page: 100, _fields: [ 'id', 'name', 'parent' ], }; return { latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ), categoriesList: getEntityRecords( 'taxonomy', 'category', categoriesListQuery ), }; } )( PostsGridEdit );