Skip to content

Commit fa4a0f5

Browse files
authored
Merge branch 'Weaverse:main' into main
2 parents 72e1556 + 400b7cd commit fa4a0f5

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

app/sections/collection-filters/price-range-filter.tsx

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,8 @@ export function PriceRangeFilter({
1818
let navigate = useNavigate();
1919
let thumbRef = useRef<"from" | "to" | null>(null);
2020

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);
3723

3824
let [minPrice, setMinPrice] = useState(min);
3925
let [maxPrice, setMaxPrice] = useState(max);
@@ -55,19 +41,19 @@ export function PriceRangeFilter({
5541
return (
5642
<div className="space-y-4">
5743
<Slider.Root
58-
min={Number(minVariantPrice.amount)}
59-
max={Number(maxVariantPrice.amount)}
44+
min={minVariantPrice}
45+
max={maxVariantPrice}
6046
step={1}
6147
minStepsBetweenThumbs={1}
62-
value={[minPrice, maxPrice]}
48+
value={[minPrice || minVariantPrice, maxPrice || maxVariantPrice]}
6349
onValueChange={([newMin, newMax]) => {
6450
if (thumbRef.current) {
6551
if (thumbRef.current === "from") {
66-
if (newMin < maxPrice) {
52+
if (maxPrice === undefined || newMin < maxPrice) {
6753
setMinPrice(newMin);
6854
}
6955
} else {
70-
if (newMax > minPrice) {
56+
if (minPrice === undefined || newMax > minPrice) {
7157
setMaxPrice(newMax);
7258
}
7359
}
@@ -106,8 +92,8 @@ export function PriceRangeFilter({
10692
name="minPrice"
10793
type="number"
10894
value={minPrice ?? ""}
109-
min={minVariantPrice.amount}
110-
placeholder={Number(minVariantPrice.amount).toString()}
95+
min={minVariantPrice}
96+
placeholder={minVariantPrice.toString()}
11197
onChange={(e) => {
11298
let { value } = e.target;
11399
let newMinPrice = Number.isNaN(Number.parseFloat(value))
@@ -131,8 +117,8 @@ export function PriceRangeFilter({
131117
name="maxPrice"
132118
type="number"
133119
value={maxPrice ?? ""}
134-
max={maxVariantPrice.amount}
135-
placeholder={Number(maxVariantPrice.amount).toString()}
120+
max={maxVariantPrice}
121+
placeholder={maxVariantPrice.toString()}
136122
onChange={(e) => {
137123
let { value } = e.target;
138124
let newMaxPrice = Number.isNaN(Number.parseFloat(value))
@@ -148,3 +134,25 @@ export function PriceRangeFilter({
148134
</div>
149135
);
150136
}
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

Comments
 (0)