This commit is contained in:
Albert Juhé Lluveras 2019-02-20 10:21:05 +01:00 committed by GitHub
parent 5e4f14a81d
commit 1660484d4c
3 changed files with 18 additions and 11 deletions

View File

@ -145,15 +145,22 @@ export default compose(
}
const { report } = props.params;
const items = searchItemsByString( select, report, search );
const searchWords = search.split( ',' ).map( searchWord => searchWord.replace( '%2C', ',' ) );
const items = searchItemsByString( select, report, searchWords );
const ids = Object.keys( items );
if ( ! ids.length ) {
return {};
return {
query: {
...props.query,
search: searchWords,
},
};
}
return {
query: {
...props.query,
search: searchWords,
[ report ]: ids.join( ',' ),
},
};

View File

@ -7,17 +7,16 @@
/**
* Returns items based on a search query.
*
* @param {Object} select Instance of @wordpress/select
* @param {String} endpoint Report API Endpoint
* @param {String} search Search strings separated by commas.
* @return {Object} Object Object containing the matching items.
* @param {Object} select Instance of @wordpress/select
* @param {String} endpoint Report API Endpoint
* @param {String[]} search Array of search strings.
* @return {Object} Object containing the matching items.
*/
export function searchItemsByString( select, endpoint, search ) {
const { getItems } = select( 'wc-api' );
const searchWords = search.split( ',' );
const items = {};
searchWords.forEach( searchWord => {
search.forEach( searchWord => {
const newItems = getItems( endpoint, {
search: searchWord,
per_page: 10,

View File

@ -161,7 +161,9 @@ class TableCard extends Component {
onSearch( values ) {
const { compareParam } = this.props;
const labels = values.map( v => v.label );
// A comma is used as a separator between search terms, so we want to escape
// any comma they contain.
const labels = values.map( v => v.label.replace( ',', '%2C' ) );
if ( labels.length ) {
updateQueryString( {
filter: undefined,
@ -252,8 +254,7 @@ class TableCard extends Component {
totalRows,
} = this.props;
const { selectedRows, showCols } = this.state;
const searchedValues = query.search ? query.search.split( ',' ) : [];
const searchedLabels = searchedValues.map( v => ( { id: v, label: v } ) );
const searchedLabels = Array.isArray( query.search ) ? query.search.map( v => ( { id: v, label: v } ) ) : [];
const allHeaders = this.props.headers;
let headers = this.getVisibleHeaders();
let rows = this.getVisibleRows();