Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Convert product-elements/price to TypeScript #7683

Merged
merged 84 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
cc2a0c2
Convert product-element/price to TypeScript
nielslange Oct 31, 2022
e53fb67
Apply feedback from #7095 to this PR
nielslange Oct 31, 2022
ded42d7
Merge branch 'trunk' into update/7092-convert-product-element-price-t…
nielslange Oct 31, 2022
4084e25
Merge branch 'trunk' into update/7092-convert-product-element-price-t…
nielslange Nov 1, 2022
64684d4
Export block due to Cross-Sells dependency
nielslange Nov 2, 2022
1687c43
Update assets/js/atomic/blocks/product-elements/price/edit.tsx
nielslange Nov 3, 2022
609561f
Merge branch 'trunk' into update/7092-convert-product-element-price-t…
nielslange Nov 3, 2022
143c111
bot: update checkstyle.xml
github-actions[bot] Nov 3, 2022
10fe20e
Apply review feedback
nielslange Nov 3, 2022
e6389b6
Outsource supports section
nielslange Nov 3, 2022
136e0fb
Merge branch 'update/7092-convert-product-element-price-to-TS' of git…
nielslange Nov 3, 2022
81dabd5
bot: update checkstyle.xml
github-actions[bot] Nov 3, 2022
bfa0594
Merge branch 'trunk' into update/7092-convert-product-element-price-t…
nielslange Nov 7, 2022
2bd19b5
Resolve merge conflicts
nielslange Nov 15, 2022
8e8b4a0
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 15, 2022
239e603
bot: update checkstyle.xml
github-actions[bot] Nov 15, 2022
31534b5
Solve TS error in cart-cross-sells-product.tsx
nielslange Nov 15, 2022
9e1c763
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 15, 2022
ae10c57
bot: update checkstyle.xml
github-actions[bot] Nov 15, 2022
2f699ca
Solve TS error regarding min_amount and max_amount
nielslange Nov 16, 2022
c6bae5a
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Nov 16, 2022
0ff8471
bot: update checkstyle.xml
github-actions[bot] Nov 16, 2022
4972a43
Empty-Commit
nielslange Nov 16, 2022
06d022c
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 16, 2022
31da04f
Fix TS problems in product-elements/price/block.tsx
nielslange Nov 16, 2022
864d6ac
bot: update checkstyle.xml
github-actions[bot] Nov 16, 2022
451da01
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Nov 16, 2022
cf3d57e
Solve TS errors
nielslange Nov 16, 2022
1ed36b8
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Nov 16, 2022
42f1a9e
bot: update checkstyle.xml
github-actions[bot] Nov 16, 2022
943e0b1
Solve TS errors
nielslange Nov 16, 2022
54adbc8
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 16, 2022
e1d9ac4
bot: update checkstyle.xml
github-actions[bot] Nov 16, 2022
c310c8f
Solve TS errors
nielslange Nov 16, 2022
fc8bf18
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 16, 2022
6dd5afb
bot: update checkstyle.xml
github-actions[bot] Nov 16, 2022
1092da3
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Nov 18, 2022
9e101a9
bot: update checkstyle.xml
github-actions[bot] Nov 18, 2022
272b7c9
Resolve merge conflicts
nielslange Nov 15, 2022
de87d05
Convert product-element/price to TypeScript
nielslange Nov 18, 2022
f1789ce
Apply feedback from #7095 to this PR
nielslange Nov 18, 2022
6fc405b
Export block due to Cross-Sells dependency
nielslange Nov 18, 2022
48607ed
Apply review feedback
nielslange Nov 18, 2022
96e2f1a
Update assets/js/atomic/blocks/product-elements/price/edit.tsx
nielslange Nov 18, 2022
b0dc7c3
bot: update checkstyle.xml
nielslange Nov 18, 2022
a27b66d
bot: update checkstyle.xml
nielslange Nov 18, 2022
bc901de
Solve TS error in cart-cross-sells-product.tsx
nielslange Nov 15, 2022
3f8be3f
bot: update checkstyle.xml
nielslange Nov 18, 2022
250b561
bot: update checkstyle.xml
nielslange Nov 18, 2022
7bf8194
Solve TS error regarding min_amount and max_amount
nielslange Nov 18, 2022
a4efbd2
Empty-Commit
nielslange Nov 16, 2022
30bd5b3
bot: update checkstyle.xml
nielslange Nov 18, 2022
f8d870b
Fix TS problems in product-elements/price/block.tsx
nielslange Nov 16, 2022
787d4b8
bot: update checkstyle.xml
nielslange Nov 18, 2022
ba80e47
bot: update checkstyle.xml
nielslange Nov 18, 2022
1e877cc
Solve TS errors
nielslange Nov 16, 2022
cf48509
Solve TS errors
nielslange Nov 16, 2022
3f87035
bot: update checkstyle.xml
nielslange Nov 18, 2022
99ccf60
Solve TS errors
nielslange Nov 16, 2022
bfd2c9e
bot: update checkstyle.xml
nielslange Nov 18, 2022
011f66e
bot: update checkstyle.xml
nielslange Nov 18, 2022
d67011a
bot: update checkstyle.xml
nielslange Nov 18, 2022
2ba1f41
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 18, 2022
1a3df0d
Empty checkstyle.xml
nielslange Nov 18, 2022
65cecfa
bot: update checkstyle.xml
github-actions[bot] Nov 18, 2022
37884b2
Solve TS errors
nielslange Nov 18, 2022
9567d58
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 18, 2022
fb12f2d
bot: update checkstyle.xml
github-actions[bot] Nov 18, 2022
2b86d96
Solve TS errors
nielslange Nov 18, 2022
2ea7d66
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Nov 18, 2022
0cfec28
bot: update checkstyle.xml
github-actions[bot] Nov 18, 2022
91e7b12
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Nov 19, 2022
d068e31
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Dec 21, 2022
edd5ecf
Use BlockAttributes from @wordpress/blocks
nielslange Dec 21, 2022
308d901
Fix TS error
nielslange Dec 21, 2022
989e955
Fix TS errors
nielslange Dec 21, 2022
02ec49b
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Dec 22, 2022
121701a
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Dec 22, 2022
60fa85d
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Dec 22, 2022
270f1d6
Fix TS error
nielslange Dec 22, 2022
4fdbda4
Merge branch 'update/7092-convert-product-elements-price-to-TS' of gi…
nielslange Dec 22, 2022
66df464
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Dec 23, 2022
3ce2cb2
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Dec 23, 2022
9a648ec
Merge branch 'trunk' into update/7092-convert-product-elements-price-…
nielslange Dec 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* External dependencies
*/
import { BlockAttributes } from '@wordpress/blocks';

export const blockAttributes = {
export const blockAttributes: BlockAttributes = {
productId: {
type: 'number',
default: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ProductPrice from '@woocommerce/base-components/product-price';
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
Expand All @@ -11,22 +10,32 @@ import {
} from '@woocommerce/shared-context';
import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import type { HTMLAttributes } from 'react';
import { CurrencyCode } from '@woocommerce/type-defs/currency';

/**
* Internal dependencies
*/
import type { BlockAttributes } from './types';
import './style.scss';

/**
* Product Price Block Component.
*
* @param {Object} props Incoming props.
* @param {string} [props.className] CSS Class name for the component.
* @param {string} [props.textAlign] Text alignment.
* context will be used if this is not provided.
* @return {*} The component.
*/
export const Block = ( props ) => {
type Props = BlockAttributes & HTMLAttributes< HTMLDivElement >;

interface PriceProps {
currency_code: CurrencyCode;
currency_symbol: string;
currency_minor_unit: number;
currency_decimal_separator: string;
currency_thousand_separator: string;
currency_prefix: string;
currency_suffix: string;
price: string;
regular_price: string;
sale_price: string;
price_range: null | { min_amount: string; max_amount: string };
}

export const Block = ( props: Props ): JSX.Element | null => {
const { className, textAlign } = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
Expand Down Expand Up @@ -54,7 +63,7 @@ export const Block = ( props ) => {
);
}

const prices = product.prices;
const prices: PriceProps = product.prices;
const currency = getCurrencyFromPriceResponse( prices );
const isOnSale = prices.price !== prices.regular_price;
const priceClassName = classnames( {
Expand Down Expand Up @@ -84,10 +93,4 @@ export const Block = ( props ) => {
);
};

Block.propTypes = {
className: PropTypes.string,
product: PropTypes.object,
textAlign: PropTypes.oneOf( [ 'left', 'right', 'center' ] ),
};

export default withProductDataContext( Block );
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import { __ } from '@wordpress/i18n';
import { currencyDollar, Icon } from '@wordpress/icons';

export const BLOCK_TITLE = __(
export const BLOCK_TITLE: string = __(
'Product Price',
'woo-gutenberg-products-block'
);
export const BLOCK_ICON = (
export const BLOCK_ICON: JSX.Element = (
<Icon
icon={ currencyDollar }
className="wc-block-editor-components-block-icon"
/>
);
export const BLOCK_DESCRIPTION = __(
export const BLOCK_DESCRIPTION: string = __(
'Display the price of a product.',
'woo-gutenberg-products-block'
);
58 changes: 0 additions & 58 deletions assets/js/atomic/blocks/product-elements/price/edit.js

This file was deleted.

78 changes: 78 additions & 0 deletions assets/js/atomic/blocks/product-elements/price/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* External dependencies
*/
import {
AlignmentToolbar,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import { useEffect } from 'react';
import type { BlockAlignment } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import Block from './block';
import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE as label, BLOCK_ICON as icon } from './constants';

type UnsupportedAligments = 'wide' | 'full';
type AllowedAlignments = Exclude< BlockAlignment, UnsupportedAligments >;

interface BlockAttributes {
textAlign?: AllowedAlignments;
}

interface Attributes {
textAlign: 'left' | 'center' | 'right';
}

interface Context {
queryId: number;
}

interface Props {
attributes: Attributes;
setAttributes: (
attributes: Partial< BlockAttributes > & Record< string, unknown >
) => void;
context: Context;
}

const PriceEdit = ( {
attributes,
setAttributes,
context,
}: Props ): JSX.Element => {
const blockProps = useBlockProps();
const blockAttrs = {
...attributes,
...context,
};
const isDescendentOfQueryLoop = Number.isFinite( context.queryId );

useEffect(
() => setAttributes( { isDescendentOfQueryLoop } ),
[ setAttributes, isDescendentOfQueryLoop ]
);

return (
<>
<BlockControls>
{ isDescendentOfQueryLoop && (
<AlignmentToolbar
value={ attributes.textAlign }
onChange={ ( textAlign: AllowedAlignments ) => {
setAttributes( { textAlign } );
} }
/>
) }
</BlockControls>
<div { ...blockProps }>
<Block { ...blockAttrs } />
</div>
</>
);
};

export default withProductSelector( { icon, label } )( PriceEdit );
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
/**
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { registerBlockType } from '@wordpress/blocks';
import type { BlockConfiguration } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import sharedConfig from '../shared/config';
import edit from './edit';
import attributes from './attributes';
import { supports } from './supports';
import {
BLOCK_TITLE as title,
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';

const blockConfig = {
type CustomBlockConfiguration = BlockConfiguration & {
ancestor: string[];
};
nielslange marked this conversation as resolved.
Show resolved Hide resolved

const blockConfig: CustomBlockConfiguration = {
...sharedConfig,
apiVersion: 2,
title,
Expand All @@ -29,25 +34,8 @@ const blockConfig = {
usesContext: [ 'query', 'queryId', 'postId' ],
icon: { src: icon },
attributes,
supports,
edit,
supports: {
...sharedConfig.supports,
...( isFeaturePluginBuild() && {
color: {
text: true,
background: true,
link: false,
__experimentalSkipSerialization: true,
},
typography: {
fontSize: true,
__experimentalFontWeight: true,
__experimentalFontStyle: true,
__experimentalSkipSerialization: true,
},
__experimentalSelector: '.wc-block-components-product-price',
} ),
},
};

registerBlockType( 'woocommerce/product-price', blockConfig );
28 changes: 28 additions & 0 deletions assets/js/atomic/blocks/product-elements/price/supports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';

/**
* Internal dependencies
*/
import sharedConfig from '../shared/config';

export const supports = {
...sharedConfig.supports,
...( isFeaturePluginBuild() && {
color: {
text: true,
background: false,
link: false,
__experimentalSkipSerialization: true,
},
typography: {
fontSize: true,
__experimentalFontWeight: true,
__experimentalFontStyle: true,
__experimentalSkipSerialization: true,
},
__experimentalSelector: '.wc-block-components-product-price',
} ),
};
6 changes: 6 additions & 0 deletions assets/js/atomic/blocks/product-elements/price/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface BlockAttributes {
productId?: number;
className?: string;
textAlign?: 'left' | 'center' | 'right';
isDescendentOfQueryLoop?: boolean;
Comment on lines +2 to +5
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attributes are optional to prevent a TS error in the following section:

}
6 changes: 5 additions & 1 deletion assets/js/atomic/blocks/product-elements/sale-badge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
} from './constants';
import { supports } from './support';

const blockConfig: BlockConfiguration = {
type CustomBlockConfiguration = BlockConfiguration & {
ancestor: string[];
};

const blockConfig: CustomBlockConfiguration = {
...sharedConfig,
title,
description,
Expand Down
4 changes: 2 additions & 2 deletions assets/js/atomic/blocks/product-elements/sale-badge/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface BlockAttributes {
productId: number;
align: 'left' | 'center' | 'right';
productId?: number;
align?: 'left' | 'center' | 'right';
Comment on lines +2 to +3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part had been changed to prevent a TS error in the following section:

isDescendentOfQueryLoop: boolean;
}
12 changes: 7 additions & 5 deletions assets/js/atomic/blocks/product-elements/sku/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
BLOCK_DESCRIPTION as description,
} from './constants';

const blockConfig: BlockConfiguration = {
type CustomBlockConfiguration = BlockConfiguration & {
ancestor: string[];
};

const blockConfig: CustomBlockConfiguration = {
...sharedConfig,
apiVersion: 2,
title,
description,
Expand All @@ -31,7 +36,4 @@ const blockConfig: BlockConfiguration = {
edit,
};

registerBlockType( 'woocommerce/product-sku', {
...sharedConfig,
...blockConfig,
} );
registerBlockType( 'woocommerce/product-sku', { ...blockConfig } );
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
BLOCK_DESCRIPTION as description,
} from './constants';

const blockConfig: BlockConfiguration = {
type CustomBlockConfiguration = BlockConfiguration & {
ancestor: string[];
};

const blockConfig: CustomBlockConfiguration = {
...sharedConfig,
apiVersion: 2,
title,
Expand Down
Loading