@@ -18,22 +18,8 @@ export function PriceRangeFilter({
18
18
let navigate = useNavigate ( ) ;
19
19
let thumbRef = useRef < "from" | "to" | null > ( null ) ;
20
20
21
- let { highestPriceProduct, lowestPriceProduct } = collection ;
22
- let minVariantPrice =
23
- lowestPriceProduct . nodes [ 0 ] ?. priceRange ?. minVariantPrice ;
24
- let maxVariantPrice =
25
- highestPriceProduct . nodes [ 0 ] ?. priceRange ?. maxVariantPrice ;
26
-
27
- let priceFilter = params . get ( `${ FILTER_URL_PREFIX } price` ) ;
28
- let price = priceFilter
29
- ? ( JSON . parse ( priceFilter ) as ProductFilter [ "price" ] )
30
- : undefined ;
31
- let min = Number . isNaN ( Number ( price ?. min ) )
32
- ? Number ( minVariantPrice ?. amount )
33
- : Number ( price ?. min ) ;
34
- let max = Number . isNaN ( Number ( price ?. max ) )
35
- ? Number ( maxVariantPrice ?. amount )
36
- : Number ( price ?. max ) ;
21
+ let { minVariantPrice, maxVariantPrice } = getPricesRange ( collection ) ;
22
+ let { min, max } = getPricesFromFilter ( params ) ;
37
23
38
24
let [ minPrice , setMinPrice ] = useState ( min ) ;
39
25
let [ maxPrice , setMaxPrice ] = useState ( max ) ;
@@ -55,19 +41,19 @@ export function PriceRangeFilter({
55
41
return (
56
42
< div className = "space-y-4" >
57
43
< Slider . Root
58
- min = { Number ( minVariantPrice . amount ) }
59
- max = { Number ( maxVariantPrice . amount ) }
44
+ min = { minVariantPrice }
45
+ max = { maxVariantPrice }
60
46
step = { 1 }
61
47
minStepsBetweenThumbs = { 1 }
62
- value = { [ minPrice , maxPrice ] }
48
+ value = { [ minPrice || minVariantPrice , maxPrice || maxVariantPrice ] }
63
49
onValueChange = { ( [ newMin , newMax ] ) => {
64
50
if ( thumbRef . current ) {
65
51
if ( thumbRef . current === "from" ) {
66
- if ( newMin < maxPrice ) {
52
+ if ( maxPrice === undefined || newMin < maxPrice ) {
67
53
setMinPrice ( newMin ) ;
68
54
}
69
55
} else {
70
- if ( newMax > minPrice ) {
56
+ if ( minPrice === undefined || newMax > minPrice ) {
71
57
setMaxPrice ( newMax ) ;
72
58
}
73
59
}
@@ -106,8 +92,8 @@ export function PriceRangeFilter({
106
92
name = "minPrice"
107
93
type = "number"
108
94
value = { minPrice ?? "" }
109
- min = { minVariantPrice . amount }
110
- placeholder = { Number ( minVariantPrice . amount ) . toString ( ) }
95
+ min = { minVariantPrice }
96
+ placeholder = { minVariantPrice . toString ( ) }
111
97
onChange = { ( e ) => {
112
98
let { value } = e . target ;
113
99
let newMinPrice = Number . isNaN ( Number . parseFloat ( value ) )
@@ -131,8 +117,8 @@ export function PriceRangeFilter({
131
117
name = "maxPrice"
132
118
type = "number"
133
119
value = { maxPrice ?? "" }
134
- max = { maxVariantPrice . amount }
135
- placeholder = { Number ( maxVariantPrice . amount ) . toString ( ) }
120
+ max = { maxVariantPrice }
121
+ placeholder = { maxVariantPrice . toString ( ) }
136
122
onChange = { ( e ) => {
137
123
let { value } = e . target ;
138
124
let newMaxPrice = Number . isNaN ( Number . parseFloat ( value ) )
@@ -148,3 +134,25 @@ export function PriceRangeFilter({
148
134
</ div >
149
135
) ;
150
136
}
137
+
138
+ function getPricesRange ( collection : CollectionDetailsQuery [ "collection" ] ) {
139
+ let { highestPriceProduct, lowestPriceProduct } = collection ;
140
+ let minVariantPrice =
141
+ lowestPriceProduct . nodes [ 0 ] ?. priceRange ?. minVariantPrice ;
142
+ let maxVariantPrice =
143
+ highestPriceProduct . nodes [ 0 ] ?. priceRange ?. maxVariantPrice ;
144
+ return {
145
+ minVariantPrice : Number ( minVariantPrice ?. amount ) || 0 ,
146
+ maxVariantPrice : Number ( maxVariantPrice ?. amount ) || 1000 ,
147
+ } ;
148
+ }
149
+
150
+ function getPricesFromFilter ( params : URLSearchParams ) {
151
+ let priceFilter = params . get ( `${ FILTER_URL_PREFIX } price` ) ;
152
+ let price = priceFilter
153
+ ? ( JSON . parse ( priceFilter ) as ProductFilter [ "price" ] )
154
+ : undefined ;
155
+ let min = Number . isNaN ( Number ( price ?. min ) ) ? undefined : Number ( price ?. min ) ;
156
+ let max = Number . isNaN ( Number ( price ?. max ) ) ? undefined : Number ( price ?. max ) ;
157
+ return { min, max } ;
158
+ }
0 commit comments