Skip to content

Commit 2af8106

Browse files
authored
Merge pull request #264 from codeableorg/branch-Jeff4
Refactor: To add functions about as filters for stickers category
2 parents f80a5e8 + 4dec2c4 commit 2af8106

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/routes/category/components/product-card/index.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ import { Button } from "@/components/ui";
77
interface ProductCardProps {
88
product: Product;
99
categorySlug: string;
10+
minPrice?: string;
11+
maxPrice?: string;
1012
}
1113

12-
export function ProductCard({ product, categorySlug }: ProductCardProps) {
14+
export function ProductCard({
15+
product,
16+
categorySlug,
17+
minPrice,
18+
maxPrice,
19+
}: ProductCardProps) {
1320
const navigate = useNavigate();
1421
const [hoveredPrice, setHoveredPrice] = useState<number | null>(null);
1522
let variantTitle: string | null = null;
@@ -18,8 +25,8 @@ export function ProductCard({ product, categorySlug }: ProductCardProps) {
1825

1926
const variantMap: Record<string, string> = {
2027
"3*3": "3*3",
21-
"5*5": "5*5",
22-
"10*10": "10*10"
28+
"5*5": "5*5",
29+
"10*10": "10*10",
2330
};
2431

2532
if (categorySlug === "polos") {
@@ -28,7 +35,17 @@ export function ProductCard({ product, categorySlug }: ProductCardProps) {
2835
variantParamName = "size";
2936
} else if (categorySlug === "stickers") {
3037
variantTitle = "Elige la medida";
31-
variants = ["3*3", "5*5", "10*10"];
38+
39+
const min = minPrice ? parseFloat(minPrice) : 0;
40+
const max = maxPrice ? parseFloat(maxPrice) : Infinity;
41+
42+
if (product.stickersVariants?.length) {
43+
variants = product.stickersVariants
44+
.filter((variant) => variant.price >= min && variant.price <= max)
45+
.map((variant) => variant.measure);
46+
} else {
47+
variants = ["3*3", "5*5", "10*10"];
48+
}
3249
variantParamName = "measure";
3350
}
3451

src/routes/category/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,29 @@ export async function loader({ params, request }: Route.LoaderArgs) {
3131
const filterProductsByPrice = (
3232
products: Product[],
3333
minPrice: string,
34-
maxPrice: string
34+
maxPrice: string,
35+
categorySlug: string
3536
) => {
3637
const min = minPrice ? parseFloat(minPrice) : 0;
3738
const max = maxPrice ? parseFloat(maxPrice) : Infinity;
38-
return products.filter(
39-
(product) => product.price >= min && product.price <= max
40-
);
39+
return products.filter((product) => {
40+
if (
41+
categorySlug === "stickers" &&
42+
product.stickersVariants?.length
43+
) {
44+
return product.stickersVariants.some(
45+
(variant) => variant.price >= min && variant.price <= max
46+
);
47+
}
48+
return product.price >= min && product.price <= max;
49+
});
4150
};
4251

4352
const filteredProducts = filterProductsByPrice(
4453
products,
4554
minPrice,
46-
maxPrice
55+
maxPrice,
56+
categorySlug
4757
);
4858

4959
return {
@@ -86,6 +96,8 @@ export default function Category({ loaderData }: Route.ComponentProps) {
8696
product={product}
8797
key={product.id}
8898
categorySlug={category.slug}
99+
minPrice={minPrice}
100+
maxPrice={maxPrice}
89101
/>
90102
))}
91103
</div>

0 commit comments

Comments
 (0)