Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renamed & moved QueryControls from blocks/QueryPanel to components/QueryControls #5319

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions blocks/library/latest-posts/block.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,6 +12,7 @@ import { stringify } from 'querystringify';
import { Component } from '@wordpress/element';
import {
Placeholder,
QueryControls,
RangeControl,
Spinner,
ToggleControl,
Expand All @@ -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';
Expand All @@ -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 && (
<InspectorControls key="inspector">
<h3>{ __( 'Latest Posts Settings' ) }</h3>
<QueryPanel
<QueryControls
{ ...{ order, orderBy } }
numberOfItems={ postsToShow }
category={ categories }
categoriesList={ get( categoriesList, 'data', {} ) }
Copy link
Member

Choose a reason for hiding this comment

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

categoriesList will never be undefined, get isn't strictly necessary.

In a perfect world we wouldn't assume the source of the value, but given the data property access, we already kinda are.

selectedCategoryId={ categories }
onOrderChange={ ( value ) => setAttributes( { order: value } ) }
onOrderByChange={ ( value ) => setAttributes( { orderBy: value } ) }
onCategoryChange={ ( value ) => setAttributes( { categories: '' !== value ? value : undefined } ) }
Expand Down Expand Up @@ -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 );
34 changes: 0 additions & 34 deletions blocks/query-panel/category-select.js

This file was deleted.

1 change: 1 addition & 0 deletions components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
20 changes: 20 additions & 0 deletions components/query-controls/category-select.js
Original file line number Diff line number Diff line change
@@ -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 (
<TreeSelect
{ ...{ label, noOptionLabel, onChange } }
tree={ termsTree }
selectedId={ selectedCategoryId }
/>
);
}
27 changes: 12 additions & 15 deletions blocks/query-panel/index.js → components/query-controls/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
/**
* 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,
maxItems = DEFAULT_MAX_ITEMS,
minItems = DEFAULT_MIN_ITEMS,
onCategoryChange,
onNumberOfItemsChange,
onOrderChange = noop,
onOrderByChange = noop,
onOrderChange,
onOrderByChange,
} ) {
return [
( onOrderChange || onOrderByChange ) && (
( onOrderChange && onOrderByChange ) && (
<SelectControl
key="query-panel-select"
key="query-controls-order-select"
label={ __( 'Order by' ) }
value={ `${ orderBy }/${ order }` }
options={ [
Expand Down Expand Up @@ -68,15 +64,16 @@ export default function QueryPanel( {
),
onCategoryChange && (
<CategorySelect
key="query-panel-category-select"
key="query-controls-category-select"
categoriesList={ categoriesList }
label={ __( 'Category' ) }
noOptionLabel={ __( 'All' ) }
selectedCategory={ category }
selectedCategoryId={ selectedCategoryId }
onChange={ onCategoryChange }
/> ),
onNumberOfItemsChange && (
<RangeControl
key="query-panel-range-control"
key="query-controls-range-control"
label={ __( 'Number of items' ) }
value={ numberOfItems }
onChange={ onNumberOfItemsChange }
Expand Down