2019-11-15 14:41:23 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-06-15 12:50:42 +00:00
|
|
|
import {
|
|
|
|
AttributeObject,
|
|
|
|
AttributeQuery,
|
|
|
|
AttributeTerm,
|
|
|
|
} from '@woocommerce/types';
|
2023-04-28 10:29:45 +00:00
|
|
|
import { sort } from 'fast-sort';
|
2019-11-15 14:41:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a query object, removes an attribute filter by a single slug.
|
2019-12-10 17:17:46 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Array} query Current query object.
|
|
|
|
* @param {Function} setQuery Callback to update the current query object.
|
|
|
|
* @param {Object} attribute An attribute object.
|
|
|
|
* @param {string} slug Term slug to remove.
|
2019-11-15 14:41:23 +00:00
|
|
|
*/
|
|
|
|
export const removeAttributeFilterBySlug = (
|
2022-06-15 12:50:42 +00:00
|
|
|
query: AttributeQuery[] = [],
|
|
|
|
setQuery: ( query: AttributeQuery[] ) => void,
|
|
|
|
attribute: AttributeObject,
|
2019-11-15 14:41:23 +00:00
|
|
|
slug = ''
|
|
|
|
) => {
|
|
|
|
// Get current filter for provided attribute.
|
|
|
|
const foundQuery = query.filter(
|
|
|
|
( item ) => item.attribute === attribute.taxonomy
|
|
|
|
);
|
|
|
|
|
|
|
|
const currentQuery = foundQuery.length ? foundQuery[ 0 ] : null;
|
|
|
|
|
|
|
|
if (
|
|
|
|
! currentQuery ||
|
|
|
|
! currentQuery.slug ||
|
|
|
|
! Array.isArray( currentQuery.slug ) ||
|
|
|
|
! currentQuery.slug.includes( slug )
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newSlugs = currentQuery.slug.filter( ( item ) => item !== slug );
|
|
|
|
|
|
|
|
// Remove current attribute filter from query.
|
|
|
|
const returnQuery = query.filter(
|
|
|
|
( item ) => item.attribute !== attribute.taxonomy
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add a new query for selected terms, if provided.
|
|
|
|
if ( newSlugs.length > 0 ) {
|
|
|
|
currentQuery.slug = newSlugs.sort();
|
|
|
|
returnQuery.push( currentQuery );
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:29:45 +00:00
|
|
|
setQuery( sort( returnQuery ).asc( 'attribute' ) );
|
2019-11-15 14:41:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a query object, sets the query up to filter by a given attribute and attribute terms.
|
2019-12-10 17:17:46 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Array} query Current query object.
|
|
|
|
* @param {Function} setQuery Callback to update the current query object.
|
|
|
|
* @param {Object} attribute An attribute object.
|
|
|
|
* @param {Array} attributeTerms Array of term objects.
|
|
|
|
* @param {string} operator Operator for the filter. Valid values: in, and.
|
2022-04-22 06:38:33 +00:00
|
|
|
*
|
|
|
|
* @return {Object} An attribute object.
|
2019-11-15 14:41:23 +00:00
|
|
|
*/
|
|
|
|
export const updateAttributeFilter = (
|
2022-06-15 12:50:42 +00:00
|
|
|
query: AttributeQuery[] = [],
|
|
|
|
setQuery: ( query: AttributeQuery[] ) => void,
|
|
|
|
attribute?: AttributeObject,
|
|
|
|
attributeTerms: AttributeTerm[] = [],
|
|
|
|
operator: 'in' | 'and' = 'in'
|
2019-11-15 14:41:23 +00:00
|
|
|
) => {
|
2022-06-15 12:50:42 +00:00
|
|
|
if ( ! attribute || ! attribute.taxonomy ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:41:23 +00:00
|
|
|
const returnQuery = query.filter(
|
|
|
|
( item ) => item.attribute !== attribute.taxonomy
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( attributeTerms.length === 0 ) {
|
|
|
|
setQuery( returnQuery );
|
|
|
|
} else {
|
|
|
|
returnQuery.push( {
|
|
|
|
attribute: attribute.taxonomy,
|
|
|
|
operator,
|
2021-05-16 17:59:32 +00:00
|
|
|
slug: attributeTerms.map( ( { slug } ) => slug ).sort(),
|
2019-11-15 14:41:23 +00:00
|
|
|
} );
|
2023-04-28 10:29:45 +00:00
|
|
|
setQuery( sort( returnQuery ).asc( 'attribute' ) );
|
2019-11-15 14:41:23 +00:00
|
|
|
}
|
2022-04-22 06:38:33 +00:00
|
|
|
|
|
|
|
return returnQuery;
|
2019-11-15 14:41:23 +00:00
|
|
|
};
|