diff --git a/blocks/library/latest-posts/block.js b/blocks/library/latest-posts/block.js index 0ca30c391bd82..17bc96dce57f5 100644 --- a/blocks/library/latest-posts/block.js +++ b/blocks/library/latest-posts/block.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { isUndefined, pickBy } from 'lodash'; +import { get, isUndefined, pickBy } from 'lodash'; import moment from 'moment'; import classnames from 'classnames'; import { stringify } from 'querystringify'; @@ -12,6 +12,7 @@ import { stringify } from 'querystringify'; import { Component } from '@wordpress/element'; import { Placeholder, + QueryControls, RangeControl, Spinner, ToggleControl, @@ -26,7 +27,6 @@ import { decodeEntities } from '@wordpress/utils'; */ import './editor.scss'; import './style.scss'; -import QueryPanel from '../../query-panel'; import InspectorControls from '../../inspector-controls'; import BlockControls from '../../block-controls'; import BlockAlignmentToolbar from '../../block-alignment-toolbar'; @@ -49,16 +49,17 @@ class LatestPostsBlock extends Component { render() { const latestPosts = this.props.latestPosts.data; - const { attributes, isSelected, setAttributes } = this.props; + const { attributes, categoriesList, isSelected, setAttributes } = this.props; const { displayPostDate, align, layout, columns, order, orderBy, categories, postsToShow } = attributes; const inspectorControls = isSelected && (

{ __( 'Latest Posts Settings' ) }

- setAttributes( { order: value } ) } onOrderByChange={ ( value ) => setAttributes( { orderBy: value } ) } onCategoryChange={ ( value ) => setAttributes( { categories: '' !== value ? value : undefined } ) } @@ -155,14 +156,19 @@ class LatestPostsBlock extends Component { export default withAPIData( ( props ) => { const { postsToShow, order, orderBy, categories } = props.attributes; - const queryString = stringify( pickBy( { + const latestPostsQuery = stringify( pickBy( { categories, order, orderBy, per_page: postsToShow, _fields: [ 'date_gmt', 'link', 'title' ], }, value => ! isUndefined( value ) ) ); + const categoriesListQuery = stringify( { + per_page: 100, + _fields: [ 'id', 'name', 'parent' ], + } ); return { - latestPosts: `/wp/v2/posts?${ queryString }`, + latestPosts: `/wp/v2/posts?${ latestPostsQuery }`, + categoriesList: `/wp/v2/categories?${ categoriesListQuery }`, }; } )( LatestPostsBlock ); diff --git a/blocks/query-panel/category-select.js b/blocks/query-panel/category-select.js deleted file mode 100644 index b04be89b10d22..0000000000000 --- a/blocks/query-panel/category-select.js +++ /dev/null @@ -1,34 +0,0 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; -import { stringify } from 'querystringify'; - -/** - * WordPress dependencies - */ -import { buildTermsTree } from '@wordpress/utils'; -import { TreeSelect, withAPIData } from '@wordpress/components'; - -function CategorySelect( { label, noOptionLabel, categories, selectedCategory, onChange } ) { - const termsTree = buildTermsTree( get( categories, 'data', {} ) ); - return ( - - ); -} - -const applyWithAPIData = withAPIData( () => { - const query = stringify( { - per_page: 100, - _fields: [ 'id', 'name', 'parent' ], - } ); - return { - categories: `/wp/v2/categories?${ query }`, - }; -} ); - -export default applyWithAPIData( CategorySelect ); diff --git a/components/index.js b/components/index.js index 0f1c40ee2d023..905a87458d872 100644 --- a/components/index.js +++ b/components/index.js @@ -31,6 +31,7 @@ export { default as PanelHeader } from './panel/header'; export { default as PanelRow } from './panel/row'; export { default as Placeholder } from './placeholder'; export { default as Popover } from './popover'; +export { default as QueryControls } from './query-controls'; export { default as RadioControl } from './radio-control'; export { default as RangeControl } from './range-control'; export { default as ResponsiveWrapper } from './responsive-wrapper'; diff --git a/components/query-controls/category-select.js b/components/query-controls/category-select.js new file mode 100644 index 0000000000000..978dd9da84e1c --- /dev/null +++ b/components/query-controls/category-select.js @@ -0,0 +1,20 @@ +/** + * WordPress dependencies + */ +import { buildTermsTree } from '@wordpress/utils'; + +/** + * Internal dependencies + */ +import TreeSelect from '../tree-select'; + +export default function CategorySelect( { label, noOptionLabel, categoriesList, selectedCategoryId, onChange } ) { + const termsTree = buildTermsTree( categoriesList ); + return ( + + ); +} diff --git a/blocks/query-panel/index.js b/components/query-controls/index.js similarity index 79% rename from blocks/query-panel/index.js rename to components/query-controls/index.js index 667409a3b23a6..4ed57e468eb69 100644 --- a/blocks/query-panel/index.js +++ b/components/query-controls/index.js @@ -1,24 +1,20 @@ -/** - * External dependencies - */ -import { noop } from 'lodash'; - /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { RangeControl, SelectControl } from '@wordpress/components'; /** * Internal dependencies */ +import { RangeControl, SelectControl } from '../'; import CategorySelect from './category-select'; const DEFAULT_MIN_ITEMS = 1; const DEFAULT_MAX_ITEMS = 100; -export default function QueryPanel( { - category, +export default function QueryControls( { + categoriesList, + selectedCategoryId, numberOfItems, order, orderBy, @@ -26,13 +22,13 @@ export default function QueryPanel( { minItems = DEFAULT_MIN_ITEMS, onCategoryChange, onNumberOfItemsChange, - onOrderChange = noop, - onOrderByChange = noop, + onOrderChange, + onOrderByChange, } ) { return [ - ( onOrderChange || onOrderByChange ) && ( + ( onOrderChange && onOrderByChange ) && ( ), onNumberOfItemsChange && (