Skip to content

Commit

Permalink
Merge branch 'Weaverse:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
hta218 authored Dec 14, 2024
2 parents 72e1556 + 400b7cd commit fa4a0f5
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions app/sections/collection-filters/price-range-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,8 @@ export function PriceRangeFilter({
let navigate = useNavigate();
let thumbRef = useRef<"from" | "to" | null>(null);

let { highestPriceProduct, lowestPriceProduct } = collection;
let minVariantPrice =
lowestPriceProduct.nodes[0]?.priceRange?.minVariantPrice;
let maxVariantPrice =
highestPriceProduct.nodes[0]?.priceRange?.maxVariantPrice;

let priceFilter = params.get(`${FILTER_URL_PREFIX}price`);
let price = priceFilter
? (JSON.parse(priceFilter) as ProductFilter["price"])
: undefined;
let min = Number.isNaN(Number(price?.min))
? Number(minVariantPrice?.amount)
: Number(price?.min);
let max = Number.isNaN(Number(price?.max))
? Number(maxVariantPrice?.amount)
: Number(price?.max);
let { minVariantPrice, maxVariantPrice } = getPricesRange(collection);
let { min, max } = getPricesFromFilter(params);

let [minPrice, setMinPrice] = useState(min);
let [maxPrice, setMaxPrice] = useState(max);
Expand All @@ -55,19 +41,19 @@ export function PriceRangeFilter({
return (
<div className="space-y-4">
<Slider.Root
min={Number(minVariantPrice.amount)}
max={Number(maxVariantPrice.amount)}
min={minVariantPrice}
max={maxVariantPrice}
step={1}
minStepsBetweenThumbs={1}
value={[minPrice, maxPrice]}
value={[minPrice || minVariantPrice, maxPrice || maxVariantPrice]}
onValueChange={([newMin, newMax]) => {
if (thumbRef.current) {
if (thumbRef.current === "from") {
if (newMin < maxPrice) {
if (maxPrice === undefined || newMin < maxPrice) {
setMinPrice(newMin);
}
} else {
if (newMax > minPrice) {
if (minPrice === undefined || newMax > minPrice) {
setMaxPrice(newMax);
}
}
Expand Down Expand Up @@ -106,8 +92,8 @@ export function PriceRangeFilter({
name="minPrice"
type="number"
value={minPrice ?? ""}
min={minVariantPrice.amount}
placeholder={Number(minVariantPrice.amount).toString()}
min={minVariantPrice}
placeholder={minVariantPrice.toString()}
onChange={(e) => {
let { value } = e.target;
let newMinPrice = Number.isNaN(Number.parseFloat(value))
Expand All @@ -131,8 +117,8 @@ export function PriceRangeFilter({
name="maxPrice"
type="number"
value={maxPrice ?? ""}
max={maxVariantPrice.amount}
placeholder={Number(maxVariantPrice.amount).toString()}
max={maxVariantPrice}
placeholder={maxVariantPrice.toString()}
onChange={(e) => {
let { value } = e.target;
let newMaxPrice = Number.isNaN(Number.parseFloat(value))
Expand All @@ -148,3 +134,25 @@ export function PriceRangeFilter({
</div>
);
}

function getPricesRange(collection: CollectionDetailsQuery["collection"]) {
let { highestPriceProduct, lowestPriceProduct } = collection;
let minVariantPrice =
lowestPriceProduct.nodes[0]?.priceRange?.minVariantPrice;
let maxVariantPrice =
highestPriceProduct.nodes[0]?.priceRange?.maxVariantPrice;
return {
minVariantPrice: Number(minVariantPrice?.amount) || 0,
maxVariantPrice: Number(maxVariantPrice?.amount) || 1000,
};
}

function getPricesFromFilter(params: URLSearchParams) {
let priceFilter = params.get(`${FILTER_URL_PREFIX}price`);
let price = priceFilter
? (JSON.parse(priceFilter) as ProductFilter["price"])
: undefined;
let min = Number.isNaN(Number(price?.min)) ? undefined : Number(price?.min);
let max = Number.isNaN(Number(price?.max)) ? undefined : Number(price?.max);
return { min, max };
}

0 comments on commit fa4a0f5

Please sign in to comment.