Server IP : 104.21.14.103 / Your IP : 3.142.156.202 Web Server : LiteSpeed System : Linux business53.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : giankuin ( 1871) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/thread-self/root/home/giankuin/sieuthiweb.com.vn/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/utils/ |
Upload File : |
/** * External dependencies */ import { sortBy, map } from 'lodash'; /** * Given a query object, removes an attribute filter by a single slug. * * @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. */ export const removeAttributeFilterBySlug = ( query = [], setQuery = () => {}, attribute, 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 ); } setQuery( sortBy( returnQuery, 'attribute' ) ); }; /** * Given a query object, sets the query up to filter by a given attribute and attribute terms. * * @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. */ export const updateAttributeFilter = ( query = [], setQuery = () => {}, attribute, attributeTerms = [], operator = 'in' ) => { const returnQuery = query.filter( ( item ) => item.attribute !== attribute.taxonomy ); if ( attributeTerms.length === 0 ) { setQuery( returnQuery ); } else { returnQuery.push( { attribute: attribute.taxonomy, operator, slug: map( attributeTerms, 'slug' ).sort(), } ); setQuery( sortBy( returnQuery, 'attribute' ) ); } };