From 12861288f2880ff650ebdac0e4e3c9967c2112d0 Mon Sep 17 00:00:00 2001 From: devops Date: Wed, 14 Oct 2026 20:04:50 +0000 Subject: [PATCH] feat(shop): show available products first --- .../application/components/ArchivePage.tsx | 78 ++++++++++++------- .../components/__tests__/ArchivePage.test.tsx | 28 ++++++- 2 files changed, 76 insertions(+), 30 deletions(-) diff --git a/frontend/application/components/ArchivePage.tsx b/frontend/application/components/ArchivePage.tsx index bcbe0924..631baf83 100644 --- a/frontend/application/components/ArchivePage.tsx +++ b/frontend/application/components/ArchivePage.tsx @@ -76,6 +76,12 @@ const CATEGORY_MAP: Record { + 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) */} -
- - {filteredProducts.map((p: Product) => ( - - {/* In list mode on mobile, render ArchiveProductListItem; on desktop (lg+), render standard card */} - {viewMode === "list" ? ( - <> -
- -
-
+ {/* 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.title}

+ {/* Mobile List View vs Card View (Desktop is always responsive multi-column grid) */} +
+ + {section.products.map((p: Product) => ( + + {/* In list mode on mobile, render ArchiveProductListItem; on desktop (lg+), render standard card */} + {viewMode === "list" ? ( + <> +
+ +
+
+ +
+ + ) : ( -
- - ) : ( - - )} - - ))} - -
+ )} +
+ ))} +
+
+ + ))} {/* SEO Category Rich Description Section (Rendered below products to maintain primary UX while boosting category SEO) */} {selectedCategory !== "all" && ( diff --git a/frontend/application/components/__tests__/ArchivePage.test.tsx b/frontend/application/components/__tests__/ArchivePage.test.tsx index 5b02650c..bd3e2da2 100644 --- a/frontend/application/components/__tests__/ArchivePage.test.tsx +++ b/frontend/application/components/__tests__/ArchivePage.test.tsx @@ -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();