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 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows for being able to edit the text portion of the discount rendering as its own entity.
- Loading branch information
Showing
9 changed files
with
174 additions
and
33 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
56 changes: 27 additions & 29 deletions
56
assets/js/atomic/blocks/product-elements/price-v2/inner-blocks/discount/edit.tsx
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 |
---|---|---|
@@ -1,51 +1,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import type { HTMLAttributes, CSSProperties } from 'react'; | ||
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor'; | ||
import { useProductDataContext } from '@woocommerce/shared-context'; | ||
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format'; | ||
import { useStyleProps } from '@woocommerce/base-hooks'; | ||
import { useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DiscountBlock from './block'; | ||
import { discountAmountName } from './inner-blocks'; | ||
import { TEMPLATE } from './template'; | ||
|
||
interface Attributes { | ||
style: CSSProperties; | ||
} | ||
|
||
type Props = { | ||
attributes: Attributes; | ||
context?: { isDescendentOfSingleProductTemplate: boolean }; | ||
} & HTMLAttributes< HTMLDivElement >; | ||
|
||
const DiscountEdit = ( { attributes, context }: Props ): JSX.Element => { | ||
const blockProps = useBlockProps(); | ||
export const Edit = ( { attributes, setAttributes } ): JSX.Element => { | ||
const { style, className } = useStyleProps( attributes ); | ||
const { product } = useProductDataContext(); | ||
const { isDescendentOfSingleProductTemplate = false } = context || {}; | ||
const originalPrice = product?.prices?.regular_price; | ||
const currentPrice = product?.prices?.price; | ||
const showPrice = originalPrice && currentPrice !== originalPrice; | ||
const currency = isDescendentOfSingleProductTemplate | ||
? getCurrencyFromPriceResponse() | ||
: getCurrencyFromPriceResponse( product?.prices ); | ||
const blockAttrs = { | ||
attributes, | ||
currency, | ||
context, | ||
originalPrice, | ||
currentPrice, | ||
}; | ||
useEffect( () => { | ||
if ( ! attributes?.style ) { | ||
setAttributes( { style: { spacing: { blockGap: '0' } } } ); | ||
} | ||
}, [ attributes, setAttributes ] ); | ||
return ( | ||
<> | ||
{ showPrice && ( | ||
<div { ...blockProps }> | ||
<DiscountBlock { ...blockAttrs } /> | ||
<div className={ className } style={ style }> | ||
<InnerBlocks | ||
allowedBlocks={ [ | ||
discountAmountName, | ||
'core/paragraph', | ||
] } | ||
template={ TEMPLATE } | ||
/> | ||
</div> | ||
) } | ||
</> | ||
); | ||
}; | ||
|
||
export default DiscountEdit; | ||
export const Save = (): JSX.Element => { | ||
return ( | ||
<div { ...useBlockProps.save() }> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
}; |
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
14 changes: 14 additions & 0 deletions
14
...s/product-elements/price-v2/inner-blocks/discount/inner-blocks/discount-amount/block.json
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,14 @@ | ||
{ | ||
"name": "woocommerce/discount-amount", | ||
"version": "1.0.0", | ||
"icon": "info", | ||
"title": "Discount Amount", | ||
"description": "Display the discounted amount value.", | ||
"category": "woocommerce", | ||
"usesContext": ["woocommerce/isDescendantOfSingleProductTemplate"], | ||
"keywords": [ "WooCommerce" ], | ||
"textdomain": "woo-gutenberg-products-block", | ||
"ancestor": "woocommerce/discount", | ||
"apiVersion": 2, | ||
"$schema": "https://schemas.wp.org/trunk/block.json" | ||
} |
File renamed without changes.
51 changes: 51 additions & 0 deletions
51
...cks/product-elements/price-v2/inner-blocks/discount/inner-blocks/discount-amount/edit.tsx
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,51 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import type { HTMLAttributes, CSSProperties } from 'react'; | ||
import { useProductDataContext } from '@woocommerce/shared-context'; | ||
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DiscountBlock from './block'; | ||
|
||
interface Attributes { | ||
style: CSSProperties; | ||
} | ||
|
||
type Props = { | ||
attributes: Attributes; | ||
context?: { isDescendentOfSingleProductTemplate: boolean }; | ||
} & HTMLAttributes< HTMLDivElement >; | ||
|
||
const DiscountEdit = ( { attributes, context }: Props ): JSX.Element => { | ||
const blockProps = useBlockProps(); | ||
const { product } = useProductDataContext(); | ||
const { isDescendentOfSingleProductTemplate = false } = context || {}; | ||
const originalPrice = product?.prices?.regular_price; | ||
const currentPrice = product?.prices?.price; | ||
const showPrice = originalPrice && currentPrice !== originalPrice; | ||
const currency = isDescendentOfSingleProductTemplate | ||
? getCurrencyFromPriceResponse() | ||
: getCurrencyFromPriceResponse( product?.prices ); | ||
const blockAttrs = { | ||
attributes, | ||
currency, | ||
context, | ||
originalPrice, | ||
currentPrice, | ||
}; | ||
return ( | ||
<> | ||
{ showPrice && ( | ||
<div { ...blockProps }> | ||
<DiscountBlock { ...blockAttrs } /> | ||
</div> | ||
) } | ||
</> | ||
); | ||
}; | ||
|
||
export default DiscountEdit; |
27 changes: 27 additions & 0 deletions
27
...cks/product-elements/price-v2/inner-blocks/discount/inner-blocks/discount-amount/index.ts
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,27 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import edit from './edit'; | ||
import sharedConfig from '../../../../../shared/config'; | ||
import metadata from './block.json'; | ||
|
||
const { ancestor, ...configuration } = sharedConfig; | ||
|
||
const blockConfig = { | ||
...configuration, | ||
...metadata, | ||
edit, | ||
supports: { | ||
...configuration.supports, | ||
context: '', | ||
}, | ||
}; | ||
|
||
registerBlockType( metadata.name, blockConfig ); | ||
|
||
export const BLOCK_NAME = metadata.name; |
6 changes: 6 additions & 0 deletions
6
...s/js/atomic/blocks/product-elements/price-v2/inner-blocks/discount/inner-blocks/index.tsx
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,6 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './discount-amount/index.ts'; | ||
|
||
export { BLOCK_NAME as discountAmountName } from './discount-amount/index'; |
34 changes: 34 additions & 0 deletions
34
assets/js/atomic/blocks/product-elements/price-v2/inner-blocks/discount/template.ts
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,34 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { InnerBlockTemplate } from '@wordpress/blocks'; | ||
import { _x, sprintf } from '@wordpress/i18n'; | ||
|
||
// Some hackery to both make translation clearer and to allow for RTL languages | ||
// in default template layout. | ||
const translatedContent = sprintf( | ||
// translators: %s: discount amount. The rendered value will only allow for placeholder at beginning or end of string. | ||
_x( | ||
'%s off', | ||
'post text next to discount amount', | ||
'woo-gutenberg-products-block' | ||
), | ||
':placeholder:' | ||
); | ||
|
||
const templateContent = translatedContent.replace( ':placeholder:', '' ); | ||
|
||
const RTLtemplate: InnerBlockTemplate[] = [ | ||
[ 'core/paragraph', { content: templateContent }, [] ], | ||
[ 'woocommerce/discount-amount', {}, [] ], | ||
]; | ||
|
||
const LTRtemplate: InnerBlockTemplate[] = [ | ||
[ 'woocommerce/discount-amount', {}, [] ], | ||
[ 'core/paragraph', { content: templateContent }, [] ], | ||
]; | ||
|
||
export const TEMPLATE: InnerBlockTemplate[] = | ||
translatedContent.indexOf( ':placeholder:' ) === 0 | ||
? LTRtemplate | ||
: RTLtemplate; |