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

Commit 0365ba9

Browse files
authored
Fix: Convert Filter Products by Price Block to TypeScript (#6514)
1 parent a2682b4 commit 0365ba9

File tree

12 files changed

+163
-79
lines changed

12 files changed

+163
-79
lines changed

assets/js/base/components/price-slider/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ export interface PriceSliderProps {
3333
/**
3434
* Maximum constraint.
3535
*/
36-
maxConstraint: number;
36+
maxConstraint: number | null | undefined;
3737
/**
3838
* Maximum price for slider.
3939
*/
40-
maxPrice: number;
40+
maxPrice: number | null;
4141
/**
4242
* Minimum constraint.
4343
*/
44-
minConstraint: number;
44+
minConstraint: number | null | undefined;
4545
/**
4646
* Minimum price for slider.
4747
*/
48-
minPrice: number;
48+
minPrice: number | null;
4949
/**
5050
* Function to call on the change event.
5151
*/
@@ -65,7 +65,7 @@ export interface PriceSliderProps {
6565
/**
6666
* What step values the slider uses.
6767
*/
68-
step: number;
68+
step?: number;
6969
}
7070

7171
const PriceSlider = ( {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { __ } from '@wordpress/i18n';
5+
6+
export const blockAttributes = {
7+
heading: {
8+
type: 'string',
9+
default: __( 'Filter by price', 'woo-gutenberg-products-block' ),
10+
},
11+
};

assets/js/blocks/price-filter/block.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
}
2020
},
2121
"attributes": {
22+
"className": {
23+
"type": "string",
24+
"default": ""
25+
},
2226
"showInputFields": {
2327
"type": "boolean",
2428
"default": true
Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ import PropTypes from 'prop-types';
1414
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
1515
import { getSettingWithCoercion } from '@woocommerce/settings';
1616
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
17-
import { isBoolean } from '@woocommerce/types';
17+
import {
18+
CurrencyResponse,
19+
isBoolean,
20+
isString,
21+
objectHasProp,
22+
} from '@woocommerce/types';
1823

1924
/**
2025
* Internal dependencies
2126
*/
22-
import usePriceConstraints from './use-price-constraints.js';
27+
import usePriceConstraints from './use-price-constraints';
2328
import { getUrlParameter } from '../../utils/filters';
2429
import './style.scss';
30+
import { Attributes } from './types';
2531

2632
/**
2733
* Formats filter values into a string for the URL parameters needed for filtering PHP templates.
@@ -31,8 +37,11 @@ import './style.scss';
3137
*
3238
* @return {string} New URL with query parameters in it.
3339
*/
34-
function formatParams( url, params ) {
35-
const paramObject = {};
40+
function formatParams(
41+
url: string,
42+
params: Record< string, string | number >
43+
) {
44+
const paramObject: Record< string, string > = {};
3645

3746
for ( const [ key, value ] of Object.entries( params ) ) {
3847
if ( value ) {
@@ -51,13 +60,13 @@ function formatParams( url, params ) {
5160
/**
5261
* Formats price values taking into account precision
5362
*
54-
* @param {string} value
55-
* @param {number} minorUnit
63+
* @param {string|void} value
64+
* @param {number} minorUnit
5665
*
5766
* @return {number} Formatted price.
5867
*/
5968

60-
function formatPrice( value, minorUnit ) {
69+
function formatPrice( value: unknown, minorUnit: number ) {
6170
return Number( value ) * 10 ** minorUnit;
6271
}
6372

@@ -68,7 +77,13 @@ function formatPrice( value, minorUnit ) {
6877
* @param {Object} props.attributes Incoming block attributes.
6978
* @param {boolean} props.isEditor Whether in editor context or not.
7079
*/
71-
const PriceFilterBlock = ( { attributes, isEditor = false } ) => {
80+
const PriceFilterBlock = ( {
81+
attributes,
82+
isEditor = false,
83+
}: {
84+
attributes: Attributes;
85+
isEditor: boolean;
86+
} ) => {
7287
const hasFilterableProducts = getSettingWithCoercion(
7388
'has_filterable_products',
7489
false,
@@ -97,7 +112,11 @@ const PriceFilterBlock = ( { attributes, isEditor = false } ) => {
97112
queryState,
98113
} );
99114

100-
const currency = getCurrencyFromPriceResponse( results.price_range );
115+
const currency = getCurrencyFromPriceResponse(
116+
objectHasProp( results, 'price_range' )
117+
? ( results.price_range as CurrencyResponse )
118+
: undefined
119+
);
101120

102121
const [ minPriceQuery, setMinPriceQuery ] = useQueryStateByKey(
103122
'min_price',
@@ -116,12 +135,18 @@ const PriceFilterBlock = ( { attributes, isEditor = false } ) => {
116135
);
117136

118137
const { minConstraint, maxConstraint } = usePriceConstraints( {
119-
minPrice: results.price_range
120-
? results.price_range.min_price
121-
: undefined,
122-
maxPrice: results.price_range
123-
? results.price_range.max_price
124-
: undefined,
138+
minPrice:
139+
objectHasProp( results, 'price_range' ) &&
140+
objectHasProp( results.price_range, 'min_price' ) &&
141+
isString( results.price_range.min_price )
142+
? results.price_range.min_price
143+
: undefined,
144+
maxPrice:
145+
objectHasProp( results, 'price_range' ) &&
146+
objectHasProp( results.price_range, 'max_price' ) &&
147+
isString( results.price_range.max_price )
148+
? results.price_range.max_price
149+
: undefined,
125150
minorUnit: currency.minorUnit,
126151
} );
127152

@@ -290,7 +315,7 @@ const PriceFilterBlock = ( { attributes, isEditor = false } ) => {
290315
return null;
291316
}
292317

293-
const TagName = `h${ attributes.headingLevel }`;
318+
const TagName = `h${ attributes.headingLevel }` as keyof JSX.IntrinsicElements;
294319

295320
return (
296321
<>
File renamed without changes.
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { blocksConfig } from '@woocommerce/block-settings';
88
import HeadingToolbar from '@woocommerce/editor-components/heading-toolbar';
99
import BlockTitle from '@woocommerce/editor-components/block-title';
1010
import { Icon, currencyDollar, external } from '@wordpress/icons';
11+
import type { BlockEditProps } from '@wordpress/blocks';
1112
import {
1213
Placeholder,
1314
Disabled,
@@ -23,10 +24,14 @@ import {
2324
/**
2425
* Internal dependencies
2526
*/
26-
import Block from './block.js';
27+
import Block from './block';
2728
import './editor.scss';
29+
import type { Attributes } from './types';
2830

29-
export default function ( { attributes, setAttributes } ) {
31+
export default function ( {
32+
attributes,
33+
setAttributes,
34+
}: BlockEditProps< Attributes > ) {
3035
const {
3136
heading,
3237
headingLevel,
@@ -51,7 +56,7 @@ export default function ( { attributes, setAttributes } ) {
5156
'woo-gutenberg-products-block'
5257
) }
5358
value={ showInputFields ? 'editable' : 'text' }
54-
onChange={ ( value ) =>
59+
onChange={ ( value: string ) =>
5560
setAttributes( {
5661
showInputFields: value === 'editable',
5762
} )
@@ -106,7 +111,7 @@ export default function ( { attributes, setAttributes } ) {
106111
minLevel={ 2 }
107112
maxLevel={ 7 }
108113
selectedLevel={ headingLevel }
109-
onChange={ ( newLevel ) =>
114+
onChange={ ( newLevel: number ) =>
110115
setAttributes( { headingLevel: newLevel } )
111116
}
112117
/>
@@ -164,7 +169,7 @@ export default function ( { attributes, setAttributes } ) {
164169
className="wc-block-price-filter__title"
165170
headingLevel={ headingLevel }
166171
heading={ heading }
167-
onChange={ ( value ) =>
172+
onChange={ ( value: string ) =>
168173
setAttributes( { heading: value } )
169174
}
170175
/>
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import { renderFrontend } from '@woocommerce/base-utils';
66
/**
77
* Internal dependencies
88
*/
9-
import Block from './block.js';
9+
import Block from './block';
10+
import metadata from './block.json';
11+
import { blockAttributes } from './attributes';
1012

11-
const getProps = ( el ) => {
13+
const getProps = ( el: HTMLElement ) => {
1214
return {
1315
attributes: {
1416
showInputFields: el.dataset.showinputfields === 'true',
1517
showFilterButton: el.dataset.showfilterbutton === 'true',
18+
heading: el.dataset.heading || blockAttributes.heading.default,
19+
headingLevel: el.dataset.headingLevel
20+
? parseInt( el.dataset.headingLevel, 10 )
21+
: metadata.attributes.headingLevel.default,
1622
},
1723
isEditor: false,
1824
};
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { useBlockProps } from '@wordpress/block-editor';
1111
/**
1212
* Internal dependencies
1313
*/
14-
import edit from './edit.js';
14+
import edit from './edit';
1515
import metadata from './block.json';
16+
import { blockAttributes } from './attributes';
1617

1718
registerBlockType( metadata, {
1819
title: __( 'Filter Products by Price', 'woo-gutenberg-products-block' ),
@@ -40,10 +41,7 @@ registerBlockType( metadata, {
4041
},
4142
attributes: {
4243
...metadata.attributes,
43-
heading: {
44-
type: 'string',
45-
default: __( 'Filter by price', 'woo-gutenberg-products-block' ),
46-
},
44+
...blockAttributes,
4745
},
4846
transforms: {
4947
from: [
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface Attributes {
2+
showFilterButton: boolean;
3+
showInputFields: boolean;
4+
heading: string;
5+
headingLevel: number;
6+
className?: string;
7+
}

assets/js/blocks/price-filter/use-price-constraints.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)