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:
Karol Manijak 2023-10-25 19:11:56 +02:00 committed by GitHub
parent 9cc4146c4d
commit d74b0423e1
7 changed files with 3 additions and 163 deletions

View File

@ -40,7 +40,6 @@ export const DEFAULT_QUERY: ProductCollectionQuery = {
postType: 'product',
order: 'asc',
orderBy: 'title',
author: '',
search: '',
exclude: [],
inherit: null,

View File

@ -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;

View File

@ -38,7 +38,6 @@ import KeywordControl from './keyword-control';
import AttributesControl from './attributes-control';
import TaxonomyControls from './taxonomy-controls';
import HandPickedProductsControl from './hand-picked-products-control';
import AuthorControl from './author-control';
import LayoutOptionsControl from './layout-options-control';
const ProductCollectionInspectorControls = (
@ -99,7 +98,6 @@ const ProductCollectionInspectorControls = (
<KeywordControl { ...queryControlProps } />
<AttributesControl { ...queryControlProps } />
<TaxonomyControls { ...queryControlProps } />
<AuthorControl { ...queryControlProps } />
</ToolsPanel>
) : null }
<ProductCollectionFeedbackPrompt />

View File

@ -29,7 +29,6 @@ export interface ProductCollectionDisplayLayout {
}
export interface ProductCollectionQuery {
author: string;
exclude: string[];
inherit: boolean | null;
offset: number;

View File

@ -77,7 +77,6 @@ const ProductTemplateEdit = ( {
offset = 0,
order,
orderBy,
author,
search,
exclude,
inherit,
@ -155,9 +154,6 @@ const ProductTemplateEdit = ( {
if ( perPage ) {
query.per_page = perPage;
}
if ( author ) {
query.author = author;
}
if ( search ) {
query.search = search;
}
@ -186,7 +182,6 @@ const ProductTemplateEdit = ( {
order,
orderBy,
clientId,
author,
search,
postType,
exclude,

View File

@ -207,7 +207,9 @@ class ProductCollection extends AbstractBlock {
$stock_status = $request->get_param( 'woocommerceStockStatus' );
$product_attributes = $request->get_param( 'woocommerceAttributes' );
$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(
$args,
@ -297,7 +299,6 @@ class ProductCollection extends AbstractBlock {
'tax_query' => array(),
'paged' => $page,
's' => $query['search'],
'author' => $query['author'] ?? '',
);
$is_on_sale = $query['woocommerceOnSale'] ?? false;

View File

@ -29,7 +29,6 @@ class ProductCollection extends \WP_UnitTestCase {
'postType' => 'product',
'order' => 'desc',
'orderBy' => 'date',
'author' => '',
'search' => '',
'exclude' => array(),
'sticky' => '',