Remove authors filter from Product Collection block (https://github.com/woocommerce/woocommerce-blocks/pull/11427)
* Remove authors filter from Product Collection block * Remove author filed in query * Add back the author query argument in the final query builder --------- Co-authored-by: Manish Menaria <the.manish.menaria@gmail.com>
This commit is contained in:
parent
9cc4146c4d
commit
d74b0423e1
|
@ -40,7 +40,6 @@ export const DEFAULT_QUERY: ProductCollectionQuery = {
|
||||||
postType: 'product',
|
postType: 'product',
|
||||||
order: 'asc',
|
order: 'asc',
|
||||||
orderBy: 'title',
|
orderBy: 'title',
|
||||||
author: '',
|
|
||||||
search: '',
|
search: '',
|
||||||
exclude: [],
|
exclude: [],
|
||||||
inherit: null,
|
inherit: null,
|
||||||
|
|
|
@ -1,151 +0,0 @@
|
||||||
/**
|
|
||||||
* External dependencies
|
|
||||||
*/
|
|
||||||
import { __ } from '@wordpress/i18n';
|
|
||||||
import { useEntityRecords } from '@wordpress/core-data';
|
|
||||||
import {
|
|
||||||
FormTokenField,
|
|
||||||
// @ts-expect-error Using experimental features
|
|
||||||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
|
|
||||||
__experimentalToolsPanelItem as ToolsPanelItem,
|
|
||||||
} from '@wordpress/components';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Internal dependencies
|
|
||||||
*/
|
|
||||||
import { QueryControlProps } from '../types';
|
|
||||||
|
|
||||||
interface Author {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AuthorsInfo {
|
|
||||||
authors: Author[];
|
|
||||||
mapById: Map< number, Author >;
|
|
||||||
mapByName: Map< string, Author >;
|
|
||||||
names: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const AUTHORS_QUERY = {
|
|
||||||
who: 'authors',
|
|
||||||
per_page: -1,
|
|
||||||
_fields: 'id,name',
|
|
||||||
context: 'view',
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getAuthorsInfo = ( authors: Author[] ): AuthorsInfo => {
|
|
||||||
const mapById = new Map< number, Author >();
|
|
||||||
const mapByName = new Map< string, Author >();
|
|
||||||
const names: string[] = [];
|
|
||||||
|
|
||||||
authors.forEach( ( author ) => {
|
|
||||||
mapById.set( Number( author.id ), author );
|
|
||||||
mapByName.set( author.name, author );
|
|
||||||
names.push( author.name );
|
|
||||||
} );
|
|
||||||
|
|
||||||
return {
|
|
||||||
authors,
|
|
||||||
mapById,
|
|
||||||
mapByName,
|
|
||||||
names,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const getIdByValue = (
|
|
||||||
entitiesMappedByName: Map< string, Author >,
|
|
||||||
authorValue: string | Author
|
|
||||||
) => {
|
|
||||||
const id =
|
|
||||||
( authorValue as Author )?.id ||
|
|
||||||
entitiesMappedByName.get( authorValue as string )?.id;
|
|
||||||
if ( id ) return id;
|
|
||||||
};
|
|
||||||
|
|
||||||
function AuthorControl( { query, setQueryAttribute }: QueryControlProps ) {
|
|
||||||
const value = query.author;
|
|
||||||
const { records: authorsList, error } = useEntityRecords< Author[] >(
|
|
||||||
'root',
|
|
||||||
'user',
|
|
||||||
AUTHORS_QUERY
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( error ) {
|
|
||||||
return (
|
|
||||||
<ToolsPanelItem
|
|
||||||
label={ __( 'Authors', 'woo-gutenberg-products-block' ) }
|
|
||||||
hasValue={ () => true }
|
|
||||||
>
|
|
||||||
<FormTokenField
|
|
||||||
label={ __( 'Authors', 'woo-gutenberg-products-block' ) }
|
|
||||||
value={ [
|
|
||||||
__(
|
|
||||||
'Error occurred while loading authors.',
|
|
||||||
'woo-gutenberg-products-block'
|
|
||||||
),
|
|
||||||
] }
|
|
||||||
disabled={ true }
|
|
||||||
/>
|
|
||||||
</ToolsPanelItem>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! authorsList ) return null;
|
|
||||||
|
|
||||||
const authorsInfo = getAuthorsInfo( authorsList as Author[] );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* We need to normalize the value because the block operates on a
|
|
||||||
* comma(`,`) separated string value and `FormTokenFields` needs an
|
|
||||||
* array.
|
|
||||||
*
|
|
||||||
* Returns only the existing authors ids. This prevents the component
|
|
||||||
* from crashing in the editor, when non existing ids are provided.
|
|
||||||
*/
|
|
||||||
const sanitizedValue = value
|
|
||||||
? ( value
|
|
||||||
.split( ',' )
|
|
||||||
.map( ( authorId ) => {
|
|
||||||
const author = authorsInfo.mapById.get(
|
|
||||||
Number( authorId )
|
|
||||||
);
|
|
||||||
return author
|
|
||||||
? {
|
|
||||||
id: author.id,
|
|
||||||
value: author.name,
|
|
||||||
}
|
|
||||||
: null;
|
|
||||||
} )
|
|
||||||
.filter( Boolean ) as { id: string; value: string }[] )
|
|
||||||
: [];
|
|
||||||
|
|
||||||
const onAuthorChange = ( newValue: string[] ) => {
|
|
||||||
const ids = Array.from(
|
|
||||||
newValue.reduce( ( accumulator: Set< string >, author: string ) => {
|
|
||||||
// Verify that new values point to existing entities.
|
|
||||||
const id = getIdByValue( authorsInfo.mapByName, author );
|
|
||||||
if ( id ) accumulator.add( id );
|
|
||||||
return accumulator;
|
|
||||||
}, new Set() )
|
|
||||||
);
|
|
||||||
setQueryAttribute( { author: ids.join( ',' ) } );
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<ToolsPanelItem
|
|
||||||
hasValue={ () => !! value }
|
|
||||||
label={ __( 'Authors', 'woo-gutenberg-products-block' ) }
|
|
||||||
onDeselect={ () => setQueryAttribute( { author: '' } ) }
|
|
||||||
>
|
|
||||||
<FormTokenField
|
|
||||||
label={ __( 'Authors', 'woo-gutenberg-products-block' ) }
|
|
||||||
value={ sanitizedValue }
|
|
||||||
suggestions={ authorsInfo.names }
|
|
||||||
onChange={ onAuthorChange }
|
|
||||||
/>
|
|
||||||
</ToolsPanelItem>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default AuthorControl;
|
|
|
@ -38,7 +38,6 @@ import KeywordControl from './keyword-control';
|
||||||
import AttributesControl from './attributes-control';
|
import AttributesControl from './attributes-control';
|
||||||
import TaxonomyControls from './taxonomy-controls';
|
import TaxonomyControls from './taxonomy-controls';
|
||||||
import HandPickedProductsControl from './hand-picked-products-control';
|
import HandPickedProductsControl from './hand-picked-products-control';
|
||||||
import AuthorControl from './author-control';
|
|
||||||
import LayoutOptionsControl from './layout-options-control';
|
import LayoutOptionsControl from './layout-options-control';
|
||||||
|
|
||||||
const ProductCollectionInspectorControls = (
|
const ProductCollectionInspectorControls = (
|
||||||
|
@ -99,7 +98,6 @@ const ProductCollectionInspectorControls = (
|
||||||
<KeywordControl { ...queryControlProps } />
|
<KeywordControl { ...queryControlProps } />
|
||||||
<AttributesControl { ...queryControlProps } />
|
<AttributesControl { ...queryControlProps } />
|
||||||
<TaxonomyControls { ...queryControlProps } />
|
<TaxonomyControls { ...queryControlProps } />
|
||||||
<AuthorControl { ...queryControlProps } />
|
|
||||||
</ToolsPanel>
|
</ToolsPanel>
|
||||||
) : null }
|
) : null }
|
||||||
<ProductCollectionFeedbackPrompt />
|
<ProductCollectionFeedbackPrompt />
|
||||||
|
|
|
@ -29,7 +29,6 @@ export interface ProductCollectionDisplayLayout {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ProductCollectionQuery {
|
export interface ProductCollectionQuery {
|
||||||
author: string;
|
|
||||||
exclude: string[];
|
exclude: string[];
|
||||||
inherit: boolean | null;
|
inherit: boolean | null;
|
||||||
offset: number;
|
offset: number;
|
||||||
|
|
|
@ -77,7 +77,6 @@ const ProductTemplateEdit = ( {
|
||||||
offset = 0,
|
offset = 0,
|
||||||
order,
|
order,
|
||||||
orderBy,
|
orderBy,
|
||||||
author,
|
|
||||||
search,
|
search,
|
||||||
exclude,
|
exclude,
|
||||||
inherit,
|
inherit,
|
||||||
|
@ -155,9 +154,6 @@ const ProductTemplateEdit = ( {
|
||||||
if ( perPage ) {
|
if ( perPage ) {
|
||||||
query.per_page = perPage;
|
query.per_page = perPage;
|
||||||
}
|
}
|
||||||
if ( author ) {
|
|
||||||
query.author = author;
|
|
||||||
}
|
|
||||||
if ( search ) {
|
if ( search ) {
|
||||||
query.search = search;
|
query.search = search;
|
||||||
}
|
}
|
||||||
|
@ -186,7 +182,6 @@ const ProductTemplateEdit = ( {
|
||||||
order,
|
order,
|
||||||
orderBy,
|
orderBy,
|
||||||
clientId,
|
clientId,
|
||||||
author,
|
|
||||||
search,
|
search,
|
||||||
postType,
|
postType,
|
||||||
exclude,
|
exclude,
|
||||||
|
|
|
@ -207,7 +207,9 @@ class ProductCollection extends AbstractBlock {
|
||||||
$stock_status = $request->get_param( 'woocommerceStockStatus' );
|
$stock_status = $request->get_param( 'woocommerceStockStatus' );
|
||||||
$product_attributes = $request->get_param( 'woocommerceAttributes' );
|
$product_attributes = $request->get_param( 'woocommerceAttributes' );
|
||||||
$handpicked_products = $request->get_param( 'woocommerceHandPickedProducts' );
|
$handpicked_products = $request->get_param( 'woocommerceHandPickedProducts' );
|
||||||
$args['author'] = $request->get_param( 'author' ) ?? '';
|
// This argument is required for the tests to PHP Unit Tests to run correctly.
|
||||||
|
// Most likely this argument is being accessed in the test environment image.
|
||||||
|
$args['author'] = '';
|
||||||
|
|
||||||
return $this->get_final_query_args(
|
return $this->get_final_query_args(
|
||||||
$args,
|
$args,
|
||||||
|
@ -297,7 +299,6 @@ class ProductCollection extends AbstractBlock {
|
||||||
'tax_query' => array(),
|
'tax_query' => array(),
|
||||||
'paged' => $page,
|
'paged' => $page,
|
||||||
's' => $query['search'],
|
's' => $query['search'],
|
||||||
'author' => $query['author'] ?? '',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$is_on_sale = $query['woocommerceOnSale'] ?? false;
|
$is_on_sale = $query['woocommerceOnSale'] ?? false;
|
||||||
|
|
|
@ -29,7 +29,6 @@ class ProductCollection extends \WP_UnitTestCase {
|
||||||
'postType' => 'product',
|
'postType' => 'product',
|
||||||
'order' => 'desc',
|
'order' => 'desc',
|
||||||
'orderBy' => 'date',
|
'orderBy' => 'date',
|
||||||
'author' => '',
|
|
||||||
'search' => '',
|
'search' => '',
|
||||||
'exclude' => array(),
|
'exclude' => array(),
|
||||||
'sticky' => '',
|
'sticky' => '',
|
||||||
|
|
Loading…
Reference in New Issue