This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Attribute Filter block compatible with PHP rendered Classic Temp…
…late block (#6204) * Enable Attribute Filter block to work with the PHP rendered Classic Template block * Check for presence of option before rendering it * improve filter product by attribute * fix pagination problem * fix check when two filter block with same attribute are used Co-authored-by: Luigi <[email protected]>
- Loading branch information
1 parent
4fe44b9
commit 520997b
Showing
6 changed files
with
242 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { addQueryArgs, removeQueryArgs } from '@wordpress/url'; | ||
import { QueryArgs } from '@wordpress/url/build-types/get-query-args'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getUrlParameter } from '../../utils/filters'; | ||
|
||
interface Param { | ||
attribute: string; | ||
operator: string; | ||
slug: Array< string >; | ||
} | ||
|
||
export const formatParams = ( url: string, params: Array< Param > = [] ) => { | ||
const paramObject: Record< string, string > = {}; | ||
|
||
params.forEach( ( param ) => { | ||
const { attribute, slug, operator } = param; | ||
|
||
// Custom filters are prefix with `pa_` so we need to remove this. | ||
const name = attribute.replace( 'pa_', '' ); | ||
const values = slug.join( ',' ); | ||
const queryType = `query_type_${ name }`; | ||
const type = operator === 'in' ? 'or' : 'and'; | ||
|
||
// The URL parameter requires the prefix filter_ with the attribute name. | ||
paramObject[ `filter_${ name }` ] = values; | ||
paramObject[ queryType ] = type; | ||
} ); | ||
|
||
// Clean the URL before we add our new query parameters to it. | ||
const cleanUrl = removeQueryArgs( url, ...Object.keys( paramObject ) ); | ||
|
||
return addQueryArgs( cleanUrl, paramObject ); | ||
}; | ||
|
||
export const areAllFiltersRemoved = ( { | ||
currentCheckedFilters, | ||
hasSetPhpFilterDefaults, | ||
}: { | ||
currentCheckedFilters: Array< string >; | ||
hasSetPhpFilterDefaults: boolean; | ||
} ) => hasSetPhpFilterDefaults && currentCheckedFilters.length === 0; | ||
|
||
export const getActiveFilters = ( | ||
isFilteringForPhpTemplateEnabled: boolean, | ||
attributeObject: Record< string, string > | undefined | ||
) => { | ||
if ( isFilteringForPhpTemplateEnabled && attributeObject ) { | ||
const defaultAttributeParam = getUrlParameter( | ||
`filter_${ attributeObject.name }` | ||
); | ||
const defaultCheckedValue = | ||
typeof defaultAttributeParam === 'string' | ||
? defaultAttributeParam.split( ',' ) | ||
: []; | ||
|
||
return defaultCheckedValue; | ||
} | ||
|
||
return []; | ||
}; | ||
|
||
export const isQueryArgsEqual = ( | ||
currentQueryArgs: QueryArgs, | ||
newQueryArgs: QueryArgs | ||
) => { | ||
// The user can add same two filter blocks for the same attribute. | ||
// We removed the query type from the check to avoid refresh loop. | ||
const filteredNewQueryArgs = Object.entries( newQueryArgs ).reduce( | ||
( acc, [ key, value ] ) => { | ||
return key.includes( 'query_type' ) | ||
? acc | ||
: { | ||
...acc, | ||
[ key ]: value, | ||
}; | ||
}, | ||
{} | ||
); | ||
|
||
return Object.entries( filteredNewQueryArgs ).reduce( | ||
( isEqual, [ key, value ] ) => | ||
currentQueryArgs[ key ] === value ? isEqual : false, | ||
true | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { getQueryArg } from '@wordpress/url'; | ||
|
||
/** | ||
* Returns specified parameter from URL | ||
* | ||
* @param {string} name Parameter you want the value of. | ||
*/ | ||
export function getUrlParameter( name: string ) { | ||
if ( ! window ) { | ||
return null; | ||
} | ||
return getQueryArg( window.location.href, name ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters