@@ -10,13 +10,19 @@ import {
1010 useCollectionData ,
1111} from '@woocommerce/base-context/hooks' ;
1212import { getSetting , getSettingWithCoercion } from '@woocommerce/settings' ;
13- import { useCallback , useEffect , useState , useMemo } from '@wordpress/element' ;
13+ import {
14+ useCallback ,
15+ useEffect ,
16+ useState ,
17+ useMemo ,
18+ useRef ,
19+ } from '@wordpress/element' ;
1420import CheckboxList from '@woocommerce/base-components/checkbox-list' ;
1521import FilterSubmitButton from '@woocommerce/base-components/filter-submit-button' ;
1622import Label from '@woocommerce/base-components/filter-element-label' ;
1723import isShallowEqual from '@wordpress/is-shallow-equal' ;
1824import { decodeEntities } from '@wordpress/html-entities' ;
19- import { isBoolean } from '@woocommerce/types' ;
25+ import { isBoolean , objectHasProp } from '@woocommerce/types' ;
2026import { addQueryArgs , removeQueryArgs } from '@wordpress/url' ;
2127import { PREFIX_QUERY_ARG_FILTER_TYPE } from '@woocommerce/utils' ;
2228
@@ -26,6 +32,7 @@ import { PREFIX_QUERY_ARG_FILTER_TYPE } from '@woocommerce/utils';
2632import { previewOptions } from './preview' ;
2733import './style.scss' ;
2834import { getActiveFilters } from './utils' ;
35+ import { Attributes , DisplayOption } from './types' ;
2936
3037export const QUERY_PARAM_KEY = PREFIX_QUERY_ARG_FILTER_TYPE + 'stock_status' ;
3138
@@ -39,6 +46,9 @@ export const QUERY_PARAM_KEY = PREFIX_QUERY_ARG_FILTER_TYPE + 'stock_status';
3946const StockStatusFilterBlock = ( {
4047 attributes : blockAttributes ,
4148 isEditor = false ,
49+ } : {
50+ attributes : Attributes ;
51+ isEditor ?: boolean ;
4252} ) => {
4353 const filteringForPhpTemplate = getSettingWithCoercion (
4454 'is_rendering_php_template' ,
@@ -50,27 +60,26 @@ const StockStatusFilterBlock = ( {
5060 false
5161 ) ;
5262
53- const [ hideOutOfStockItems ] = useState (
54- getSetting ( 'hideOutOfStockItems' , false )
63+ const { outofstock, ...otherStockStatusOptions } = getSetting (
64+ 'stockStatusOptions' ,
65+ { }
5566 ) ;
56- const [ { outofstock, ...otherStockStatusOptions } ] = useState (
57- getSetting ( 'stockStatusOptions' , { } )
58- ) ;
59- const [ STOCK_STATUS_OPTIONS ] = useState (
60- hideOutOfStockItems
67+
68+ const STOCK_STATUS_OPTIONS = useRef (
69+ getSetting ( 'hideOutOfStockItems' , false )
6170 ? otherStockStatusOptions
6271 : { outofstock, ...otherStockStatusOptions }
6372 ) ;
6473
6574 const [ checked , setChecked ] = useState (
66- getActiveFilters ( STOCK_STATUS_OPTIONS , QUERY_PARAM_KEY )
75+ getActiveFilters ( STOCK_STATUS_OPTIONS . current , QUERY_PARAM_KEY )
6776 ) ;
6877 const [ displayedOptions , setDisplayedOptions ] = useState (
6978 blockAttributes . isPreview ? previewOptions : [ ]
7079 ) ;
7180 // Filter added to handle if there are slugs without a corresponding name defined.
7281 const [ initialOptions ] = useState (
73- Object . entries ( STOCK_STATUS_OPTIONS )
82+ Object . entries ( STOCK_STATUS_OPTIONS . current )
7483 . map ( ( [ slug , name ] ) => ( { slug, name } ) )
7584 . filter ( ( status ) => ! ! status . name )
7685 . sort ( ( a , b ) => a . slug . localeCompare ( b . slug ) )
@@ -95,7 +104,10 @@ const StockStatusFilterBlock = ( {
95104 */
96105 const getFilteredStock = useCallback (
97106 ( slug ) => {
98- if ( ! filteredCounts . stock_status_counts ) {
107+ if (
108+ ! objectHasProp ( filteredCounts , 'stock_status_counts' ) ||
109+ ! Array . isArray ( filteredCounts . stock_status_counts )
110+ ) {
99111 return null ;
100112 }
101113 return filteredCounts . stock_status_counts . find (
@@ -115,12 +127,13 @@ const StockStatusFilterBlock = ( {
115127 *
116128 * @param {string } queryStatus The status slug to check.
117129 */
118- const isStockStatusInQueryState = ( queryStatus ) => {
130+ const isStockStatusInQueryState = ( queryStatus : string ) => {
119131 if ( ! queryState ?. stock_status ) {
120132 return false ;
121133 }
122- return queryState . stock_status . some ( ( { status = [ ] } ) =>
123- status . includes ( queryStatus )
134+ return queryState . stock_status . some (
135+ ( { status = [ ] } : { status : string [ ] } ) =>
136+ status . includes ( queryStatus )
124137 ) ;
125138 } ;
126139
@@ -153,7 +166,7 @@ const StockStatusFilterBlock = ( {
153166 ) ,
154167 } ;
155168 } )
156- . filter ( Boolean ) ;
169+ . filter ( ( option ) : option is DisplayOption => ! ! option ) ;
157170
158171 setDisplayedOptions ( newOptions ) ;
159172 } , [
@@ -171,7 +184,7 @@ const StockStatusFilterBlock = ( {
171184 *
172185 * @param {Array } checkedOptions Array of checked stock options.
173186 */
174- const redirectPageForPhpTemplate = ( checkedOptions ) => {
187+ const redirectPageForPhpTemplate = ( checkedOptions : string [ ] ) => {
175188 if ( checkedOptions . length === 0 ) {
176189 const url = removeQueryArgs (
177190 window . location . href ,
@@ -252,12 +265,14 @@ const StockStatusFilterBlock = ( {
252265 useEffect ( ( ) => {
253266 if ( ! hasSetPhpFilterDefaults && filteringForPhpTemplate ) {
254267 setProductStockStatusQuery (
255- getActiveFilters ( STOCK_STATUS_OPTIONS , QUERY_PARAM_KEY )
268+ getActiveFilters (
269+ STOCK_STATUS_OPTIONS . current ,
270+ QUERY_PARAM_KEY
271+ )
256272 ) ;
257273 setHasSetPhpFilterDefaults ( true ) ;
258274 }
259275 } , [
260- STOCK_STATUS_OPTIONS ,
261276 filteringForPhpTemplate ,
262277 setProductStockStatusQuery ,
263278 hasSetPhpFilterDefaults ,
@@ -269,15 +284,22 @@ const StockStatusFilterBlock = ( {
269284 */
270285 const onChange = useCallback (
271286 ( checkedValue ) => {
272- const getFilterNameFromValue = ( filterValue ) => {
273- const { name } = displayedOptions . find (
287+ const getFilterNameFromValue = ( filterValue : string ) => {
288+ const filterOption = displayedOptions . find (
274289 ( option ) => option . value === filterValue
275290 ) ;
276291
277- return name ;
292+ if ( ! filterOption ) {
293+ return null ;
294+ }
295+
296+ return filterOption . name ;
278297 } ;
279298
280- const announceFilterChange = ( { filterAdded, filterRemoved } ) => {
299+ const announceFilterChange = ( {
300+ filterAdded,
301+ filterRemoved,
302+ } : Record < string , string > ) => {
281303 const filterAddedName = filterAdded
282304 ? getFilterNameFromValue ( filterAdded )
283305 : null ;
@@ -332,8 +354,9 @@ const StockStatusFilterBlock = ( {
332354 return null ;
333355 }
334356
335- const TagName = `h${ blockAttributes . headingLevel } ` ;
336- const isLoading = ! blockAttributes . isPreview && ! STOCK_STATUS_OPTIONS ;
357+ const TagName = `h${ blockAttributes . headingLevel } ` as keyof JSX . IntrinsicElements ;
358+ const isLoading =
359+ ! blockAttributes . isPreview && ! STOCK_STATUS_OPTIONS . current ;
337360 const isDisabled = ! blockAttributes . isPreview && filteredCountsLoading ;
338361
339362 const hasFilterableProducts = getSettingWithCoercion (
0 commit comments