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

Add support for filtering on categories and tags #13505

Merged
merged 2 commits into from
Sep 23, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class SearchFilterPostTypes extends Component {
toggleFilter = () => {
const selected = getCheckedInputNames( this.filtersList.current );
this.setState( { selected }, () => {
this.props.onChange( 'postTypes', selected );
this.props.onChange( 'post_types', selected );
} );
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,51 @@
/**
* External dependencies
*/
import { h, Component } from 'preact';
import { h, createRef, Component } from 'preact';
import strip from 'strip';
import { getCheckedInputNames } from '../lib/dom';

export default class SearchFilterTaxonomies extends Component {
constructor( props ) {
super( props );
this.state = { selected: this.props.initialValue };
this.filtersList = createRef();
}

toggleFilter = () => {
const selected = getCheckedInputNames( this.filtersList.current );
this.setState( { selected }, () => {
this.props.onChange( this.props.configuration.taxonomy, selected );
} );
};

renderTaxonomy = ( { key, doc_count: count } ) => {
return (
<div>
<input
checked={ this.state.selected && this.state.selected.includes( key ) }
id={ `jp-instant-search-filter-taxonomies-${ key }` }
name={ key }
onChange={ this.toggleFilter }
type="checkbox"
/>
<label htmlFor={ `jp-instant-search-filter-taxonomies-${ key }` }>
{ strip( key ) } ({ count })
</label>
</div>
);
};

render() {
return (
<div>
<h4 className="jetpack-search-filters-widget__sub-heading">{ this.props.filter.name }</h4>
<ul className="jetpack-search-filters-widget__filter-list">
<h4 className="jetpack-search-filters-widget__sub-heading">
{ this.props.configuration.name }
</h4>
<ul className="jetpack-search-filters-widget__filter-list" ref={ this.filtersList }>
{ this.props.aggregation &&
'buckets' in this.props.aggregation &&
this.props.aggregation.buckets.map( bucket => (
<div>
<input
disabled
id={ `jp-instant-search-filter-taxonomies-${ bucket.key }` }
name={ bucket.key }
type="checkbox"
/>
<label htmlFor={ `jp-instant-search-filter-taxonomies-${ bucket.key }` }>
{ strip( bucket.key ) } ({ bucket.doc_count })
</label>
</div>
) ) }
this.props.aggregation.buckets.map( this.renderTaxonomy ) }
</ul>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ export default class SearchFiltersWidget extends Component {
return results && <SearchFilterDates aggregation={ results } filter={ configuration } />;
case 'taxonomy':
return (
results && <SearchFilterTaxonomies aggregation={ results } filter={ configuration } />
results && (
<SearchFilterTaxonomies
aggregation={ results }
configuration={ configuration }
initialValue={ this.props.initialValues[ configuration.taxonomy ] }
onChange={ this.props.onChange }
/>
)
);
case 'post_type':
return (
results && (
<SearchFilterPostTypes
aggregation={ results }
configuration={ configuration }
initialValue={ this.props.initialValues.postTypes }
initialValue={ this.props.initialValues.post_types }
onChange={ this.props.onChange }
postTypes={ this.props.postTypes }
/>
Expand Down
9 changes: 7 additions & 2 deletions modules/search/instant-search/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ function buildFilterObject( filterQuery ) {
}

const filter = { bool: { must: [] } };
if ( Array.isArray( filterQuery.postTypes ) && filterQuery.postTypes.length > 0 ) {
filterQuery.postTypes.forEach( postType => {
if ( Array.isArray( filterQuery.post_types ) && filterQuery.post_types.length > 0 ) {
filterQuery.post_types.forEach( postType => {
filter.bool.must.push( { term: { post_type: postType } } );
} );
}
if ( Array.isArray( filterQuery.post_tag ) && filterQuery.post_tag.length > 0 ) {
filterQuery.post_tag.forEach( tag => {
filter.bool.must.push( { term: { 'tag.slug': tag } } );
} );
}
return filter;
}

Expand Down
4 changes: 3 additions & 1 deletion modules/search/instant-search/lib/query-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export function getFilterQuery( filterKey ) {
}

return {
postTypes: getFilterQueryByKey( 'postTypes' ),
category: getFilterQueryByKey( 'category' ),
post_tag: getFilterQueryByKey( 'post_tag' ),
post_types: getFilterQueryByKey( 'post_types' ),
};
}

Expand Down