feat(shop): show available products first

This commit is contained in:
devops committed 2026-10-14 20:04:50 +00:00
1 parent 73bfe0d893
commit 12861288f2
2 files changed
+76 -30

No files matched your search

+49 -29
View File
@@ -76,6 +76,12 @@ const CATEGORY_MAP: Record<string, { category?: string; query?: string; symptoms
"شستشوی چشم": { category: "special-care", query: "چشم" },
};
export const orderProductsByAvailability = (products: Product[]): Product[] => {
const available = products.filter(product => !isProductOutOfStock(product));
const unavailable = products.filter(product => isProductOutOfStock(product));
return [...available, ...unavailable];
};
export const ArchiveProductCard: React.FC<{ product: Product }> = ({ product }) => {
const { addItem } = useCartStore();
const cartItem = useCartStore((s) => s.items.find(i => i.product.id === product.id));
@@ -572,7 +578,7 @@ export default function ArchivePage({
result = result.filter(p => p.symptoms && p.symptoms.some(s => activeSymptoms.includes(s)));
}
setFilteredProducts(result);
setFilteredProducts(orderProductsByAvailability(result));
} catch (error) {
console.error("Failed to fetch products:", error);
toast.error("خطا در دریافت لیست محصولات از سرور. لطفاً صفحه را رفرش کنید.");
@@ -1087,35 +1093,49 @@ export default function ArchivePage({
)
) : filteredProducts.length > 0 ? (
<>
{/* Mobile List View vs Card View (Desktop is always responsive multi-column grid) */}
<div
key={viewMode}
className={
viewMode === "list"
? "flex flex-col gap-3 lg:grid lg:grid-cols-2 xl:grid-cols-4 lg:gap-6"
: "grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-6"
}
>
<AnimatePresence mode="popLayout">
{filteredProducts.map((p: Product) => (
<React.Fragment key={p.id}>
{/* In list mode on mobile, render ArchiveProductListItem; on desktop (lg+), render standard card */}
{viewMode === "list" ? (
<>
<div className="block lg:hidden w-full">
<ArchiveProductListItem product={p} />
</div>
<div className="hidden lg:block h-full">
{/* Keep API sort order inside each availability section. */}
{([{
key: "available",
title: "محصولات موجود",
products: filteredProducts.filter(product => !isProductOutOfStock(product)),
}, {
key: "unavailable",
title: "محصولات ناموجود",
products: filteredProducts.filter(product => isProductOutOfStock(product)),
}] as const).map(section => section.products.length > 0 && (
<section key={section.key} data-testid={`products-${section.key}`} className="mb-10 last:mb-0">
<h2 className="mb-5 text-xl font-black text-medical-gray-900">{section.title}</h2>
{/* Mobile List View vs Card View (Desktop is always responsive multi-column grid) */}
<div
key={viewMode}
className={
viewMode === "list"
? "flex flex-col gap-3 lg:grid lg:grid-cols-2 xl:grid-cols-4 lg:gap-6"
: "grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-6"
}
>
<AnimatePresence mode="popLayout">
{section.products.map((p: Product) => (
<React.Fragment key={p.id}>
{/* In list mode on mobile, render ArchiveProductListItem; on desktop (lg+), render standard card */}
{viewMode === "list" ? (
<>
<div className="block lg:hidden w-full">
<ArchiveProductListItem product={p} />
</div>
<div className="hidden lg:block h-full">
<ArchiveProductCard product={p} />
</div>
</>
) : (
<ArchiveProductCard product={p} />
</div>
</>
) : (
<ArchiveProductCard product={p} />
)}
</React.Fragment>
))}
</AnimatePresence>
</div>
)}
</React.Fragment>
))}
</AnimatePresence>
</div>
</section>
))}
{/* SEO Category Rich Description Section (Rendered below products to maintain primary UX while boosting category SEO) */}
{selectedCategory !== "all" && (
@@ -2,7 +2,7 @@
import React from "react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import ArchivePage from "../ArchivePage";
import ArchivePage, { orderProductsByAvailability } from "../ArchivePage";
import { productService } from "../../lib/services/productService";
import { useCartStore } from "../../lib/store/cartStore";
import { usePetStore } from "../../lib/store/usePetStore";
@@ -117,6 +117,32 @@ const mockProducts = [
},
];
it("puts available products first while preserving descending-price sort order within groups", () => {
const sortedByPriceDesc = [
{ id: "out-high", stock: 0 },
{ id: "in-mid", stock: 5 },
{ id: "in-low", stock: 2 },
{ id: "out-low", stock: 0 },
] as any;
expect(orderProductsByAvailability(sortedByPriceDesc).map(product => product.id)).toEqual([
"in-mid", "in-low", "out-high", "out-low",
]);
});
it("puts available products first while preserving ascending-price sort order and groups status fields", () => {
const sortedByPriceAsc = [
{ id: "in-low", stockStatus: "IN_STOCK" },
{ id: "out-mid", stockStatus: "OUT_OF_STOCK" },
{ id: "in-high", stockStatus: "IN_STOCK" },
{ id: "out-discontinued", stockStatus: "DISCONTINUED" },
] as any;
expect(orderProductsByAvailability(sortedByPriceAsc).map(product => product.id)).toEqual([
"in-low", "in-high", "out-mid", "out-discontinued",
]);
});
describe("ArchivePage Component", () => {
beforeEach(() => {
vi.clearAllMocks();