Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12861288f2 | ||
|
|
73bfe0d893 | ||
|
|
a289c40503 | ||
|
|
f6736cd43f |
No files matched your search
@@ -76,6 +76,12 @@ const CATEGORY_MAP: Record<string, { category?: string; query?: string; symptoms
|
|||||||
"شستشوی چشم": { category: "special-care", query: "چشم" },
|
"شستشوی چشم": { 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 }) => {
|
export const ArchiveProductCard: React.FC<{ product: Product }> = ({ product }) => {
|
||||||
const { addItem } = useCartStore();
|
const { addItem } = useCartStore();
|
||||||
const cartItem = useCartStore((s) => s.items.find(i => i.product.id === product.id));
|
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)));
|
result = result.filter(p => p.symptoms && p.symptoms.some(s => activeSymptoms.includes(s)));
|
||||||
}
|
}
|
||||||
|
|
||||||
setFilteredProducts(result);
|
setFilteredProducts(orderProductsByAvailability(result));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch products:", error);
|
console.error("Failed to fetch products:", error);
|
||||||
toast.error("خطا در دریافت لیست محصولات از سرور. لطفاً صفحه را رفرش کنید.");
|
toast.error("خطا در دریافت لیست محصولات از سرور. لطفاً صفحه را رفرش کنید.");
|
||||||
@@ -1087,35 +1093,49 @@ export default function ArchivePage({
|
|||||||
)
|
)
|
||||||
) : filteredProducts.length > 0 ? (
|
) : filteredProducts.length > 0 ? (
|
||||||
<>
|
<>
|
||||||
{/* Mobile List View vs Card View (Desktop is always responsive multi-column grid) */}
|
{/* Keep API sort order inside each availability section. */}
|
||||||
<div
|
{([{
|
||||||
key={viewMode}
|
key: "available",
|
||||||
className={
|
title: "محصولات موجود",
|
||||||
viewMode === "list"
|
products: filteredProducts.filter(product => !isProductOutOfStock(product)),
|
||||||
? "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"
|
key: "unavailable",
|
||||||
}
|
title: "محصولات ناموجود",
|
||||||
>
|
products: filteredProducts.filter(product => isProductOutOfStock(product)),
|
||||||
<AnimatePresence mode="popLayout">
|
}] as const).map(section => section.products.length > 0 && (
|
||||||
{filteredProducts.map((p: Product) => (
|
<section key={section.key} data-testid={`products-${section.key}`} className="mb-10 last:mb-0">
|
||||||
<React.Fragment key={p.id}>
|
<h2 className="mb-5 text-xl font-black text-medical-gray-900">{section.title}</h2>
|
||||||
{/* In list mode on mobile, render ArchiveProductListItem; on desktop (lg+), render standard card */}
|
{/* Mobile List View vs Card View (Desktop is always responsive multi-column grid) */}
|
||||||
{viewMode === "list" ? (
|
<div
|
||||||
<>
|
key={viewMode}
|
||||||
<div className="block lg:hidden w-full">
|
className={
|
||||||
<ArchiveProductListItem product={p} />
|
viewMode === "list"
|
||||||
</div>
|
? "flex flex-col gap-3 lg:grid lg:grid-cols-2 xl:grid-cols-4 lg:gap-6"
|
||||||
<div className="hidden lg:block h-full">
|
: "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} />
|
<ArchiveProductCard product={p} />
|
||||||
</div>
|
)}
|
||||||
</>
|
</React.Fragment>
|
||||||
) : (
|
))}
|
||||||
<ArchiveProductCard product={p} />
|
</AnimatePresence>
|
||||||
)}
|
</div>
|
||||||
</React.Fragment>
|
</section>
|
||||||
))}
|
))}
|
||||||
</AnimatePresence>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* SEO Category Rich Description Section (Rendered below products to maintain primary UX while boosting category SEO) */}
|
{/* SEO Category Rich Description Section (Rendered below products to maintain primary UX while boosting category SEO) */}
|
||||||
{selectedCategory !== "all" && (
|
{selectedCategory !== "all" && (
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
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 { productService } from "../../lib/services/productService";
|
||||||
import { useCartStore } from "../../lib/store/cartStore";
|
import { useCartStore } from "../../lib/store/cartStore";
|
||||||
import { usePetStore } from "../../lib/store/usePetStore";
|
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", () => {
|
describe("ArchivePage Component", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
|
|||||||
Reference in new issue
Block a user