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

Commit 2a94b77

Browse files
committed
extract change URL logic
1 parent 0c2e198 commit 2a94b77

File tree

5 files changed

+36
-56
lines changed

5 files changed

+36
-56
lines changed

assets/js/blocks/active-filters/utils.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ import { formatPrice } from '@woocommerce/price-format';
66
import { RemovableChip } from '@woocommerce/base-components/chip';
77
import Label from '@woocommerce/base-components/label';
88
import { getQueryArgs, addQueryArgs, removeQueryArgs } from '@wordpress/url';
9-
import { getSettingWithCoercion } from '@woocommerce/settings';
10-
import { isBoolean } from '@woocommerce/types';
11-
12-
const filteringForPhpTemplate = getSettingWithCoercion(
13-
'is_rendering_php_template',
14-
false,
15-
isBoolean
16-
);
9+
import { changeUrl } from '@woocommerce/utils';
1710

1811
/**
1912
* Format a min/max price range to display.
@@ -195,11 +188,7 @@ export const removeArgsFromFilterUrl = (
195188

196189
const newUrl = addQueryArgs( cleanUrl, filteredQuery );
197190

198-
if ( filteringForPhpTemplate ) {
199-
window.location.href = newUrl;
200-
} else {
201-
window.history.replaceState( {}, '', newUrl );
202-
}
191+
changeUrl( newUrl );
203192
};
204193

205194
/**
@@ -233,9 +222,5 @@ export const cleanFilterUrl = () => {
233222

234223
const newUrl = addQueryArgs( cleanUrl, remainingArgs );
235224

236-
if ( filteringForPhpTemplate ) {
237-
window.location.href = newUrl;
238-
} else {
239-
window.history.replaceState( {}, '', newUrl );
240-
}
225+
changeUrl( newUrl );
241226
};

assets/js/blocks/attribute-filter/block.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
objectHasProp,
3030
} from '@woocommerce/types';
3131
import {
32+
changeUrl,
3233
PREFIX_QUERY_ARG_FILTER_TYPE,
3334
PREFIX_QUERY_ARG_QUERY_TYPE,
3435
} from '@woocommerce/utils';
@@ -281,26 +282,18 @@ const AttributeFilterBlock = ( {
281282
);
282283

283284
const newUrl = formatParams( url, query );
284-
if ( filteringForPhpTemplate ) {
285-
window.location.href = newUrl;
286-
} else {
287-
window.history.replaceState( {}, '', newUrl );
288-
}
285+
changeUrl( newUrl );
289286
} else {
290287
const newUrl = formatParams( pageUrl, query );
291288
const currentQueryArgs = getQueryArgs( window.location.href );
292289
const newUrlQueryArgs = getQueryArgs( newUrl );
293290

294291
if ( ! isQueryArgsEqual( currentQueryArgs, newUrlQueryArgs ) ) {
295-
if ( filteringForPhpTemplate ) {
296-
window.location.href = newUrl;
297-
} else {
298-
window.history.replaceState( {}, '', newUrl );
299-
}
292+
changeUrl( newUrl );
300293
}
301294
}
302295
},
303-
[ pageUrl, attributeObject?.taxonomy, filteringForPhpTemplate ]
296+
[ pageUrl, attributeObject?.taxonomy ]
304297
);
305298

306299
const onSubmit = ( checkedFilters: string[] ) => {

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ 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 { changeUrl, getUrlParameter } from '@woocommerce/utils';
1718
import {
1819
CurrencyResponse,
1920
isBoolean,
@@ -25,7 +26,6 @@ import {
2526
* Internal dependencies
2627
*/
2728
import usePriceConstraints from './use-price-constraints';
28-
import { getUrlParameter } from '../../utils/filters';
2929
import './style.scss';
3030
import { Attributes } from './types';
3131

@@ -184,13 +184,7 @@ const PriceFilterBlock = ( {
184184

185185
// If the params have changed, lets update the filter URL.
186186
if ( window.location.href !== newUrl ) {
187-
// When filtering on the PHP template, we need to reload the page.
188-
if ( filteringForPhpTemplate ) {
189-
return ( window.location.href = newUrl );
190-
}
191-
192-
// When filtering All Products block, we only update the URL.
193-
window.history.replaceState( {}, '', newUrl );
187+
changeUrl( newUrl );
194188
}
195189
}
196190

@@ -202,7 +196,6 @@ const PriceFilterBlock = ( {
202196
maxConstraint,
203197
setMinPriceQuery,
204198
setMaxPriceQuery,
205-
filteringForPhpTemplate,
206199
currency.minorUnit,
207200
]
208201
);

assets/js/blocks/stock-filter/block.tsx

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import isShallowEqual from '@wordpress/is-shallow-equal';
2424
import { decodeEntities } from '@wordpress/html-entities';
2525
import { isBoolean, objectHasProp } from '@woocommerce/types';
2626
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
27-
import { PREFIX_QUERY_ARG_FILTER_TYPE } from '@woocommerce/utils';
27+
import { changeUrl, PREFIX_QUERY_ARG_FILTER_TYPE } from '@woocommerce/utils';
2828

2929
/**
3030
* Internal dependencies
@@ -180,13 +180,9 @@ const StockStatusFilterBlock = ( {
180180
/**
181181
* Used to redirect the page when filters are changed so templates using the Classic Template block can filter.
182182
*
183-
* @param {Array} checkedOptions Array of checked stock options.
184-
* @param {boolean} isPhpTemplate Whether filters are being used for a PHP template.
183+
* @param {Array} checkedOptions Array of checked stock options.
185184
*/
186-
const updateFilterUrl = (
187-
checkedOptions: string[],
188-
isPhpTemplate: boolean
189-
) => {
185+
const updateFilterUrl = ( checkedOptions: string[] ) => {
190186
if ( ! window ) {
191187
return;
192188
}
@@ -197,11 +193,7 @@ const StockStatusFilterBlock = ( {
197193
);
198194

199195
if ( url !== window.location.href ) {
200-
if ( isPhpTemplate ) {
201-
window.location.href = url;
202-
} else {
203-
window.history.replaceState( {}, '', url );
204-
}
196+
changeUrl( url );
205197
}
206198

207199
return;
@@ -215,11 +207,7 @@ const StockStatusFilterBlock = ( {
215207
return;
216208
}
217209

218-
if ( isPhpTemplate ) {
219-
window.location.href = newUrl;
220-
} else {
221-
window.history.replaceState( {}, '', newUrl );
222-
}
210+
changeUrl( newUrl );
223211
};
224212

225213
const onSubmit = useCallback(
@@ -231,7 +219,7 @@ const StockStatusFilterBlock = ( {
231219
setProductStockStatusQuery( checked );
232220
}
233221

234-
updateFilterUrl( checked, filteringForPhpTemplate );
222+
updateFilterUrl( checked );
235223
},
236224
[
237225
isEditor,

assets/js/utils/filters.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
* External dependencies
33
*/
44
import { getQueryArg } from '@wordpress/url';
5+
import { getSettingWithCoercion } from '@woocommerce/settings';
6+
import { isBoolean } from '@woocommerce/types';
7+
8+
const filteringForPhpTemplate = getSettingWithCoercion(
9+
'is_rendering_php_template',
10+
false,
11+
isBoolean
12+
);
513

614
/**
715
* Returns specified parameter from URL
@@ -18,3 +26,16 @@ export function getUrlParameter( name: string ) {
1826
}
1927
return getQueryArg( window.location.href, name );
2028
}
29+
30+
/**
31+
* Change the URL and reload the page if filtering for PHP templates.
32+
*
33+
* @param {string} newUrl New URL to be set.
34+
*/
35+
export function changeUrl( newUrl: string ) {
36+
if ( filteringForPhpTemplate ) {
37+
window.location.href = newUrl;
38+
} else {
39+
window.history.replaceState( {}, '', newUrl );
40+
}
41+
}

0 commit comments

Comments
 (0)