* Make extensions optional, not all filters will need to pass this through

For example the CartCouponSchema has no option for extensibility (and I don't think it's needed at any rate) so extensions will always be an empty object. Rather than explicitly specifying this when running the filter, we can let it default to an empty object.

* Add filter for coupon code
This commit is contained in:
Thomas Roberts 2021-05-11 13:57:02 +01:00 committed by GitHub
parent f97ee971af
commit 1dcbddc0bd
2 changed files with 49 additions and 31 deletions

View File

@ -5,7 +5,11 @@ import { __, sprintf } from '@wordpress/i18n';
import LoadingMask from '@woocommerce/base-components/loading-mask'; import LoadingMask from '@woocommerce/base-components/loading-mask';
import { RemovableChip } from '@woocommerce/base-components/chip'; import { RemovableChip } from '@woocommerce/base-components/chip';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { TotalsItem } from '@woocommerce/blocks-checkout'; import {
__experimentalApplyCheckoutFilter,
mustBeString,
TotalsItem,
} from '@woocommerce/blocks-checkout';
import { getSetting } from '@woocommerce/settings'; import { getSetting } from '@woocommerce/settings';
/** /**
@ -53,18 +57,31 @@ const TotalsDiscount = ( {
showSpinner={ false } showSpinner={ false }
> >
<ul className="wc-block-components-totals-discount__coupon-list"> <ul className="wc-block-components-totals-discount__coupon-list">
{ cartCoupons.map( ( cartCoupon ) => ( { cartCoupons.map( ( cartCoupon ) => {
const filteredCouponCode = __experimentalApplyCheckoutFilter(
{
validation: mustBeString,
arg: {
context: 'summary',
coupon: cartCoupon,
},
filterName: 'couponName',
defaultValue: cartCoupon.code,
}
);
return (
<RemovableChip <RemovableChip
key={ 'coupon-' + cartCoupon.code } key={ 'coupon-' + cartCoupon.code }
className="wc-block-components-totals-discount__coupon-list-item" className="wc-block-components-totals-discount__coupon-list-item"
text={ cartCoupon.code } text={ filteredCouponCode }
screenReaderText={ sprintf( screenReaderText={ sprintf(
/* translators: %s Coupon code. */ /* translators: %s Coupon code. */
__( __(
'Coupon: %s', 'Coupon: %s',
'woo-gutenberg-products-block' 'woo-gutenberg-products-block'
), ),
cartCoupon.code filteredCouponCode
) } ) }
disabled={ isRemovingCoupon } disabled={ isRemovingCoupon }
onRemove={ () => { onRemove={ () => {
@ -77,10 +94,11 @@ const TotalsDiscount = ( {
'Remove coupon "%s"', 'Remove coupon "%s"',
'woo-gutenberg-products-block' 'woo-gutenberg-products-block'
), ),
cartCoupon.code filteredCouponCode
) } ) }
/> />
) ) } );
} ) }
</ul> </ul>
</LoadingMask> </LoadingMask>
) )

View File

@ -61,7 +61,7 @@ const getCheckoutFilters = ( filterName: string ): CheckoutFilterFunction[] => {
export const __experimentalApplyCheckoutFilter = < T >( { export const __experimentalApplyCheckoutFilter = < T >( {
filterName, filterName,
defaultValue, defaultValue,
extensions, extensions = {},
arg = null, arg = null,
validation = returnTrue, validation = returnTrue,
}: { }: {
@ -70,7 +70,7 @@ export const __experimentalApplyCheckoutFilter = < T >( {
/** Default value to filter. */ /** Default value to filter. */
defaultValue: T; defaultValue: T;
/** Values extend to REST API response. */ /** Values extend to REST API response. */
extensions: Record< string, unknown >; extensions?: Record< string, unknown >;
/** Object containing arguments for the filter function. */ /** Object containing arguments for the filter function. */
arg: CheckoutFilterArguments; arg: CheckoutFilterArguments;
/** Function that needs to return true when the filtered value is passed in order for the filter to be applied. */ /** Function that needs to return true when the filtered value is passed in order for the filter to be applied. */