Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Active Filters block: Clear All button work with all types of permali…
Browse files Browse the repository at this point in the history
…nk settings. (#6315)
  • Loading branch information
dinhtungdu authored Apr 26, 2022
1 parent b66ab53 commit 9183017
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 2 additions & 3 deletions assets/js/blocks/active-filters/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
formatPriceRange,
renderRemovableListItem,
removeArgsFromFilterUrl,
getBaseUrl,
cleanFilterUrl,
} from './utils';
import ActiveAttributeFilters from './active-attribute-filters';

Expand Down Expand Up @@ -255,8 +255,7 @@ const ActiveFiltersBlock = ( {
className="wc-block-active-filters__clear-all"
onClick={ () => {
if ( filteringForPhpTemplate ) {
window.location.href = getBaseUrl();
return;
return cleanFilterUrl();
}
setMinPrice( undefined );
setMaxPrice( undefined );
Expand Down
29 changes: 20 additions & 9 deletions assets/js/blocks/active-filters/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,28 @@ export const removeArgsFromFilterUrl = ( ...args ) => {
};

/**
* Get the base URL for the current page.
*
* @return {string} The current URL without the query args.
* Clean the filter URL.
*/
export const getBaseUrl = () => {
export const cleanFilterUrl = () => {
const url = window.location.href;
const args = getQueryArgs( url );
const cleanUrl = removeQueryArgs( url, ...Object.keys( args ) );
const remainingArgs = Object.fromEntries(
Object.keys( args )
.filter( ( arg ) => {
if (
arg.includes( 'min_price' ) ||
arg.includes( 'max_price' ) ||
arg.includes( 'filter_' ) ||
arg.includes( 'query_type_' )
) {
return false;
}

const queryStringIndex = url.indexOf( '?' );
if ( queryStringIndex === -1 ) {
return url;
}
return true;
} )
.map( ( key ) => [ key, args[ key ] ] )
);

return url.substring( 0, queryStringIndex );
window.location.href = addQueryArgs( cleanUrl, remainingArgs );
};

0 comments on commit 9183017

Please sign in to comment.